Flutter :无法用鼠标向下滚动(拖动)解决
2024-8-14 18:44:50 Author: www.uedbox.com(查看原文) 阅读量:5 收藏

  • 发表于
  • flutter

Flutter 在 2.5 之后改变了鼠标滚动行为。详细请参阅此

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

//为了支持鼠标滑动,我们需要通过以下步骤更改应用程序的默认滚动行为:

//1-创建一个类,从MaterialScrollBeavior扩展它并覆盖dragDevices

//2-将AppScrollBehavior实例传递给MaterialApp的scrollBehavior属性:

class MyCustomScrollBehavior extends MaterialScrollBehavior {

  // Override behavior methods and getters like dragDevices

  Set<PointerDeviceKind> get dragDevices => {

        PointerDeviceKind.touch,

        PointerDeviceKind.mouse,

      };

}

// 在整个应用顶层使用AppScrollBehavior

class MyApp extends StatelessWidget {

  const MyApp({super.key});

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      debugShowCheckedModeBanner: false,

      title: "flutterDemo",

      theme: ThemeData(

        primarySwatch: Colors.blue,

      ),

      home: const HomePage(),

      scrollBehavior: MyCustomScrollBehavior(),

    );

  }

}

// 在小部件中使用

// ScrollBehavior can be set for a specific widget.

final ScrollController controller = ScrollController();

ScrollConfiguration(

  behavior: MyCustomScrollBehavior(),

  child: ListView.builder(

    controller: controller,

    itemBuilder: (BuildContext context, int index) {

     return Text('Item $index');

    }

  ),

);


文章来源: https://www.uedbox.com/post/69688/
如有侵权请联系:admin#unsafe.sh