【发布时间】:2020-09-15 07:33:09
【问题描述】:
您好,我在警报对话框中有一个下拉菜单和一个日期选择器。选择打印在控制台 onChange 上,所以我知道它正在工作,但它们不会在实际的警报对话框中改变,除非我关闭并再次重新打开它。有什么方法可以让我实时更改日期和下拉菜单选择??
我的下拉代码如下:
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);
});
}
),
],
)
),
【问题讨论】:
-
我找到了正确的答案。如果其他人遇到类似问题,请查看以下链接:stackoverflow.com/questions/51962272/…
-
请查看我发布的解决方案。
标签: flutter dart state dropdown