【问题标题】:Detect back button press while dialog is open in flutter在颤动中打开对话框时检测返回按钮按下
【发布时间】:2019-04-08 22:43:24
【问题描述】:

我正在创建一个应用程序,我需要在其中显示一个警报对话框。这不是一个可忽略的对话。但是当我按下 android 上的后退按钮时,它会被解雇。我尝试使用 WillPopScope 小部件来检测回压事件。我能够使用 WillPopScope 检测后退按钮按下,但这在对话框打开时不起作用。任何建议和指导都会非常有帮助。谢谢。

对话框创建sn-p:

void buildMaterialDialog(
  String dialogTitle,
  String dialogContent,
  String negativeBtnText,
  String positiveBtnText,
  String positiveTextUri) {

showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return new AlertDialog(
        title: new Text(dialogTitle),
        content: new Text(dialogContent),
        actions: <Widget>[
          new FlatButton(
            onPressed: () {
              //Function called
              _updateDialogNegBtnClicked(isCancelable);
            },
            child: new Text(negativeBtnText),
          ),
          new FlatButton(
            onPressed: () => launch(positiveTextUri),
            child: new Text(positiveBtnText),
          ),
        ],
      );
    });}

【问题讨论】:

  • 您要检测回压还是阻止关闭对话框?
  • 我想阻止对话框关闭
  • 你能分享一下sn-p @anmol.majhail

标签: android user-interface dialog flutter flutter-layout


【解决方案1】:

后退按钮不会关闭对话框。

showDialog(
  context: context,
  barrierDismissible: false,
  builder: (BuildContext context) {
    return WillPopScope(
      onWillPop: () async => false,
      child: AlertDialog(
        title: Text('Title'),
        content: Text('This is Demo'),
        actions: <Widget>[
          FlatButton(
            onPressed: () => Navigator.pop(context),
            child: Text('Go Back'),
          ),
        ],
      ),
    );
  },
);

【讨论】:

  • 太好了。但一直以来,我一直在 onWillPop 中返回一个 Future,比如 onWillPop:(){return Future.value(false);} 。从来不知道没有它可以工作!
  • 或者我们可以使用'() async => false'
  • @MaciejStramski 这实际上是首选方式。它使我免于在我的 IDE 中抑制有关未实现的返回类型声明 (Future) 的警告。
  • 如果您已经使用WillPopScope(),则不再需要barrierDismissible = false
【解决方案2】:

三种方法可以阻止 Android 后退按钮关闭对话框

选项一:

           onWillPop: () {
                          return Future.value(false);
                        },

选项二:

    onWillPop: () async {
                          return false;
                        },

选项三:

 onWillPop: () {}, // This will give surpress warning, try to avoid this one.

【讨论】:

    【解决方案3】:

    由于我的声誉不足以评论已接受的答案,我想为onPressed: () {} 提供其他选择。您可以使用onPressed: () =&gt; null。不会弹出任何警告。

    【讨论】:

      【解决方案4】:

      要实现此行为,您可以覆盖 MaterialPageRoute 中的 hasScopedWillPopCallback getter。

      class CustomMaterialPageRoute extends MaterialPageRoute {
        @protected
        bool get hasScopedWillPopCallback {
          return false;
        }
        CustomMaterialPageRoute({
          @required WidgetBuilder builder,
          RouteSettings settings,
          bool maintainState = true,
          bool fullscreenDialog = false,
        }) : super(
                builder: builder,
                settings: settings,
                maintainState: maintainState,
                fullscreenDialog: fullscreenDialog,
              );
      }
      

      然后在您的路由器中将MaterialPageRoute 替换为CustomMaterialPageRoute。 然后在 iOS 上滑动即可工作,WillPopScope 小部件将工作。

      【讨论】:

      • 这是一个很好的答案,因为它保留了 iOS 上的向后滑动行为!
      【解决方案5】:
       @override
            Widget build(BuildContext context) {
              return WillPopScope(
                onWillPop: _onBackPressed,
                child: Scaffold(
                  
                ),
              );
            }
          
            Future<bool> _onBackPressed() async {
              return await showDialog(
                  context: context, builder: (context) => ExitAppDialogBox());
            }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-10
        • 2020-09-10
        • 2011-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        相关资源
        最近更新 更多