DropdownButton 不改变值且无法重复使用
DropdownButton doesn't change value and unable to be reused
我为下拉按钮创建了一个方法来重用代码。每当调用该方法时,按钮都不会更新。但是当我尝试调试时,控制台中的值会更新。即使我在有状态小部件中创建了方法,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);
},
);
}
}
Dropdown buttons screenshot
Console screenshot
我坚持了很长一段时间。
谁能告诉我这个问题的原因?或者有解决办法吗?
您正在分配给局部变量 selectedValue
,该变量不会从一个构建持续到另一个构建。因此,如果您希望跨构建保存值,则必须将其分配给状态 class 上的属性(如 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);
},
);
}
我为下拉按钮创建了一个方法来重用代码。每当调用该方法时,按钮都不会更新。但是当我尝试调试时,控制台中的值会更新。即使我在有状态小部件中创建了方法,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);
},
);
}
}
Dropdown buttons screenshot Console screenshot
我坚持了很长一段时间。
谁能告诉我这个问题的原因?或者有解决办法吗?
您正在分配给局部变量 selectedValue
,该变量不会从一个构建持续到另一个构建。因此,如果您希望跨构建保存值,则必须将其分配给状态 class 上的属性(如 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);
},
);
}