如何在Flutter上创建原始的网格视图

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

How to make original Grid View on Flutter

问题

我想要使用Grid View或其他方式在Flutter中实现类似这张图片的布局。

我使用了GridView.builder,如下所示的代码。但是这段代码无法达到图片中的效果。

你能给我一些关于如何实现这张图片布局的建议吗?

GridView.builder(
  itemCount: items.length,
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
    crossAxisCount: 3,
  ),
  itemBuilder: (BuildContext context, int index) {
    final isDoubleWidth = (index + 1) % 5 == 0;
    final itemWidth = isDoubleWidth ? MediaQuery.of(context).size.width / 3 * 2 : MediaQuery.of(context).size.width / 3;
    return SizedBox(
      width: itemWidth,
      child: Text(item[index].title.toString),
    );
  },
);

如何在Flutter上创建原始的网格视图

英文:

I would like to implement something like this picture using Grid View or another way by Flutter.

I implemented to use of GridView.builder below like codes. But this code doesn't work like the picture.

Could you give me your idea to express like this picture?

GridView.builder(
  itemCount: items.length,
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
    crossAxisCount: 3,
  ),
  itemBuilder: (BuildContext context, int index) {
    final isDoubleWidth = (index + 1) % 5 == 0;
    final itemWidth = isDoubleWidth ? MediaQuery.of(context).size.width / 3 * 2 : MediaQuery.of(context).size.width / 3;
    return SizedBox(
      width: itemWidth,
      child: Text(item[index].title.toString),
    );
  },
);

如何在Flutter上创建原始的网格视图

答案1

得分: 2

只使用 flutter_staggered_grid_view: ^0.6.2

import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

StaggeredGrid.count(
  crossAxisCount: 4,
  mainAxisSpacing: 4,
  crossAxisSpacing: 4,
  children: const [
    StaggeredGridTile.count(
      crossAxisCellCount: 2,
      mainAxisCellCount: 2,
      child: Tile(index: 0),
    ),
    StaggeredGridTile.count(
      crossAxisCellCount: 2,
      mainAxisCellCount: 1,
      child: Tile(index: 1),
    ),
    StaggeredGridTile.count(
      crossAxisCellCount: 1,
      mainAxisCellCount: 1,
      child: Tile(index: 2),
    ),
    StaggeredGridTile.count(
      crossAxisCellCount: 1,
      mainAxisCellCount: 1,
      child: Tile(index: 3),
    ),
    StaggeredGridTile.count(
      crossAxisCellCount: 4,
      mainAxisCellCount: 2,
      child: Tile(index: 4),
    ),
  ],
);

如何在Flutter上创建原始的网格视图

英文:

Just use flutter_staggered_grid_view: ^0.6.2

import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

StaggeredGrid.count(
  crossAxisCount: 4,
  mainAxisSpacing: 4,
  crossAxisSpacing: 4,
  children: const [
    StaggeredGridTile.count(
      crossAxisCellCount: 2,
      mainAxisCellCount: 2,
      child: Tile(index: 0),
    ),
    StaggeredGridTile.count(
      crossAxisCellCount: 2,
      mainAxisCellCount: 1,
      child: Tile(index: 1),
    ),
    StaggeredGridTile.count(
      crossAxisCellCount: 1,
      mainAxisCellCount: 1,
      child: Tile(index: 2),
    ),
    StaggeredGridTile.count(
      crossAxisCellCount: 1,
      mainAxisCellCount: 1,
      child: Tile(index: 3),
    ),
    StaggeredGridTile.count(
      crossAxisCellCount: 4,
      mainAxisCellCount: 2,
      child: Tile(index: 4),
    ),
  ],
);

如何在Flutter上创建原始的网格视图

答案2

得分: 1

GridView不支持像图片中所示的样式。如果您不想使用第三方插件,我建议您使用ListViewRow小部件来构建此UI。您可以将此UI视为行的列表,每行可能包含2个或3个元素。因此,根据ListView.builder()itemBuilder参数的索引,您可以计算应该为此列表项(整个行)显示哪些元素,然后您可以使用Row小部件构建自己的行。

英文:

GridView does not support something like what you have shown in the picture. If you don't want to use 3rd party plugins, I suggest you use ListView and Row widgets to build this UI. You can consider this UI as a list of rows, and each row would contain either 2 or 3 elements. So based on the index of the ListView.builder()'s itemBuilder parameter, you can calculate which elements you should show for this list item (the entire row), and from there you can construct your own row using Row widget.

答案3

得分: 0

使用这段代码,我可以制作出这张图片的效果。

在这种情况下,我增加了5,因为列和行各有5项。

英文:

With this code, I could make that picture likely.

I incremented 5 in this case because the column and row are in 5 items.

 final itemLength = (items.length) / 5;

        return ListView.builder(
            itemCount: itemLength.toInt(),
            itemBuilder: (BuildContext context, int index) {
              index *= 5;
              return Column(
                children: [
                  Row(
                    children: [
                      SizedBox(
                        width: MediaQuery.of(context).size.width * 2 / 3,
                        child: Text(items[index].title.toString()),
                      ),
                      SizedBox(
                        width: MediaQuery.of(context).size.width * 1 / 3,
                        child: Text(items[index + 1].title.toString()),
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      SizedBox(
                        width: MediaQuery.of(context).size.width * 1 / 3,
                        child: Text(items[index + 2].title.toString()),
                      ),
                      SizedBox(
                        width: MediaQuery.of(context).size.width * 1 / 3,
                        child: Text(items[index + 3].title.toString()),
                      ),
                      SizedBox(
                        width: MediaQuery.of(context).size.width * 1 / 3,
                        child: Text(items[index + 4].title.toString()),
                      ),
                    ],
                  ),
                ],
              );
            });

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

发表评论

匿名网友

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

确定