nyp*_*ogi 7 dart dropdown flutter
当我选择一个项目时,对话框中的初始值不会改变。这是下拉列表的代码:
void _buildStatusDialog(String documentID) {
String _selectedText = "SDD";
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Status Update"),
content: new DropdownButton<String>(
hint: Text("Status"),
value: _selectedText,
items: <String>['SDD', 'Meeting', 'Home', 'Space']
.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (String val) {
_selectedText = val;
setState(() {
_selectedText = val;
});
},
),
actions: <Widget>[
FlatButton(
child: Text("UPDATE"),
onPressed: () {
.....
},
),
],
);
});
Run Code Online (Sandbox Code Playgroud)
}
如何更新“提示”或显示所选项目?
使用@Jonah Williams 在评论中的提示,我想出了以下工作示例来解决我遇到的类似问题:
import 'package:flutter/material.dart';
class StatusDialog extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return StatusDialogState();
}
}
class StatusDialogState extends State<StatusDialog> {
String _selectedText = "SSD";
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text("Status Update"),
content: new DropdownButton<String>(
hint: Text("Status"),
value: _selectedText,
items: <String>['SDD', 'Meeting', 'Home', 'Space']
.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (String val) {
setState(() {
_selectedText = val;
});
},
),
actions: <Widget>[
FlatButton(
child: Text("UPDATE"),
onPressed: () {
//.....
},
),
],
);
}
}
void _buildStatusDialog(String documentID) {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return StatusDialog();
}
);
}
Run Code Online (Sandbox Code Playgroud)
然后你只需要添加一些逻辑,以获得_selectedText来自StatusDialog-可能使用一个回调。
| 归档时间: |
|
| 查看次数: |
20095 次 |
| 最近记录: |