首页 > 解决方案 > 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);
      },
    );
  }
}

下拉按钮屏幕截图 控制台屏幕截图

我坚持了很长一段时间。

谁能告诉我这个问题的原因?或者有解决办法吗?

标签: flutterdartsetstatedropdownbox

解决方案


您正在分配给局部变量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);
    },
  );
}

推荐阅读