列出具有不同宽度的小部件(芯片),放在一个容器中。

huangapple go评论67阅读模式
英文:

Listing widgets (chips) that have different width, in a container

问题

我正在尝试创建一个包含类似下面图片所示的元素列表的容器。

我使用了GridView.builder来使它具有响应性,以便元素可以并排排列,而如果没有足够的空间,它们将换行显示。

但问题在于,使用GridView.builder,所有元素将具有相同的固定宽度,因此在我的情况下,我需要元素具有可变宽度(例如:取货和送货服务的宽度较大,因为文本较长)。

列出具有不同宽度的小部件(芯片),放在一个容器中。

英文:

I'm trying to make a container with a list of element like shown in the picture below.

I used gridView.builder to make it responsive so the elements will be next to each other and in case there's no space it will return to next line.

But the problem here is with GridView.builder the elements will all have the same fixed width, thus in my case I need the elements to have variable width (example: pickup and delivery service has a bigger width because the text is longer)

列出具有不同宽度的小部件(芯片),放在一个容器中。

答案1

得分: 1

使用 Wrap 小部件替代。请参阅官方文档:

https://api.flutter.dev/flutter/widgets/Wrap-class.html

正如上面链接中的视频所解释的那样,它与图中所示的芯片非常配合得很好。

英文:

Use the Wrap widget instead. See official docs:

https://api.flutter.dev/flutter/widgets/Wrap-class.html

As the video explains on the link above, it works really well with Chips, as you have in your picture.

答案2

得分: -1

使用"Wrap with action chip",以下是你可以参考的示例:

Wrap(
  spacing: 10,
  runSpacing: 10,
  children: services
      .map(
        (data) => ActionChip(
          label: Text(
            data,
          ),
          padding: EdgeInsets.all(5),
          elevation: 5,
          backgroundColor: Colors.white,
          disabledColor: Colors.white,
          onPressed: () {},
        ),
      )
      .toList(),
)
英文:

For this you can use Wrap with action chip, below is example you can refer

        Wrap(
          spacing: 10,
          runSpacing: 10,
          children: services
              .map(
                (data) => ActionChip(
                  label: Text(
                    data,
                  ),
                  padding: EdgeInsets.all(5),
                  elevation: 5,
                  backgroundColor: Colors.white,
                  disabledColor: Colors.white,
                  onPressed: () {},
                ),
              )
              .toList(),
        ),

huangapple
  • 本文由 发表于 2023年4月13日 23:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定