【问题标题】:Is there a way to close a modal bottom sheet in flutter on device orientation change?有没有办法在设备方向更改时关闭模态底页?
【发布时间】:2021-03-18 23:24:21
【问题描述】:

我已经试过了:

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

在 _BottomContentState 中,但是当我切换设备的方向时会出现错误。

另外,如果 ModalBottomSheet 打开,我希望它关闭,否则不执行任何操作

【问题讨论】:

    标签: flutter flutter-showmodalbottomsheet


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,成功了。我不得不在'showModalBottomSheet'函数中放置一个OrientationBuilder,而不是在didChangeDependencies()方法中
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 2014-07-08
    • 2020-04-07
    • 2020-09-27
    • 2013-02-18
    • 2020-04-24
    相关资源
    最近更新 更多