我希望文本字段在选择时显示“A”/“B”/等,但是它只返回索引“0”/“1”/“2”/...,为什么???
完整的小部件代码如下:
更新:
int selectedValue;
Future showPicker() async {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return CupertinoPicker(
backgroundColor: Colors.white,
scrollController: FixedExtentScrollController(initialItem: 3),
onSelectedItemChanged: (value) {
setState(() {
if (value != null) {
selectedValue = listTextValues[value];
regionController.value = TextEditingValue(text: selectedValue.toString());
}
});
},
itemExtent: 32.0,
children: const listTextValues = [
Text('Eastern'),
Text('South'),
Text('West'),
Text('North'),
Text('Island'),
],
);
}
);
}
下面的代码是可行的,只是想知道如何简化代码,因为我正在使用 2 个列表,一个用于选择器,一个用于映射索引
解决方案:
int selectedValue;
final List<String> listTextValues = ['Eastern', 'South', 'West', 'North', 'Island'];
Future showPicker() async {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return CupertinoPicker(
backgroundColor: Colors.white,
scrollController: FixedExtentScrollController(initialItem: 3),
onSelectedItemChanged: (value) {
setState(() {
if (value != null) {
selectedValue = value;
regionController.value = TextEditingValue(text: listTextValues[selectedValue].toString());
}
});
},
itemExtent: 32.0,
children: const [
Text('Eastern'),
Text('South'),
Text('West'),
Text('North'),
Text('Island'),
],
);
}
);
}