Updated 28 April 2023
Flutter Staggered grid view supports multiple columns with different row sizes.
In this blog, we will look to design a page with the different column count at the different row.
Read more about Flutter app development from mobikul.
StaggeredTileBuilder is used to create different no of the column in row and height of each tile.
There are three types of staggered tile, StaggeredTile.count
, StaggeredTile.fit
 StaggeredTile.extent
 .
We will use StaggeredTile.extent(this.crossAxisCellCount, this.mainAxisExtent).
Where crossAxisCellCount
is the number of column in a row and mainAxisExtent
 is the height of row item/tile.
Here, we will show the 1,2,3 column count on different rows.
Let’s calculate crossAxisCellCount
SingleColumn crossAxisCellCount
 = 1*2*3=6/1 =6
TwoColumnGrid crossAxisCellCount
 = 1*2*3=6/2 =3
ThreeColumnGrid crossAxisCellCount
 = 1*2*3=6/3 =2
1 2 3 4 5 6 7 8 9 10 11 12 |
staggeredTileBuilder: (int index) { if (listData[index].itemViewType == "TwoColumnGrid") { return new StaggeredTile.extent( 3, (1 / 2) * MediaQuery.of(context).size.width); } else if (listData[index].itemViewType == "ThreeColumnGrid") { return new StaggeredTile.extent( 2, (1 / 3) * MediaQuery.of(context).size.width); } else { return new StaggeredTile.extent( 6, (1 / 2) * MediaQuery.of(context).size.width); } }); |
We will get the result as attached image
Let’s check the complete code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
class _HomePageState extends State<MyHomePage> { List<HomePageItems> listData = []; @override void initState() { super.initState(); listData.add(HomePageItems(itemViewType: "SingleColumn")); listData.add(HomePageItems(itemViewType: "TwoColumnGrid")); listData.add(HomePageItems(itemViewType: "TwoColumnGrid")); listData.add(HomePageItems(itemViewType: "TwoColumnGrid")); listData.add(HomePageItems(itemViewType: "TwoColumnGrid")); listData.add(HomePageItems(itemViewType: "ThreeColumnGrid")); listData.add(HomePageItems(itemViewType: "ThreeColumnGrid")); listData.add(HomePageItems(itemViewType: "ThreeColumnGrid")); listData.add(HomePageItems(itemViewType: "ThreeColumnGrid")); listData.add(HomePageItems(itemViewType: "ThreeColumnGrid")); listData.add(HomePageItems(itemViewType: "ThreeColumnGrid")); } @override Widget build(BuildContext context) { return StaggeredGridView.countBuilder( crossAxisCount: 6, shrinkWrap: true, itemCount: listData.length, itemBuilder: (BuildContext context, int index) { switch (listData[index].itemViewType) { case 'TwoColumnGrid': { return Container( color: Colors.blue, margin: EdgeInsets.all(4), ); } break; case 'ThreeColumnGrid': { return Container( color: Colors.yellow, margin: EdgeInsets.all(4), ); } break; default: { return Container( color: Colors.red, margin: EdgeInsets.all(4), ); } break; } }, staggeredTileBuilder: (int index) { if (listData[index].itemViewType == "TwoColumnGrid") { return new StaggeredTile.extent( 3, (1 / 2) * MediaQuery.of(context).size.width); } else if (listData[index].itemViewType == "ThreeColumnGrid") { return new StaggeredTile.extent( 2, (1 / 3) * MediaQuery.of(context).size.width); } else { return new StaggeredTile.extent( 6, (1 / 2) * MediaQuery.of(context).size.width); } }); } } |
I hope this blog will help to make the different number of columns in different rows.
Happy Learning 🙂
Reference Links :
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.