gpt4 book ai didi

flutter - 弹出没有数据/推送到当前屏幕

转载 作者:行者123 更新时间:2023-12-03 03:59:49 26 4
gpt4 key购买 nike

我试图弹出而没有返回数据。
我有1个屏幕,其中有4个Bottomnavigator,每个按钮具有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页中,我有一个带文本字段和flatbutton的showdialog对话框 添加,在我按 添加之后,我需要返回到第3页,我尝试使用pop这是可行的,但是如果我再次按 添加按钮它将返回我添加的最后一个数据。

有什么方法可以弹出返回数据?
我尝试使用Push / PushNamed,但它总是返回到第一个屏幕
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的代码

最佳答案

我认为,您忘记按照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();
}

关于flutter - 弹出没有数据/推送到当前屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58110836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com