gpt4 book ai didi

dart - 如何在 flutter 中将数据从警报对话框传递到同一页面

转载 作者:IT王子 更新时间:2023-10-29 07:04:13 28 4
gpt4 key购买 nike

我想从警报对话框传递数据。警报对话框包含文本字段,因此无论用户在文本字段上键入什么,文本都应该传递到主页(屏幕)。下面是警报对话框的代码

    Padding(
padding: const EdgeInsets.only(left: 42.0),
child: Align(
alignment: Alignment.topCenter,
child: RaisedButton(onPressed: (){
_showDialog();
},
),
),

Padding(
padding: const EdgeInsets.only(top: 50.0),
child: new Text('// Displays text'););

void _showDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: TextField(
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'Enter the number'
),
)
,
actions: <Widget>[
// usually buttons at the bottom of the dialog
Row(
children: <Widget>[
new FlatButton(
child: new Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
},
),
new FlatButton(onPressed: (){

}, child: new Text("OK"))
],

),
],
);
},
);
}

最佳答案

编辑新解决方案:

// write this in your main page
String onMainPageText;

_showdialog 方法 Text(onMainPageText) 中点击 okey 后,您可以在您的主页上这样显示!>

使用以下代码更改您的 _showDialog 方法。

  void _showDialog() {
String dialogText;
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: TextField(
onChanged: (String textTyped) {
setState(() {
dialogText = textTyped;
});
},
keyboardType: TextInputType.number,
decoration: InputDecoration(hintText: 'Enter the number'),
),
actions: <Widget>[
// usually buttons at the bottom of the dialog
Row(
children: <Widget>[
new FlatButton(
child: new Text("Cancel"),
onPressed: () {
setState(() {
onMainPageText = '';
});
Navigator.of(context).pop();
},
),
new FlatButton(
onPressed: () {
setState(() {
onMainPageText = dialogText;
});
Navigator.of(context).pop();
},
child: new Text("OK"))
],
),
],
);
},
);
}

旧答案:

创建一个全局 TextEditingController 将处理您的问题您可以使用 textEditingConroller.text 访问文本字段文本

不要忘记在类中定义 textEditingController

class YourMainPageState extends State<YourMainPage>{
TextEditingController textEditingController = new TextEditingController();

}
  void _showDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: TextField(
controller: textEditingController,
keyboardType: TextInputType.number,
decoration: InputDecoration(hintText: 'Enter the number'),
),
actions: <Widget>[
// usually buttons at the bottom of the dialog
Row(
children: <Widget>[
new FlatButton(
child: new Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
},
),
new FlatButton(onPressed: () {print(textEditingController.text);}, child: new Text("OK"))
],
),
],
);
},
);
}

您可以使用该代码显示键入的文本:

Padding(
padding: const EdgeInsets.only(top: 50.0),
child: new Text(texEditingController.text););

关于dart - 如何在 flutter 中将数据从警报对话框传递到同一页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54933278/

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