[英]Flutter: cannot update DropdownButton selection UI into an AlertDialog
I want to update the initial DropDownButton display with the selected item value, but for some reason the UI doesn't update when an item is selected.我想使用所选项目值更新初始DropDownButton显示,但由于某种原因,UI在选择项目时UI不会更新。 Actually I have a ListView composed by a Text() and a RisedButton() and when RaisedButton is clicked, an AlertDialog will show.
实际上,我有一个由 Text() 和 RisedButton() 组成的 ListView,当单击 RaisedButton 时,会显示一个 AlertDialog。 Everything is built with StatefulWidget.
一切都是用 StatefulWidget 构建的。
This is my ListView.builder:这是我的 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(): creates UI ListView 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(): This is the part of code from my AlertDialog with DropDownButton. _typeTrue():这是我的带有 DropDownButton 的 AlertDialog 代码的一部分。
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);
});
},
),
],
),
In AlertDialog scaffold state is not work, so you have to use StatefulBuilder which provide it's own state to change state in AlertDialog在 AlertDialog脚手架状态下是不工作的,所以你必须使用StatefulBuilder它提供它自己的状态来改变 AlertDialog 中的状态
_typeTrue(String columnValue, {String value}) {
return showDialog(
context: context,
builder: (context) =>
AlertDialog(
title: Text(columnValue),
content: StatefulBuilder(builder: (BuildContext context, state) {
return Container(
height: 200,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Segno"),
DropdownButton(
hint: new Text("Seleziona un valore"),
items: type.map((String newValue) {
return DropdownMenuItem(
value: newValue,
child: new Text(newValue),
);
}).toList(),
value: selectedValue,
onChanged: (String val) {
state(() {
selectedValue = val;
print(selectedValue);
});
},
)
],
),
]),
);
},
),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.