Max
Max

Reputation: 93

Is it possible to make the bottom sheet unclosable?

I have a modalBottomSheet waiting for Future with Progress Indicator. I want to make it so that there is no way for the user to close this panel by clicking on the dark area above the panel, by swiping the panel down and (this is probably more difficult, but still) by using the back button on the android. I need to show user the answer from Future. And I cannot, as I understand it, change the enableDrag and isDismissible properties, since this showModalBottomSheet is a method

The only solution I see is a full screen container, but sheet is called with a new route.

Is there a solution on how to do this?

My not working workaround (that i wrap home widget):

class GestureOverlay extends StatelessWidget {
  final Widget child;

  const GestureOverlay({Key key, @required this.child}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [child, GestureOverlayContainer()],
    );
  }
}

class GestureOverlayContainer extends StatefulWidget {
  @override
  _GestureOverlayContainerState createState() =>
      _GestureOverlayContainerState();
}

class _GestureOverlayContainerState extends State<GestureOverlayContainer> {
  StreamSubscription subscription;
  bool gesture = true;

  @override
  void initState() {
    subscription = appBloc.gestureOverlay
        .listen((event) => setState(() => gesture = event));
    super.initState();
  }

  @override
  void dispose() {
    subscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: gesture ? 0 : double.infinity,
      color: Colors.red,
    );
  }
}

Upvotes: 0

Views: 365

Answers (1)

Omer
Omer

Reputation: 102

I think catching the pop action is what you want to implement here.
There is a function in Scaffold called onWillPop, This will catch the popping action and you can decide what to do when popping. For instance, do nothing and just don't let it pop out.

Future<bool> _willPopCallback() async {
// await showDialog or Show add banners or whatever
// then
return true; // return true if the route to be popped
}


//then pass the callback to WillPopScope
new WillPopScope(child: new Scaffold(), onWillPop: _willPopCallback)

Upvotes: 1

Related Questions