Flutter :无法用鼠标向下滚动(拖动)解决

  • 发表于
  • flutter

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

//为了支持鼠标滑动,我们需要通过以下步骤更改应用程序的默认滚动行为:
//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');
}
),
);