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

858

Solution 1

   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(...),
    );
  }
}

Solution 2

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
Share:
858
Max
Author by

Max

Updated on January 02, 2023

Comments

  • Max
    Max over 1 year

    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.

  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.