RenderFlex 子项具有非零弹性,但传入的宽度约束在 flutter 中是无限的

RenderFlex children have non-zero flex but incoming width constraints are unbounded in flutter

我 运行 使用 DropdownButton 时出错并删除了其中我不使用 selectedItemBuilder 的方法并因此收到错误,因为它说错误是由于 GFCheckboxListTile 小部件.但是我不明白如何解决这个错误,如果你知道 - 请告诉我,我将不胜感激。

代码

class DropdownWidget extends StatefulWidget {
  List<String> items;
  SvgPicture? icon;
  double width;

  DropdownWidget({
    Key? key,
    required this.items,
    required this.icon,
    required this.width,
  }) : super(key: key);

  @override
  State<DropdownWidget> createState() => _DropdownWidgetState();
}

class _DropdownWidgetState extends State<DropdownWidget> {
  String? selectedValue;
  bool selected = false;

  final List _selectedTitles = [];
  final List _selectedTitlesIndex = [];

  final GFCheckboxType type = GFCheckboxType.basic;

  @override
  void initState() {
    super.initState();
    if (widget.items.isNotEmpty) {
      _selectedTitles.add(widget.items[1]);
    }
  }

  void _onItemSelect(bool selected, int index) {
    if (selected == true) {
      setState(() {
        _selectedTitles.add(widget.items[index]);
        _selectedTitlesIndex.add(index);
      });
    } else {
      setState(() {
        _selectedTitles.remove(widget.items[index]);
        _selectedTitlesIndex.remove(index);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: widget.width,
      child: DropdownButtonHideUnderline(
        child: DropdownButton2(
          items: List.generate(
            widget.items.length,
            (index) => DropdownMenuItem<String>(
              value: widget.items[index],
              child: Container(
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: constants.Colors.white.withOpacity(0.1),
                      width: 1,
                    ),
                  ),
                ),
                child: StatefulBuilder(
                  builder: (context, setStateSB) => Expanded(
                    child: GFCheckboxListTile(
                      value: _selectedTitles.contains(widget.items[index]),
                      onChanged: (bool selected) {
                        _onItemSelect(selected, index);
                        setStateSB(() {});
                      },
                      selected: selected,
                      title: Text(
                        widget.items[index],
                        style: constants.Styles.smallTextStyleWhite,
                      ),
                      padding: const EdgeInsets.only(top: 12, bottom: 13),
                      margin: const EdgeInsets.only(right: 0, left: 0),
                      size: 22,
                      activeBgColor: constants.Colors.greyCheckbox,
                      activeBorderColor: constants.Colors.greyCheckbox,
                      inactiveBgColor: constants.Colors.greyCheckbox,
                      activeIcon:
                          SvgPicture.asset(constants.Assets.checkboxIcon),
                      inactiveBorderColor: constants.Colors.greyXMiddle,
                      type: type,
                    ),
                  ),
                ),
              ),
            ),
          ),
          hint: _selectedTitles.length > 1
              ? const Text('Selecte EV',
                  style: constants.Styles.bigBookTextStyleWhite)
              : Text(_selectedTitles.join().toString(),
                  style: constants.Styles.bigBookTextStyleWhite),
          value: selectedValue,
          onChanged: (value) {
            setState(() {
              selectedValue = value as String;
            });
          },
          icon: SvgPicture.asset(constants.Assets.arrowDropdown),
          iconSize: 21,
          buttonHeight: 27,
          itemHeight: 47,
          dropdownMaxHeight: 191,
          dropdownWidth: 160,
          dropdownDecoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            border: Border.all(
              color: constants.Colors.purpleMain,
            ),
            color: constants.Colors.greyDark,
          ),
        ),
      ),
    );
  }
}

Exception caught by rendering library ═════════════════════════════════ RenderFlex children have non-zero flex but incoming width constraints are unbounded. The relevant error-causing widget was GFCheckboxListTile

您可以将 Expanded 替换为 SizedBox.shrink

child: StatefulBuilder(
  builder: (context, setStateSB) => SizedBox.shrink(
    child: GFCheckboxListTile(

更多关于 SizedBox