flutter - DropdownButton 不改变值且无法重用
问题描述
我为下拉按钮创建了一个方法来重用代码。每当调用该方法时,按钮都不会更新。但是当我尝试调试时,控制台中的值会更新。即使我在 Stateful 小部件中创建了该方法,但 setState 方法似乎不起作用!
这是代码和几个屏幕截图
class _SGPAState extends State<SGPA> {
List<String> _credits = ['0', '1', '2', '3', '4', '5'];
List<String> _grades = ['O', 'A+', 'A', 'B+', 'B', 'C', 'P', 'F', 'Ab', 'I'];
List<String?> selectedCredit = List.filled(15, '0');
List<String?> selectedGrade = List.filled(15, 'O');
@override
Widget build(BuildContext context) {
return Column(
children: [
SizedBox(
height: 15,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text('SNo', style: kGPATextStyle),
Text('Credits', style: kGPATextStyle),
Text('Grade', style: kGPATextStyle),
],
),
SizedBox(
height: 15,
),
dataRow(index: 0),
dataRow(index: 1),
dataRow(index: 2),
dataRow(index: 3),
dataRow(index: 4),
],
);
}
Row dataRow({required int index}) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
'${index+1}',
style: kGPATextStyle,
),
buildDropdown(selectedValue: selectedCredit[index], selectedList: _credits),
buildDropdown(selectedValue: selectedGrade[index], selectedList: _grades),
],
);
}
DropdownButton<String> buildDropdown(
{required String? selectedValue, required List<String> selectedList}) {
return DropdownButton(
value: selectedValue,
items: selectedList.map((location) {
return DropdownMenuItem(
value: location,
child: Text(
location,
style: kDropdownTextStyle,
),
);
}).toList(),
onChanged: (String? newValue) {
selectedValue = newValue;
setState(() {
selectedValue = newValue;
});
print(selectedValue);
},
);
}
}
我坚持了很长一段时间。
谁能告诉我这个问题的原因?或者有解决办法吗?
解决方案
您正在分配给局部变量selectedValue
,它不会从一个构建持续到另一个构建。因此,如果您希望跨构建保存该值,则必须将其分配给状态类的属性(例如selectedCredit
/ selectedGrade
)。
这是您的问题的可能解决方案:
Row dataRow({required int index}) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
'${index + 1}',
style: kGPATextStyle,
),
buildDropdown(
selectedValue: selectedCredit[index],
onSelected: (v) => setState(() {
selectedCredit[index] = v;
}),
selectedList: _credits,
),
buildDropdown(
selectedValue: selectedGrade[index],
onSelected: (v) => setState(() {
selectedGrade[index] = v;
}),
selectedList: _grades,
),
],
);
}
DropdownButton<String> buildDropdown({
required final String? selectedValue,
required final ValueSetter<String?> onSelected,
required final List<String> selectedList,
}) {
return DropdownButton(
value: selectedValue,
items: selectedList.map((location) {
return DropdownMenuItem(
value: location,
child: Text(
location,
style: kDropdownTextStyle,
),
);
}).toList(),
onChanged: (String? newValue) {
onSelected(newValue);
},
);
}
推荐阅读
- html - 更改输入 HTML 日期格式
- python - 分析恶意软件 APK 时是否应该关闭防病毒软件?
- python - 用于大数据的 Python3 的 Reed-Solomon 代码
- c# - 如何使用rigidbody.moveposition移动玩家,但将玩家移动到他正在寻找的位置并根据该移动玩家?
- excel - =0/1 和 0 之间的差异
- javascript - 为什么提交表单时数据没有了,出现“TypeError: NetworkError when trying to fetch resource”?
- git - 如何在 Git 中将 branchA 的 branchB 合并到 Master 中?
- java - 带有参数和身份验证的 Java Get 请求
- flutter - 如何在颤动中使用custompainter矩形内的位置行?
- jupyter-notebook - 如何在 Jupyter Notebook 中动态更新 Holoviews 绘图布局?