Using the "children" parameter:
GridView.count(
crossAxisCount: 2,
children: [
Container(color: Colors.red),
Container(color: Colors.blue),
Container(color: Colors.green),
Container(color: Colors.yellow),
],
);}
In this example, we're creating a GridView with 2 columns using the `crossAxisCount` parameter. We're then providing it with a list of 4 containers, each with a different background color. This will create a grid layout with 2 columns and 2 rows.
Using the "builder" parameter:
GridView.builder(
itemCount: 10,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemBuilder: (BuildContext context, int index) {
return Container(color: Colors.purple);
},
);}
In this example, we're creating a GridView with 3 columns using the `gridDelegate` parameter. We're then generating a list of 10 containers dynamically using the `itemBuilder` parameter. Each container has a background color of purple.
The GridView also allows us to add a number of different scrollable physics options, including a scrollable that snaps from item to item using the "PageScrollPhysics".
GridView.count(
physics: PageScrollPhysics(),
crossAxisCount: 2,
children: [
// ...
],
);}
In this example, we've added the `PageScrollPhysics()` option to create a grid layout that snaps to each item instead of scrolling smoothly.
In conclusion, the GridView widget is a powerful tool for creating grid layouts in Flutter. We can either provide a list of widgets using the "children" parameter or generate them dynamically using the "builder" parameter. We can also add different scrollable physics options for unique user experiences.