Flutter 尝试在对话框中选择后获取要填充的下拉菜单和日期
嗨,我有一个下拉菜单和一个警报对话框中的日期选择器。这些选择会在控制台上打印出来,这样我就知道它正在工作,但在实际的警报对话框中它们不会改变,除非我关闭并再次打开它。有什么方法可以让我实时更改日期和下拉菜单选项吗 我的下拉代码如下:Flutter 尝试在对话框中选择后获取要填充的下拉菜单和日期,flutter,dart,state,dropdown,Flutter,Dart,State,Dropdown,嗨,我有一个下拉菜单和一个警报对话框中的日期选择器。这些选择会在控制台上打印出来,这样我就知道它正在工作,但在实际的警报对话框中它们不会改变,除非我关闭并再次打开它。有什么方法可以让我实时更改日期和下拉菜单选项吗 我的下拉代码如下: void rightButtonPressed() { setState(() { walkDuration = dependencies.stopwatch.elapsedMilliseconds~/60000; if (dependencies.stop
void rightButtonPressed() {
setState(() {
walkDuration = dependencies.stopwatch.elapsedMilliseconds~/60000;
if (dependencies.stopwatch.isRunning) {
dependencies.stopwatch.stop();
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
actions: <Widget>[
Theme(
child: Button(
child: const Text('Add'),
onPressed: (){
add();
Navigator.of(context).pop();
},
style: Style(
color: Colors.white,
)
),
)
],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8)),
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Form(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(5.0),
child: Column(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
DropdownButton(
value: numberTimes,
items: [
DropdownMenuItem(
child: Text("0", style: TextStyle(fontSize:15.0)),
value: 0,
),
DropdownMenuItem(
child: Text("1", style: TextStyle(fontSize:15.0)),
value: 1,
),
DropdownMenuItem(
child: Text("2", style: TextStyle(fontSize:15.0)),
value: 2,
),
DropdownMenuItem(
child: Text("3", style: TextStyle(fontSize:15.0)),
value: 3,
),
DropdownMenuItem(
child: Text("4", style: TextStyle(fontSize:15.0)),
value: 4,
),
DropdownMenuItem(
child: Text("5", style: TextStyle(fontSize:15.0)),
value: 5,
),
DropdownMenuItem(
child: Text("5+", style: TextStyle(fontSize:15.0)),
value: 6,
),
],
onChanged:(value) {
setState((){
numberTimes = value;
print(numberTimes);
});
}
),
],
)
),
void rightbutonpressed(){
设置状态(){
walkDuration=dependencies.stopwatch.elapsedMilliseconds~/60000;
if(依赖项.秒表.正在运行){
dependencies.stopwatch.stop();
显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
返回警报对话框(
行动:[
主题(
孩子:按钮(
子项:常量文本('Add'),
已按下:(){
添加();
Navigator.of(context.pop();
},
风格:风格(
颜色:颜色,白色,
)
),
)
],
形状:圆形矩形边框(
边界半径:边界半径。圆形(8)),
内容:堆栈(
溢出:溢出。可见,
儿童:[
形式(
子:SingleChildScrollView(
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
填充物(
填充:所有边缘设置(5.0),
子:列(
//mainAxisAlignment:mainAxisAlignment.space,
儿童:[
下拉按钮(
值:numberTimes,
项目:[
下拉菜单项(
子项:文本(“0”,样式:TextStyle(fontSize:15.0)),
值:0,
),
下拉菜单项(
子项:文本(“1”,样式:TextStyle(fontSize:15.0)),
价值:1,
),
下拉菜单项(
子项:文本(“2”,样式:TextStyle(fontSize:15.0)),
价值:2,
),
下拉菜单项(
子项:文本(“3”,样式:TextStyle(fontSize:15.0)),
价值:3,
),
下拉菜单项(
子项:文本(“4”,样式:TextStyle(fontSize:15.0)),
价值:4,
),
下拉菜单项(
子项:文本(“5”,样式:TextStyle(fontSize:15.0)),
数值:5,
),
下拉菜单项(
子项:文本(“5+”,样式:TextStyle(fontSize:15.0)),
价值:6,
),
],
一旦更改:(值){
设置状态(){
numberTimes=值;
打印(次数);
});
}
),
],
)
),
使用statefullBuilder扭曲对话框
并使用setState
例如:
showDialog(
context: context,
builder: (context) {
String contentText = "Content of Dialog";
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("Title of Dialog"),
content: Text(contentText),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text("Cancel"),
),
FlatButton(
onPressed: () {
setState(() {
contentText = "Changed Content of Dialog";
});
},
child: Text("Change"),
),
],
);
},
);
},
);
showDialog(
上下文:上下文,
生成器:(上下文){
String contentText=“对话框的内容”;
返回状态生成器(
生成器:(上下文,设置状态){
返回警报对话框(
标题:文本(“对话框标题”),
内容:文本(contentText),
行动:[
扁平按钮(
onPressed:()=>Navigator.pop(上下文),
子项:文本(“取消”),
),
扁平按钮(
已按下:(){
设置状态(){
contentText=“更改了对话框的内容”;
});
},
子项:文本(“更改”),
),
],
);
},
);
},
);
请将下拉按钮更改为DropdownButtonFormField
。我已经编辑了你的代码,你在那里写了多少。因为你没有写完整的代码
这是我编辑过的代码
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8)),
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Form(
child: DropdownButtonFormField<int>(
value: numberTimes,
items: [
DropdownMenuItem(
child: Text("0",
style: TextStyle(
color: Colors.black,
fontSize: 15.0)),
value: 0,
),
DropdownMenuItem(
child: Text("1",
style: TextStyle(fontSize: 15.0)),
value: 1,
),
DropdownMenuItem(
child: Text("2",
style: TextStyle(fontSize: 15.0)),
value: 2,
),
DropdownMenuItem(
child: Text("3",
style: TextStyle(fontSize: 15.0)),
value: 3,
),
DropdownMenuItem(
child: Text("4",
style: TextStyle(fontSize: 15.0)),
value: 4,
),
DropdownMenuItem(
child: Text("5",
style: TextStyle(fontSize: 15.0)),
value: 5,
),
DropdownMenuItem(
child: Text("5+",
style: TextStyle(fontSize: 15.0)),
value: 6,
),
],
onChanged: (int value) {
setState(() => numberTimes = value);
}))
]));
})
showDialog(
上下文:上下文,
生成器:(BuildContext上下文){
返回警报对话框(
形状:圆形矩形边框(
边界半径:边界半径。圆形(8)),
内容:堆栈(
溢出:溢出。可见,
儿童:[
形式(
子项:DropdownButtonFormField(
值:numberTimes,
项目:[
下拉菜单项(
子项:文本(“0”,
样式:TextStyle(
颜色:颜色,黑色,
字体大小:15.0),
值:0,
),
下拉菜单项(
子项:文本(“1”,
void rightButtonPressed() {
setState(() {
walkDuration = dependencies.stopwatch.elapsedMilliseconds~/60000;
if (dependencies.stopwatch.isRunning) {
dependencies.stopwatch.stop();
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
actions: <Widget>[
Theme(
child: Button(
child: const Text('Add'),
onPressed: (){
add();
Navigator.of(context).pop();
},
style: Style(
color: Colors.white,
)
),
)
],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8)),
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Form(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(5.0),
child: Column(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
DropdownButtonFormField<int>(
value: numberTimes,
items: [
DropdownMenuItem(
child: Text("0", style: TextStyle(fontSize:15.0)),
value: 0,
),
DropdownMenuItem(
child: Text("1", style: TextStyle(fontSize:15.0)),
value: 1,
),
DropdownMenuItem(
child: Text("2", style: TextStyle(fontSize:15.0)),
value: 2,
),
DropdownMenuItem(
child: Text("3", style: TextStyle(fontSize:15.0)),
value: 3,
),
DropdownMenuItem(
child: Text("4", style: TextStyle(fontSize:15.0)),
value: 4,
),
DropdownMenuItem(
child: Text("5", style: TextStyle(fontSize:15.0)),
value: 5,
),
DropdownMenuItem(
child: Text("5+", style: TextStyle(fontSize:15.0)),
value: 6,
),
],
onChanged:(value) {
setState((){
numberTimes = value;
print(numberTimes);
});
}
),
],
)
),