Flutter 如何改变ListView中鼠标滚轮的滚动速度?

Max*_*Max 5 windows desktop flutter

我是初学者。我正在 Windows 下编写一个关于 Flutter 的应用程序。问题是 ListView 中的文本通过鼠标剪辑滚动得太慢。我尝试覆盖 ScrollPhysics,但没有成功。请提供一种改变滚动速度的工作方法。

Dab*_*bel 7

对于找到这篇文章的人:根据上面接受的答案,这个自定义类可以在整个应用程序中投入和使用。

定制的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);
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

用法

// your_file.dart

ListView(
  controller: AdjustableScrollController() // default is 40
Run Code Online (Sandbox Code Playgroud)

或者

// your_file.dart

ListView(
  controller: AdjustableScrollController(80)  // scroll even faster
Run Code Online (Sandbox Code Playgroud)


小智 5

   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(...),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)