flutter: How to enable touch gestures on Web and Windows (since update 2.5)
Asked Answered
D

2

5

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()),
          ),
        ),
      ),
    ),
  );
}
Department answered 20/9, 2021 at 11:31 Comment(1)
Can you include sample code to reproduce the errors?Bertsche
D
7

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(),
  ...
);
Deviationism answered 20/9, 2021 at 13:12 Comment(2)
awesome you saved the day !Idolism
how can I emulate the multi touch on windows emulator ?Tottering
D
1

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!

Department answered 21/9, 2021 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.