flutter - 在 gridview 上换行时如何设置宽度?

flutter - how to set width when wrapping on gridview?

当我想使用 GridView 点赞 Tags Widget 时遇到问题, 查看结果:

做成如下图:

但是按钮的宽度是根据 GridView 换行自动设置的。

如何动态设置width

任何答案将不胜感激。

您正在尝试制作芯片(来自 material):
https://material.io/design/components/chips.html#

您应该使用 Chip 小部件(或列出的变体之一):
https://api.flutter.dev/flutter/material/Chip-class.html

您应该将网格更改为 Wrap 小部件( 将包含筹码列表): https://api.flutter.dev/flutter/widgets/Wrap-class.html

class MyHomePage extends StatelessWidget {
  var tags = [
    "love",
    "instagood",
    "photooftheday",
    "beautiful",
    "fashion",
    "happy",
    "tbt",
    "cute",
    "followme",
    "like4like",
    "follow",
    "picoftheday",
    "me",
    "selfie",
    "summer",
    "instadaily"
  ];

  var selected_tags = ["love", "me", "summer"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Wrap(
        spacing: 8.0, // gap between adjacent chips
        runSpacing: 4.0, // gap between lines
        children: <Widget>[...generate_tags()],
      ),
    );
  }

  generate_tags() {
    return tags.map((tag) => get_chip(tag)).toList();
  }

  get_chip(name) {
    return FilterChip(
        selected: selected_tags.contains(name),
        selectedColor: Colors.blue.shade800,
        disabledColor: Colors.blue.shade400,
        labelStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
        label: Text("#${name}"));
  }
}