【发布时间】:2020-04-04 16:06:13
【问题描述】:
我想用所选项目值更新初始DropDownButton显示屏,但由于某种原因,UI在选择项目时不会更新。 实际上,我有一个由 Text() 和 RisedButton() 组成的 ListView,当单击 RaisedButton 时,会显示一个 AlertDialog。 一切都是用 StatefulWidget 构建的。
这是我的 ListView.builder:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_name),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return Container(
child: _getCard(index),
);
},
itemCount: threshs.length,
),
),
],
),
);
}
getCard(): 创建 UI ListView
_getCard(int index) {
Thresh thresh = threshs[index];
return Card(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
thresh.column_value,
style: TextStyle(fontSize: 25),
),
RaisedButton(
child: Text("Imposta"),
onPressed: () {
(thresh.type_text)
? _typeTrue(thresh.column_value, value: thresh.thresh_min)
: _typeFalse(thresh.id, thresh.column_value,
threshMin: thresh.thresh_min,
threshMax: thresh.thresh_max);
},
),
],
),
);
}
_typeTrue():这是我的 AlertDialog 中带有 DropDownButton 的部分代码。
var selectedValue; // display selected item
static final type = ["!=", "="]; // dropdown items
_typeTrue(String columnValue, {String value}) {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(columnValue),
content: Container(
height: 200,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Segno"),
DropdownButton(
hint: new Text("Seleziona un valore"),
value: selectedValue,
items: type.map((String newValue) {
return DropdownMenuItem(
value: newValue,
child: new Text(newValue),
);
}).toList(),
onChanged: (String val) {
// update value
setState(() {
selectedValue = val;
print(selectedValue);
});
},
),
],
),
【问题讨论】: