问题
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