设备在Flutter中的后退按钮控制



我正在开发一个flutter应用程序,有客户列表和"添加"浮动按钮。当我点击添加按钮时,我把它带到一个新的屏幕上添加新客户。在这个屏幕中,当设备返回按钮被按下时,它应该返回到列表屏幕。但是,它将返回到Dashboard。(应用程序流程:仪表盘屏幕(客户)->客户列表界面->添加客户屏幕。我已经尝试过Willscope方法:

My Add Customer Screen:

Widget build(BuildContext context) {
Future<bool> _onBackPressed() async {
return (await showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Are you sure?'),
content: new Text('Do you want to exit Add Customer'),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text('No'),
),
new FlatButton(
onPressed: () => Navigator.of(context).pop(true),
child: new Text('Yes'),
),
],
),
)) ?? false;
}
return new
WillPopScope(
onWillPop: _onBackPressed,
child : new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
backgroundColor: theme_color,
),
body: new SafeArea(
top: false,
bottom: false,
child: new Form(
key: _formKey,
autovalidate: true,
child: new ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: <Widget>[
SizedBox(
height: 20,
),
//Form Elements

new Container(
height: 70,
padding: const EdgeInsets.only(
left: 10.0, right: 10.0, top: 20.0),
child: new RaisedButton(
color: theme_color,
child: const Text(
'Save',
style: TextStyle(color: Colors.white),
),
onPressed: () {
//Send Data to Database
}
},
)), //Save button
],
))),
));
}

使用这个,而不是返回任何东西。

Future<void> _onBackPressed() async {
return (await showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Are you sure?'),
content: new Text('Do you want to exit Add Customer'),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text('No'),
),
new FlatButton(
onPressed: () => Navigator.of(context).pop(true),
child: new Text('Yes'),
),
],
),
));
}

最新更新