我正在创建类似测验的应用程序.我的目标是从单独的按钮(框)打开每个问题.按钮的数量不是恒定的,所以我动态生成它们. 然而,我在统一它们的大小(每个应该具有相同的宽度和高度)并在它们和屏幕之间设置空间方面遇到了一个问题.

我想实现类似的功能,但正如您所看到的,按钮的宽度各不相同,并且屏幕两侧的填充方式也不同.

enter image description here

因此按钮应该这样组织:

[dynamic space to edge][button][button space][button][buttons space][.....][button][dynamic space to edge].

我这样try 过:

Widget getSectionBoxes(String id, num j) {
  List<Widget> boxes = [];

  for (int i = 0; i < questions[id].length; i++) {
    j++;
    String questionId = questions[id][i];
    
    boxes.add(OutlinedButton(
        onPressed: () {
          debugPrint(questionId);
        },
        style: ButtonStyle(
            shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
        ))),
        child: Text(j.toString())));
        boxes.add(const SizedBox(width: 10));
  }

  return Wrap(
    alignment: WrapAlignment.center,
    children: boxes,
  );
}

我try 过使用Wrap小部件,它可以按照我想要的方式订购盒子,但没有空间.如果我在按钮之间添加sizedBox小部件(width: 10),那么我的左边缘或右边缘就有额外的空间.它在第一行尤其明显.

推荐答案

try 下面的代码希望对您有帮助,我用了padLeft方法来获取个位数ID

声明:

 List<Map<String, dynamic>> jsonList = [];
    for (int i = 1; i < 100; i++) {
      jsonList.add({"Id": i});
    }

UI :

Padding(
    padding: EdgeInsets.symmetric(horizontal: 5),
    child: Wrap(
      spacing: 10.0,
      runSpacing: 10.0,
      alignment: WrapAlignment.center,
      children: List.generate(
        jsonList.length,
        (index) => OutlinedButton(
          onPressed: () {},
          style: ButtonStyle(
            shape: MaterialStateProperty.all<RoundedRectangleBorder>(
              RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10),
              ),
            ),
          ),
          child: Text(
            jsonList[index]['Id'].toString().padLeft(2, "0"),
          ),
        ),
      ),
    ),
  ),

Result Screen- image

Flutter相关问答推荐

自动测试Flutter应用程序进行QA的最佳方法是什么?

如何使一个悬浮的appbar在flutter?

Flutter /dart 列表.第一个子类列表中的Where()错误

为什么Spotify Auth API返回400,而";code_verier不正确?

为什么FloatingActionButton未显示

Flutter卡内容未在水平列表视图中居中

如何在flutter中更改showModalBottomSheet小部件中CheckboxListTile小部件的值?

将自定义容器附加到屏幕底部

「Flutter」错误:没有名为'kind'的命名参数

Flutter ListView 在调用 setState 后未更新

Flutter url_launcher 插件抛出java.lang.IllegalArgumentException:接收器未注册:io.flutter.plugins.urllauncher.WebViewActivity

在 Flutter 中显示有关提供者通知的对话框

如何在 Flutter (iOS) 中解决这个问题

如何更改 DropDownMenu 的显示方向?

这是使用导航功能处理从一页到另一页的移动的最佳方式

如何让小部件根据小部件的大小构建不同的布局?

在向 Google Route API 发送 HTTP Post 请求期间发生错误,

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

无法将参数类型Future Function(String?)分配给参数类型void Function(NotificationResponse)?

Flutter RawMaterialButton 常量相对大小