Home > OS >  flutter: How to enable touch gestures on Web and Windows (since update 2.5)
flutter: How to enable touch gestures on Web and Windows (since update 2.5)

Time:09-22

Since Flutter 2.5 got released, touch gestures are not working anymore (for example with ScrollViwes or Tabs). I didn't test in on Android / IOS, since I am not developing for these platforms at the time, but I assume it is just disabled for PC platforms by default, but I could not find any resource about this and don't know how to turn it on! (I had the same issue on two independent workstations with multiple independent projects, in debug and release)

Edit

Example:

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        body: ListView.builder(
          itemBuilder: (buildContext, index) => ListTile(
            title: Text("test "   index.toString()),
          ),
        ),
      ),
    ),
  );
}

CodePudding user response:

This was a breaking change in Flutter 2.5, but there is a migration guide from Flutter if you still want this behavior: https://flutter.dev/docs/release/breaking-changes/default-scroll-behavior-drag#migration-guide.

You have to write your own ScrollBehavior:

class MyScrollBehavior extends MaterialScrollBehavior {
  @override
  Set<PointerDeviceKind> get dragDevices => { 
    PointerDeviceKind.touch,
    PointerDeviceKind.mouse,
    ...
  };
}

MaterialApp(
  scrollBehavior: MyScrollBehavior(),
  ...
);

CodePudding user response:

This is my implementation now:

class MyCustomScrollBehavior extends MaterialScrollBehavior {
  const MyCustomScrollBehavior();

  @override
  Set<PointerDeviceKind> get dragDevices => {
        PointerDeviceKind.mouse,
      }..addAll(super.dragDevices);
}

In main:

MaterialApp(
  scrollBehavior: const MyCustomScrollBehavior(),
  home: ...,
);

The advantage is, that you don't need to worry about other PointerDeviceKind, because the super getter as other ones too!

  • Related