首页 > 解决方案 > 有没有办法在 Wrap 小部件中使用 Expanded 小部件

问题描述

我正在尝试使扭曲小部件内的按钮大小动态化,她的代码如下

             Wrap(
              spacing: 10,
              alignment: WrapAlignment.spaceBetween,
              children: [
                for (InterestsClass item in interests)
                  InterestsButton(
                    text: item.interestsName,
                  )
              ],
            ),
InterestsButton is just an elevated button that is reusable, but if I add expanded as show below its Incorrect use of ParentDataWidget, since there is no flex parent

      Widget build(BuildContext context) {
    return Expanded(
      child: ElevatedButton(
        onPressed: () {
          setState(() {
            _flag = !_flag;
          });
        },
        child: Text(
          widget.text,
          style: TextStyle(color: _flag ? kWhite : k34, fontSize: 13),
        ),
        style: ElevatedButton.styleFrom(
            padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
            shape: StadiumBorder(),
            primary: _flag ? kBlue : Color(0xffFAFAFA)),
      ),
    );
  }

这张图片显示了我正在努力实现的更多目标

标签: flutter

解决方案


I think u can use an alternative way Chip/ ChoiceChip. These widget is available in Flutter very useful for creating size dynamic button with change style when tap. See more here https://api.flutter.dev/flutter/material/Chip-class.html

https://medium.com/flutterdevs/chip-widgets-in-flutter-7a2d3d34597c


推荐阅读