[英]Flutter DropDownButton not showing once selected
I have the following code我有以下代码
class InteriorDropdownButton extends StatefulWidget {
const InteriorDropdownButton({Key? key}) : super(key: key);
@override
_InteriorDropdownButtonState createState() => _InteriorDropdownButtonState();
}
class _InteriorDropdownButtonState extends State<InteriorDropdownButton> {
final interiorTypeList = [
'Cuero',
'Foamizada',
'Vinilo',
'Tela',
'Microfibra'
];
String? selectedInterior;
DropdownButton<String> androidDropdown() {
List<DropdownMenuItem<String>> dropdownItems = [];
for (String interiorType in interiorTypeList) {
var newItem = DropdownMenuItem(
child: Text(
interiorType,
style: const TextStyle(
fontWeight: FontWeight.w400,
color: Colors.black,
fontSize: 20,
),
),
value: interiorType,
);
dropdownItems.add(newItem);
}
return DropdownButton<String>(
items: dropdownItems,
icon: const Icon(Icons.expand_more_outlined),
iconSize: MediaQuery.of(context).size.height * 0.04,
isExpanded: true,
hint: const Text('Tapiceria'),
style: TextStyle(
fontWeight: FontWeight.w400,
color: Colors.black,
fontSize: MediaQuery.of(context).size.height * .025,
),
onChanged: (value) {
setState(() {
selectedInterior = value;
});
print('Hola ${selectedInterior}');
print('Hello ${value}');
});
}
CupertinoPicker iOSPicker() {
List<Text> pickerItems = [];
for (String interiorType in interiorTypeList) {
pickerItems.add(Text(interiorType));
}
return CupertinoPicker(
diameterRatio: 1,
itemExtent: 32.0,
onSelectedItemChanged: (selectedIndex) {
setState(() {
selectedInterior = interiorTypeList[selectedIndex];
});
},
children: pickerItems,
);
}
@override
Widget build(BuildContext context) {
return Platform.isIOS ? iOSPicker() : androidDropdown();
}
}
The androidDropdown() is not working properly, once I selected an item, it doesn't stay in the button, instead, it shows the hint String. androidDropdown() 无法正常工作,一旦我选择了一个项目,它就不会停留在按钮中,而是显示提示字符串。
In the console the print result is:在控制台中打印结果是:
print('Hola ${selectedInterior}'); print('Hola ${selectedInterior}'); = Hola null, print('Hello ${value}');
= 你好 null,打印('你好 ${value}'); Hello Cuero(selectedItem)
你好库罗(selectedItem)
Can someone help me on this?有人可以帮我吗?
The DropdownButton
needs to have access to the selected item, in this case, the selectedInterior
variable. DropdownButton
需要有权访问所选项目,在本例中为selectedInterior
变量。
You do this by passing it to the DropdownButton
widget as a value
property like this:为此,您可以将其作为
value
属性传递给DropdownButton
小部件,如下所示:
value: selectedInterior,
Update your androidDropdown
method to this:将您的
androidDropdown
方法更新为:
DropdownButton<String> androidDropdown() {
// Other code
return DropdownButton<String>(
items: dropdownItems,
icon: const Icon(Icons.expand_more_outlined),
iconSize: MediaQuery.of(context).size.height * 0.04,
value: selectedInterior, //Add this
isExpanded: true,
hint: const Text('Tapiceria'),
style: TextStyle(
fontWeight: FontWeight.w400,
color: Colors.black,
fontSize: MediaQuery.of(context).size.height * .025,
),
onChanged: (value) {
setState(() {
selectedInterior = value;
});
print('Hola ${selectedInterior}');
print('Hello ${value}');
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.