Max
Max

Reputation: 63

Flutter How do I change the scroll speed in ListView on the mouse wheel?

I'm a beginner. I'm writing an application on Flutter under Windows. The problem is that the text in the ListView scrolls too slowly by the mouse clip. I tried to override ScrollPhysics, but it didn't work. Please give a working way to change the scrolling speed.

Upvotes: 6

Views: 8423

Answers (2)

Dabbel
Dabbel

Reputation: 2825

For people finding this post: Based on the accepted answer above, this custom class that can be thrown in and used throughout an application.

Customized AdjustableScrollController

// scrollcontroller.dart

import 'dart:math';
import 'package:flutter/rendering.dart';
import 'package:flutter/material.dart';

class AdjustableScrollController extends ScrollController {
  AdjustableScrollController([int extraScrollSpeed = 40]) {
    super.addListener(() {
      ScrollDirection scrollDirection = super.position.userScrollDirection;
      if (scrollDirection != ScrollDirection.idle) {
        double scrollEnd = super.offset +
            (scrollDirection == ScrollDirection.reverse
                ? extraScrollSpeed
                : -extraScrollSpeed);
        scrollEnd = min(super.position.maxScrollExtent,
            max(super.position.minScrollExtent, scrollEnd));
        jumpTo(scrollEnd);
      }
    });
  }
}

Usage

// your_file.dart

ListView(
  controller: AdjustableScrollController() // default is 40

or

// your_file.dart

ListView(
  controller: AdjustableScrollController(80)  // scroll even faster

Upvotes: 8

user14858121
user14858121

Reputation:

   class ScrollViewTest extends StatelessWidget{
    
    static const _extraScrollSpeed = 80; // your "extra" scroll speed
    final ScrollController _scrollController = ScrollController();

  // Constructor
   
    ScrollViewTest({Key? key}) : super(key: key)
    {
        _scrollController.addListener(() {
        ScrollDirection scrollDirection = _scrollController.position.userScrollDirection;
      if (scrollDirection != ScrollDirection.idle)
      {
        double scrollEnd = _scrollController.offset + (scrollDirection == ScrollDirection.reverse
                       ? _extraScrollSpeed
                       : -_extraScrollSpeed);
        scrollEnd = min(
                 _scrollController.position.maxScrollExtent,
                 max(_scrollController.position.minScrollExtent, scrollEnd));
        _scrollController.jumpTo(scrollEnd);
      }
    });
  }

  @override
  
    Widget build(BuildContext context)
    {
    
    return SingleChildScrollView(
      controller: _scrollController,
      child: Container(...),
    );
  }
}

Upvotes: 6

Related Questions