Simulate touch drag with mouse

Issue

In flutter 2.2, you could use mouse to swipe up and down on a ListView. It would act like swiping by finger on touch screens. However in version 2.5 this seems to have been removed and now clicking won’t do what touch does, for example over-scrolling a BouncingScrollPhysics (something I’m relying on in my app).

Is there any way to re-enable the simulation functionality?

Solution

In newer versions the mouse drag ability has been removed on scrollable widgets however you can enable it in two ways.

First you need to create this class:

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

Then enable it in your app like this:

1. Enabling it on just one widget

Wrap your scrollable inside a ScrollConfiguration and set the behavior:

ScrollConfiguration(
  behavior: MyCustomScrollBehavior(),
  child: ListView(
    ...
  )
)

2. Enabling it appwide

In your MaterialApp set the scrollBehavior:

MaterialApp(
  scrollBehavior: MyCustomScrollBehavior(),
  ...
)

TIP: If you have multiple nested MaterialApps, you have to set scrollBehavior on all of them.

Reference

Answered By – SIMMORSAL

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *