In Flutter, how can a child widget prevent the scrolling of its scrollable parent?
Asked Answered
C

3

22

I have a scrollable widget, say a ListView, which contains some special widget.

How can I prevent the scrollable to scroll when the user tries to scroll by starting to scroll on top of that widget?

In other words, I want that widget to be like a "hole" that prevents the scrollable to sense gestures there.

Cuneal answered 28/2, 2019 at 15:45 Comment(2)
Can that child be a parent of the scroll instead? That's contrary to how the widget system works, where descendants cannot alter the behavior of their parents.Palpable
@RémiRousselet No, if the child is scrolling inside of the scrollable, how could it be the parent? Can't the child somehow "capture" the gesture? I don't know much about the gesture arena and related classes...Cuneal
D
14

Wrap the widget with GestureDetector and implement empty gesture functions in it.

GestureDetector(
   onVerticalDragUpdate: (_) {},
   child: YourWidget
),
Diluent answered 1/3, 2019 at 6:43 Comment(3)
Trivial, now that I know.Cuneal
This prevents the child from receiving the gesture also.Dotation
How is this a correct answer, the child does not receive the gesture and is frustrating for the user.Arenicolous
T
1

If your child is WebView, GoogleMap or similar, you can make it to use EagerGestureRecognizer:

WebView(
  ...
  gestureRecognizers: {
    Factory<OneSequenceGestureRecognizer>(
      () => EagerGestureRecognizer(),
    ),
  },
),
Twotone answered 30/4, 2023 at 9:19 Comment(0)
B
0

The answer is not satisfying because it prevents the child from receiving gestures (at least on native platforms).

Only acceptable solution I found -- in my case with a PageView -- is to disable the scroll gestures when the page with the gesturedetector child displays.

If anyone has a cleaner solution, please let us know.

Barytes answered 12/12, 2022 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.