【问题标题】:Pop without data / push to the current screen无数据弹出/推送到当前屏幕
【发布时间】:2019-09-26 06:34:16
【问题描述】:

我正在尝试弹出而不返回数据。 我有 1 个屏幕,有 4 个底部导航器,每个按钮有 4 个布局

      LayoutType.table: (_) => Page1(
          layoutGroup: _layoutGroup, onLayoutToggle: _onLayoutGroupToggle),
      LayoutType.favorite: (_) => Page2(
          layoutGroup: _layoutGroup, onLayoutToggle: _onLayoutGroupToggle),
      LayoutType.menu: (_) => Page3(
          layoutGroup: _layoutGroup, onLayoutToggle: _onLayoutGroupToggle),
      LayoutType.custom: (_) => Page4(
          layoutGroup: _layoutGroup, onLayoutToggle: _onLayoutGroupToggle),
    }[_layoutSelection](context);

在第 3 页,我有一个带有文本字段和平面按钮 add 的 showdialog 内容,按 add 后我需要返回第 3 页,我尝试使用 pop它可以工作,但如果我再次按下 add 按钮,它将返回我添加的最后一个数据。

有没有什么方法可以在没有返回数据的情况下弹出? 我尝试使用 Push/PushNamed 但它总是返回到第一个屏幕 Page1

class MenuPage extends StatefulWidget implements HasLayoutGroup {
  MenuPage({Key key, this.layoutGroup, this.onLayoutToggle}) : super(key: key);
  final LayoutGroup layoutGroup;
  final VoidCallback onLayoutToggle;

  @override
  _MenuPageState createState() => _MenuPageState();
}

class _MenuPageState extends State<MenuPage> {
  double _width;
  double _height;
  String menuName;
  String menuPrice;
  String menuCategory;
  String selectedCategory;
  final formatPrice = NumberFormat.simpleCurrency();
  var menu;
  crudMedthods crudObj = new crudMedthods();
  List _categorymenu = ["Food", "Beverage", "Liquor", "Electricity", "Others"];
  final _textEditingMenu = TextEditingController();
  final _textEditingPrice = TextEditingController();
  List<DropdownMenuItem<String>> _dropDownMenuItems;
  String _currentCategory;

  List<DropdownMenuItem<String>> getDropDownMenuItems() {
    List<DropdownMenuItem<String>> items = List();
    for (String catmenu in _categorymenu) {
      items.add(DropdownMenuItem(value: catmenu, child: Text(catmenu)));
    }
    return items;
  }

  @override
  void dispose() {

    _textEditingMenu.dispose();
    _textEditingPrice.dispose();
    super.dispose();
  }

  @override
  void initState() {
    _dropDownMenuItems = getDropDownMenuItems();
    _currentCategory = _dropDownMenuItems[0].value;
    crudObj.getDataMenu().then((results) {
      setState(() {
        menu = results;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _width = MediaQuery.of(context).size.width;
    _height = MediaQuery.of(context).size.height;
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            width: _width,
            height: 70,
            child: Card(
              elevation: 2,
              color: Colors.amber[300],
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child: Row(
                children: <Widget>[
                  Container(
                    alignment: Alignment.topLeft,
                    margin: EdgeInsets.all(10),
                    child: IconButton(
                      icon: Icon(FontAwesomeIcons.search),
                      onPressed: () {},
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(295),
                  ),
                  Container(
                    alignment: Alignment.topRight,
                    margin: EdgeInsets.all(10),
                    child: IconButton(
                      icon: Icon(FontAwesomeIcons.plus),
                      onPressed: () {
                        addMenu(context);
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
          Container(
            width: _width,
            height: 584,
            child: Card(
              elevation: 2,
              color: Colors.lightBlueAccent[100],
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child: Container(child: _menuList()),
            ),
          ),
        ],
      ),
    );
  }

  // void changedDropDownItem(String selectedCategory) {
  //   setState(() {
  //     _currentCategory = selectedCategory;
  //   });
  // }

  Future<bool> addMenu(BuildContext context) async {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Add Menu', style: TextStyle(fontSize: 15.0)),
            content: Container(
              height: 300,
              width: 300,
              child: Column(
                children: <Widget>[
                  TextField(
                    controller: _textEditingMenu,
                    textInputAction: TextInputAction.next,
                    decoration: InputDecoration(hintText: 'Enter Menu Name'),
                    onChanged: (value) {
                      this.menuName = value;
                    },
                  ),
                  SizedBox(height: 5.0),
                  TextField(
                    controller: _textEditingPrice,
                    keyboardType: TextInputType.number,
                    decoration: InputDecoration(hintText: 'Enter Price'),
                    onChanged: (value) {
                      this.menuPrice = value;
                    },
                  ),
                  SizedBox(height: 5.0),
                  DropdownButton(
                    isExpanded: true,
                    value: _currentCategory,
                    items: _dropDownMenuItems,
                    onChanged: (String value) {
                      setState(() {
                        _currentCategory = selectedCategory;
                      });
                      menuCategory = value;
                    },
                  ),
                  SizedBox(height: 5.0),
                  Row(
                    children: <Widget>[
                      FlatButton(
                        child: Text('Add'),
                        textColor: Colors.blue,
                        onPressed: () {
                          if (!UtilsImporter()
                              .commanUtils
                              .validateMenuName(menuName)) {
                            UtilsImporter().commanUtils.showToast(
                                UtilsImporter().stringUtils.retrunMenuName,
                                context);
                          } else if (!UtilsImporter()
                              .commanUtils
                              .validateGuestPax(menuPrice)) {
                            UtilsImporter().commanUtils.showToast(
                                UtilsImporter().stringUtils.returnMenuPrice,
                                context);
                          } else if (!UtilsImporter()
                              .commanUtils
                              .validateMenuCategory(menuCategory)) {
                            UtilsImporter().commanUtils.showToast(
                                UtilsImporter().stringUtils.returnMenuCat,
                                context);
                          } else {
                            crudObj.addMenu({
                              'menuName': this.menuName,
                              'menuPrice': this.menuPrice,
                              'menuCategory': this.menuCategory,
                            }).then((result) {
                              // dialogTrigger(context);
                            }).catchError((e) {
                              print(e);
                            });
                            Navigator.of(context).pop();
                            // Navigator.of(context).pop();
                            // Navigator.of(context)
                            //     .pushReplacementNamed('/Dashboard');
                          }
                        },
                      ),
                      Padding(
                        padding: EdgeInsets.all(60),
                      ),
                      FlatButton(
                        child: Text('Return'),
                        textColor: Colors.blue,
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      )
                    ],
                  ),
                ],
              ),
            ),
          );
        });
  }
}

这是我的布局 Page3 中的代码

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    我认为,您忘记按照this link 中的建议处理您的 TextField 状态。在这种情况下,您必须添加

        final myController1 = TextEditingController();
        final myController2 = TextEditingController();
    
         @override
          void dispose() {
            // Clean up the controller when the widget is removed from the
            // widget tree.
            myController1.dispose();
            myController2.dispose();
            super.dispose();
          }
    

    【讨论】:

    • 我有 2 个文本字段和 1 个下拉按钮,如何处理?
    • 我已经在我的 showdialog 文本字段中尝试了它现在可以使用的代码,它显示了我的最后一个数据,但没有清空它,
    • 请分享您的代码。不查看代码,几乎不可能找到解决方案。
    • 或者我可以推回屏幕 1 布局 Page3 吗?有可能吗?
    【解决方案2】:

    你好,我已经知道答案了, 事实证明,解决方案是为我的对话框创建一个新的 StatefulWidget 子类,并将文本编辑控制器放入其中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多