In some cases, you might want to display your items as a Grid rather thana normal list of items that come one after the next. For this task, we’ll employthe GridView Widget.

The simplest way to get started using grids is by using theGridView.countconstructor, because it allow us to specify how many rows or columns we’d like.

In this example, we’ll generate a List of 100 Widgets that display theirindex in the list. This will help us visualize how GridView works.

  1. GridView.count(
  2. // Create a grid with 2 columns. If you change the scrollDirection to
  3. // horizontal, this would produce 2 rows.
  4. crossAxisCount: 2,
  5. // Generate 100 Widgets that display their index in the List
  6. children: List.generate(100, (index) {
  7. return Center(
  8. child: Text(
  9. 'Item $index',
  10. style: Theme.of(context).textTheme.headline,
  11. ),
  12. );
  13. }),
  14. );

Complete example

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. final title = 'Grid List';
  9. return MaterialApp(
  10. title: title,
  11. home: Scaffold(
  12. appBar: AppBar(
  13. title: Text(title),
  14. ),
  15. body: GridView.count(
  16. // Create a grid with 2 columns. If you change the scrollDirection to
  17. // horizontal, this would produce 2 rows.
  18. crossAxisCount: 2,
  19. // Generate 100 Widgets that display their index in the List
  20. children: List.generate(100, (index) {
  21. return Center(
  22. child: Text(
  23. 'Item $index',
  24. style: Theme.of(context).textTheme.headline,
  25. ),
  26. );
  27. }),
  28. ),
  29. ),
  30. );
  31. }
  32. }