我可以帮你翻译这句话:如何使用Flutter创建图像网格。

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

How can i create an Image Grid using Flutter

问题

我正在尝试使用Flutter来实现这个,可能有很多图片,但我找不到合适的组件来完成它...请问谁知道哪个组件可以实现这个,或者如何在Dart和Flutter中实现它,如果有任何Dart包可以实现这个,请提前致谢。请查看参考图片如下: 我可以帮你翻译这句话:如何使用Flutter创建图像网格。

英文:

i am trying to achieve this using flutter, there can be a lot of images but i cant find the right component to get it done... please who has an idea which component achieves this or how it can be done, specifically in Dart, flutter..

and if there is any Dart package that can achieve this, please, and thanks in advance..

find the reference image below.. 我可以帮你翻译这句话:如何使用Flutter创建图像网格。

答案1

得分: 1

已有一些画廊构建插件,你可以在 pub.dev 上查看,例如 flutter_staggered_grid_viewstaggered_grid_view_flutter

无论如何,如果你想尝试创建一个不同的画廊或自定义网格设计,可能你应该创建自己的小部件。

对于你的示例,我的方法不是网格,而是一个列表视图。
每一行都会有你的自定义布局,加载多个图像(根据你的示例最多可以加载4张照片),或根据你可能实现的其他布局设计加载更多照片。

你也可以使用两列内含两行,或者使用行和容器或SizedBox来创建这种效果。

英文:

There are already some gallery builder plugins, that you can probably check on pub.dev, like the flutter_staggered_grid_view or the staggered_grid_view_flutter

Anyway, if you want to try to create a different gallery or a custom grid design, probably you should create your own widget.

For your example, my approach would not be a grid but a List View.
Each row would have your custom layout, loading multiple images, (up to 4 photos according to your example), or more depending on other layout designs you might implement.

you can also create this effect using 2 Columns with 2 rows inside or
with rows and containers or SizedBox.

答案2

得分: 1

你可以使用 flutter_staggered_grid_view,你需要调整 mainAxisCellCount。当有4个项目在一组时,它将是

mainAxisCellCount: switch (i % 4) {
  0 || 3 => .25,
  _ => .75,
},

你可以使用这个小部件。

class Stg4 extends StatelessWidget {
  const Stg4({super.key});

  @override
  Widget build(BuildContext context) {
    final items = List.generate(10, (index) => "path $index");
    List<Widget> children = [
      for (int i = 0; i < items.length; i++)
        StaggeredGridTile.count(
          crossAxisCellCount: 1,
          mainAxisCellCount: switch (i % 4) {
            0 || 3 => .25,
            _ => .75,
          },
          child: ColoredBox(color: Colors.cyanAccent, child: Text(items[i])),
        ),
    ];
    return Scaffold(
      body: StaggeredGrid.count(
        crossAxisCount: 2,
        mainAxisSpacing: 4,
        crossAxisSpacing: 4,
        children: children,
      ),
    );
  }
}
英文:

You can use flutter_staggered_grid_view, you need to play with mainAxisCellCount. While 4 items are in a group, it will be

mainAxisCellCount: switch (i % 4) {
  0 || 3 =&gt; .25,
  _ =&gt; .75,
},

You can play with this widget.

class Stg4 extends StatelessWidget {
  const Stg4({super.key});

  @override
  Widget build(BuildContext context) {
    final items = List.generate(10, (index) =&gt; &quot;path $index&quot;);
    List&lt;Widget&gt; children = [
      for (int i = 0; i &lt; items.length; i++)
        StaggeredGridTile.count(
          crossAxisCellCount: 1,
          mainAxisCellCount: switch (i % 4) {
            0 || 3 =&gt; .25,
            _ =&gt; .75,
          },
          child: ColoredBox(color: Colors.cyanAccent, child: Text(items[i])),
        ),
    ];
    return Scaffold(
      body: StaggeredGrid.count(
        crossAxisCount: 2,
        mainAxisSpacing: 4,
        crossAxisSpacing: 4,
        children: children,
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年8月4日 23:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837396.html
匿名

发表评论

匿名网友

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

确定