Why my Flutter BottomSheet gives me a GestureDetector error?

Viewed 1453

I have a problem with this BottomSheet, it works fine but as soon as I tap on it, I will get an error...

bottomSheet: showBottomSheet
        ? BottomSheet(
            enableDrag: true,
            onClosing: () {},
            builder: (ctx) => Container(
              height: 200,
              width: double.infinity,
              color: Colors.red,
            ),
          )

Why I am getting this error?

════════ Exception caught by gesture ═══════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The getter 'status' was called on null.
Receiver: null
Tried calling: status

When the exception was thrown, this was the stack
#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)
#1      _BottomSheetState._dismissUnderway 
package:flutter/…/material/bottom_sheet.dart:187
#2      _BottomSheetState._handleDragEnd 
package:flutter/…/material/bottom_sheet.dart:204
#3      DragGestureRecognizer._checkEnd.<anonymous closure> 
package:flutter/…/gestures/monodrag.dart:439
#4      GestureRecognizer.invokeCallback 
package:flutter/…/gestures/recognizer.dart:182
...
Handler: "onEnd"
Recognizer: VerticalDragGestureRecognizer#22607
    debugOwner: GestureDetector
    start behavior: start

What do I need to add? I guess I need to add an OnDragEnd function? Please help, thanks

2 Answers

As commented, just put enableDrag: false, and it will work.

The BottomSheet widget requires an animationController if enableDrag is set to true.

So:

bottomSheet: showBottomSheet
        ? BottomSheet(
            enableDrag: true,
            animationController: BottomSheet.createAnimationController(this),
            onClosing: () {},
            builder: (ctx) => Container(
              height: 200,
              width: double.infinity,
              color: Colors.red,
            ),
          )

If you get errors about this not being a TickerProvider, you can add the TickerProviderStateMixin:

class _MyViewState extends State<MyView> with TickerProviderStateMixin {

(All that being said, the docs for BottomSheet say that it in general shouldn’t be used directly. I’m personally not yet sure whether one should do the above solution vs. using showBottomSheet.)

Related