Search code examples
flutterflutter-showmodalbottomsheet

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


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


Solution

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

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