I am trying to layout a 4x4 grid of tiles in flutter. I managed to do it with columns and rows. But now I found the GridView
component. Could anyone provide an example on how to do it using it?
I can't really wrap my head around the docs. I don't seem to get the results I want.
Please visit this repo.
Below screenshot contains crossAxisCount: 2
GridView
is used for implementing material grid lists. If you know you have a fixed number of items and it's not very many (16 is fine), you can useGridView.count
. However, you should note that aGridView
is scrollable, and if that isn't what you want, you may be better off with just rows and columns.There are few named constructors in
GridView
for different scenarios,Constructors
GridView
GridView.builder
GridView.count
GridView.custom
GridView.extent
Below is a example of
GridView
constructor:If you want your GridView items to be dynamic according to the content, you can few lines to do that but the simplest way to use
StaggeredGridView
package. I have provided an answer with example here.Below is an example for a
GridView.count
:Screenshot for above snippet:
Example for a
SliverGridView
:A simple example loading images into the tiles.
The Flutter Gallery app contains a real world example, which can be found here.
Use whichever suits your need.
GridView.count(...)
GridView.builder(...)
GridView(...)
GridView.custom(...)
GridView.extent(...)
Output (same for all):