【问题标题】:Device Back button control in FlutterFlutter 中的设备返回按钮控制
【发布时间】:2021-05-02 05:23:25
【问题描述】:

我正在开发一个具有客户列表和“添加”浮动按钮的颤振应用程序。当我单击添加按钮时,我将其带到一个新屏幕以添加新客户。在此屏幕中,当按下设备返回按钮时,它应该返回列表屏幕。但是,相反,它会回到仪表板。(应用程序流程:仪表板屏幕(客户)-> 客户列表屏幕-> 添加客户屏幕。 我已经尝试过 Willscope 方法:

我的添加客户屏幕:

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

【问题讨论】:

  • 这转到仪表板意味着您的导航器堆栈中没有列表页面
  • 从您的构建方法中删除该方法。
  • @SWAGAssassinYT 我使用 Navigator.push() 从仪表板导航到客户列表,也从客户列表导航到添加客户。为了检查堆栈,我在添加客户屏幕中添加了一个按钮,并在 onPressed 中调用了 Navigator.pop()。屏幕移至列表。所以,我猜,列表就在堆栈中

标签: android flutter dart back-button


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2022-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2018-08-27
    • 2020-10-10
    • 2014-01-21
    • 2022-12-12
    相关资源
    最近更新 更多