我正在尝试弹出而不返回数据。 我有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页中,我有一个带文本字段和水平按钮添加的showdialog内容,按 add 后,我需要回到第3页,尝试使用pop可以,但是如果我再按一次添加按钮,它将返回我添加的最后一个数据。
有什么方法可以弹出返回数据? 我尝试使用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中的代码
答案 0 :(得分:0)
我认为,您忘记了按照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();
}
答案 1 :(得分:-1)
你好,我已经知道答案了, 事实证明,解决方案是为我的对话框创建一个新的StatefulWidget子类,并将文本编辑控制器放入其中。