首页 > 解决方案 > 有没有办法在设备方向更改时关闭模态底页?

问题描述

我已经尝试过:

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

在 _BottomContentState 中,但是当我切换设备的方向时这给了我一个错误。

另外,我希望 ModalBottomSheet 在打开时关闭,否则不采取任何行动

标签: flutterflutter-showmodalbottomsheet

解决方案


您可以使用OrientationBuilder小部件以编程方式关闭您的模态底页。

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

推荐阅读