英文:
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),
);
},
);
英文:
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),
);
},
);
答案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),
),
],
);
英文:
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),
),
],
);
答案2
得分: 1
GridView
不支持像图片中所示的样式。如果您不想使用第三方插件,我建议您使用ListView
和Row
小部件来构建此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()),
),
],
),
],
);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论