[英]Flutter: How to change variables through TextFormField in a AlertDialog?
I want to implement a function as following: There is a button named自定义
.我想实现一个 function 如下:有一个名为自定义
的按钮。 In this page, there is a variable named money
.在此页面中,有一个名为money
的变量。 When I click the button, an AlertDialog with a TextFormField inside will occur.当我单击该按钮时,将出现一个带有 TextFormField 的 AlertDialog。 I hope that after inputting a number X into the TextFormField and clicking button ok
to exit the AlertDialog, money
would be changed to X. I have used onSaved
to save the variable, and used _formkey.currentState.save()
, but money
didn't change.我希望在 TextFormField 中输入数字 X 并单击ok
按钮退出 AlertDialog 后, money
将更改为 X。我使用onSaved
保存变量,并使用_formkey.currentState.save()
,但money
没有不改变。 What's wrong with my codes?我的代码有什么问题? Here are my codes:这是我的代码:
void _showMessageDialog() {
//int addMoney = 0;
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
key: _formKey,
title: new Text('INPUT'),
content: TextFormField(
maxLines: 1,
keyboardType: TextInputType.emailAddress,
autofocus: false,
style: TextStyle(fontSize: 15),
decoration: new InputDecoration(
border: InputBorder.none,
hintText: 'input',
),
onSaved: (value) {
money = int.parse(value?.trim() ?? '0') as double;
print(money);
}
),
actions: <Widget>[
new TextButton(
key: _formKey,
child: new Text("ok"),
onPressed: () {
_formKey.currentState?.save();
Navigator.of(context).pop();
},
),
],
);
},
);
}
Here are the codes relative to the button自定义
以下是与自定义
按钮相关的代码
OutlinedButton(
style: OutlinedButton.styleFrom(
side: BorderSide(
width: 1,
color: Colors.blueAccent
)
),
onPressed: () {
// Navigator.of(context).push(
// _showMessageDialog()
// );
_showMessageDialog();
},
child: Text(
"自定义",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w700,
color: Colors.blueAccent
),
),
),
I know maybe I have made a big mistake, but it is my first Flutter project.我知道也许我犯了一个大错误,但这是我的第一个 Flutter 项目。 Thanks for your advices.谢谢你的建议。
I would use ValueNotifier for this.我会为此使用ValueNotifier 。 But first you need to add a controller to your TextFormField
so you can get the text user typed in.但首先您需要将controller添加到您的TextFormField
中,以便您可以让用户输入文本。
//initialize it
final myController = TextEditingController();
TextFormField(
controller: myController, //pass it to the TextFormField
),
TextButton(
child: new Text("ok"),
onPressed: () {
String input = myController.text; //this is how you get the text input
_formKey.currentState?.save();
Navigator.of(context).pop();
},
),
As I said you also need to initialize ValueNotifier and pass it to _showMessageDialog
正如我所说,您还需要初始化 ValueNotifier 并将其传递给_showMessageDialog
ValueNotifier<int> money = ValueNotifier(0);
_showMessageDialog(money); //and pass it to your function
void _showMessageDialog(ValueNotifier<int> money) {
TextButton(
child: new Text("ok"),
onPressed: () {
String input = myController.text; //this is how you get the text input
money.value = int.parse(input); //and then update your money variable
_formKey.currentState?.save();
Navigator.of(context).pop();
},
),
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.