【问题标题】:Flutter - Change back button from Navigation BarFlutter - 从导航栏更改后退按钮
【发布时间】:2019-01-16 21:09:16
【问题描述】:

早安,

我需要从位于 Android 手机导航栏上的后退按钮更改命令,如下图所示?

我需要更改按钮以显示一条消息,"Do you really want to quit the application?"。确认用户离开程序。

有人可以帮忙吗?

谢谢。

【问题讨论】:

    标签: uinavigationcontroller dart flutter navigationbar


    【解决方案1】:

    使用WillPopScope 小部件来处理后退按钮操作,例如:

      class TestingWidget extends StatefulWidget {
    
        @override
        TestingWidgetState createState() {
          return new TestingWidgetState();
        }
      }
    
      class TestingWidgetState extends State<TestingWidget> {
        Future<bool> _onBackPressed(){
          final alertDialog = AlertDialog(
            content: Text("Do you really want to quit the application?"),
            actions: <Widget>[
              FlatButton(
                child: Text('Yes'),
                onPressed: () => Navigator.of(context).pop(),
              ),
              FlatButton(
                child: Text('No'),
                onPressed: () => Navigator.of(context).pop(),
              )
            ],
          );
          showDialog(
              barrierDismissible: false,
              context: context,
              builder: (context) => alertDialog);
        }
    
        @override
        Widget build(BuildContext context) {
          return WillPopScope(
            onWillPop: _onBackPressed,
            child: Scaffold(
              appBar: AppBar(),
              body: Center(child: Text("Hello world"),),
            ),
          );
        }
      } 
    

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 2014-04-30
      • 1970-01-01
      相关资源
      最近更新 更多