Is there a way to close a modal bottom sheet in flutter on device orientation change?

726 views Asked by At

I've already tried:

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    Orientation orientation = MediaQuery.of(context).orientation;
    (orientation == Orientation.portrait)
        ? print(orientation)
        : Navigator.pop(context);
  }

in the _BottomContentState but that gives me an error when I switch the orientation of the device.

Additionally I want the ModalBottomSheet to close if it is open, otherwise no action

1

There are 1 answers

1
Guillaume Roux On BEST ANSWER

You could use an OrientationBuilder widget to programatically close your modal bottom sheet.

OrientationBuilder(
   builder: (context, orientation) {
      if (orientation == Orientation.landscape) {
         // Close your modal
      }
   }
);