In Flutter, I have a table where I am trying to get a list in dropdown in TableCell. I have created a gesturedetector to showDailog with DropdownButtonForm to show list and selection. but selected value is not showing in TableCell. What i found is that in below code, in Container I am not getting value of selectedVal as true.
rows.add(
TableRow(
children: [
TableCell(
child: GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
title: Text('Select Item'),
content: DropdownButtonFormField<String>(
value: selectedValue != '' ? selectedValue : null,
onChanged: (value) {
setState(() {
selectedValue = value!;
selectedVal = true;
});
},
items: itemList.map((item) {
return DropdownMenuItem<String>(
value: item,
child: Text(item),
);
}).toList(),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
setState(() {
selectedItem[rows.length - 1] = selectedValue;
});
print('selectedValue: $selectedValue');
print('selectedVal: $selectedVal');
},
child: Text('Ok'),
),
],
);
},
);
},
);
},
child: Container(
color: Colors.lightBlue,
child: Builder(
builder: (BuildContext context) {
return Column(
children: [
Text(
selectedVal ? selectedValue : 'Select Invoice',
style: TextStyle(
color: Colors.white,
),
),
],
);
},
),
),
),
),
2
Answers
The issue you are encountering is likely due to the fact that you are not updating the TableCell content when the selectedValue changes. The TableCell widget is not being rebuilt when the value of selectedValue changes, which is why the updated value is not reflected in the TableCell.
You can wrap your TableCell widget with a StatefulWidget or StatefulBuilder maybe this will help you.
),
);
It is an example code. By doing it in this way, you can achieve the dropdown selection in a
AlertDialog
. Feel free to provide acknowledgement & ask anything in the comment section.