【发布时间】:2021-01-01 16:27:18
【问题描述】:
我正在开发一个应用程序,它只会从用户那里获得 2 个输入,未来可能会得到 3 个。所以我使用带有 2 个输入字段和 2 个按钮的 showDialog。很简单。看起来很像这样: enter image description here 如果它被作为函数调用,它会完美地显示在主屏幕中。但我想在应用程序的另一个屏幕中使用相同的对话框,只需更改其中一个按钮上的文本。由于对话代码很长,我不想重复,我想把它放在一个新的 StatefulWidget 中(从输入字段中检索信息并在之后清除它们)。现在的问题是 我找不到从外部小部件返回对话框的方法。我不知道这是否由于整个异步小部件不兼容而实际上是不可能的,或者我也是愚蠢地想出这个。
这是我的对话框的代码(作为一个函数):
_dialog() async {
await showDialog<String>(
context: context,
child: ButtonBarTheme(
data: ButtonBarThemeData(alignment: MainAxisAlignment.center),
child: AlertDialog(
contentPadding: const EdgeInsets.all(20.0),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: EdgeInsets.only(bottom: 15),
child: new TextField(
// onSubmitted: next,
autofocus: true,
controller: _title,
decoration: InputDecoration(
contentPadding: EdgeInsets.only(bottom: 2),
labelText: 'Title',
hintText: 'Example: New Smartphone',
),
),
),
TextField(
autofocus: true,
controller: _codigo,
decoration: InputDecoration(
contentPadding: EdgeInsets.zero,
labelText: 'Code',
hintText: 'Example: EC20008347607',
),
),
],
),
actions: [
Row(
children: [
Container(
padding: EdgeInsets.only(right: 35, bottom: 5),
child: FlatButton(
color: Colors.cyan[600],
child: const Text('CANCEL'),
onPressed: () {
Navigator.pop(context);
}),
),
Container(
padding: EdgeInsets.only(left: 15, bottom: 5),
child: FlatButton(
color: Colors.cyan[600],
child: const Text('ADD'),
onPressed: () {
setState(() {
_title.text = '';
_code.text = '';
});
Navigator.pop(context);
},
),
),
],
),
],
),
),
);
}
提前感谢您的帮助。我是初学者。
【问题讨论】:
标签: flutter dialog widget textinput stateful