【问题标题】:How to achive this random width grid view in flutter如何在颤动中实现这种随机宽度的网格视图
【发布时间】:2022-01-19 11:48:05
【问题描述】:

这些文本将来自 REST API。我需要在底页显示这些。但我不应该保持宽度固定。相反,容器的宽度将取决于文本的长度。如何使这个在颤动?

GridView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  padding: EdgeInsets.all(0),
                  itemCount: controller.data.length,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 4,
                  ),
                  itemBuilder: (BuildContext context, int index) {
                    return Obx(() => controller.fakeObs.isTrue
                        ? InkWell(
                            onTap: () {
                              controller.commentTap(
                                  text: "${controller.data[index]}");
                            },
                            child: Container(
                              color: Colors.white,
                              child: Text("${controller.data[index]}"),
                            ),
                          )
                        : Container());
                  },
                )

【问题讨论】:

    标签: flutter gridview flutter-getx


    【解决方案1】:

    我找到了一个符合我的目的的小部件。在这里分享

    Wrap(
                        spacing: 8.0, // gap between adjacent chips
                        runSpacing: 4.0, // gap between lines
                        children: <Widget>[
                          for (int index = 0;
                              index < controller.data.length;
                              index++)
                            FilterChip(
                              backgroundColor: Colors.white,
                              elevation: 5,
                              labelStyle: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold),
                              label: Text(
                                "${controller.data[index]}",
                                style: TextStyle(
                                    fontSize: 10,
                                    fontWeight: AppTextStyle.mediumFont,
                                    color: AppColors.TextGrey),
                              ),
                              onSelected: (bool value) {
                                print(
                                    "DATA IS ${controller.data[index]} $value");
                              },
                            )
                        ],
                      ),
    

    【讨论】:

      【解决方案2】:

      不是正确的答案,但很有帮助.. 你可以这样做 宽度:控制器。数据[索引]。长度 * 10 + 20

      这 10 可以是任何数字,但是您想设置宽度,20 是水平填充。从左到右 10,从右到 10。

      【讨论】:

      • 已经尝试过类似的方法。但不工作。谢谢你的建议。欣赏它
      猜你喜欢
      • 2021-02-11
      • 2022-01-22
      • 1970-01-01
      • 2020-03-18
      • 2021-10-05
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多