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. Actually I have a ListView composed by a Text() and a RisedButton() and when RaisedButton is clicked, an AlertDialog will show. Everything is built with StatefulWidget.
This is my 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(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.
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
_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);
});
},
)
],
),
]),
);
},
),
),
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.