Flutter - Change back button from Navigation Bar

这一生的挚爱 提交于 2019-12-24 18:52:36

问题


Good day,

I need to change the command from back button located on Navigation Bar on Android cellphones, like the imagem bellow?

I need to change the button to appear a message, "Do you really want to quit the application?".To confirm the user leave the program.

Anyone can help?

Thanks.


回答1:


Use the WillPopScope widget to handle the back button action, example :

  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"),),
        ),
      );
    }
  } 


来源:https://stackoverflow.com/questions/51766382/flutter-change-back-button-from-navigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!