我正在try 做一个水平滚动按钮组.

我有一个categories个的列表,对于每个列表,我需要一个按钮来呈现. 我的代码如下所示:

GroupButton(
    buttons: [ 
        for(final cat in categories)
            ElevatedButton.icon(
                onPressed: () {
                    _getCategory(cat.id);
                },
                icon: cat.icon,
                label: Text(cat.name, style: const TextStyle(color: Colors.black),
            ),
        ),
    ],
)

I have the expected result but the buttons won't render
With this code the output looks like this:
enter image description here


If I don't wrap the buttons with the GroupButtons I obtain this result:

enter image description here



如何正确渲染按钮?

推荐答案

正如@ Ivo在 comments 中所建议的那样,
"一百"

我的问题的解决方案是添加一个ListView,将scrollDirection设置为horizontal
就像这样:

SizedBox(
    height: 40.0,
    child: ListView(
    scrollDirection: Axis.horizontal,
    children:[

        for(final cat in categories)
            ElevatedButton.icon(
                onPressed: () {
                    _getCategory(cat.id);
                 },
                 icon: cat.icon,
                 label: Text(cat.name, style: const TextStyle(color: Colors.black),),
                 style: ButtonStyle(minimumSize: MaterialStateProperty.all<Size>(const Size(150, 40))),
            ),
        ],
    ),
),

The output will look like this:
enter image description here

Flutter相关问答推荐

如何在容器中移动图标按钮?

BaseInputfield和Textfield有什么不同?

无法将新文物与Flutter 项目集成在一起

如何翻转这种剪刀设计

在Flutter 中创建自定义形状

顶部对齐ListTile的[前导、标题、尾随]小部件

如何处理平台游戏的对象冲突?

Flutter - Stripe:类型List 不是类型Map 的子类型? method_channel_stripe.dart 中的错误:263

Flutter 包错误:此应用程序使用的是已弃用的 Android 嵌入版本

减少 Flutter 中 API 的连接等待时间

具有 BLOC 和存储库模式的 Flutter Workmanager

状况不佳 - Cubit

容器在发布模式下显示灰色框(抖动)

如何将 void 函数放入单独的 dart 文件的单独页面中

如何在 Flutter 中使用简写 IF 禁用单选按钮?

我正在try 使用 Firebase 在 Flutter 中使用 Google 注销,但它不起作用

配置项目:firebase_core时出现问题

如何在 flutter 中制作这种类型的扩展瓦片

在 FAB 上添加表格小部件单击 FLUTTER

RangeError(索引):无效值:有效值范围为空:点击文章时为0