GridView Example

This Article is posted by seven.srikanth at 8/21/2018 3:46:10 PM



This example will help you to display a few images within a GirdView in Flutter. We have used GridView.count layouts in this example. If you are further interested in knowing more example on other GridView layouts, checkout my another article on GridView Widget - Grid layouts Implementation - GirdView, GridView.count, GridView.builder, GridView.custom, GridView.extent - https://fluttercentral.com/Articles/Post/45 Here is how the final output looks like,  
Below is the live coding video of GridView. Subscribe to my channel for more videos like this. 
 
 Let's see how this can be achieved. 1) Create a Class for the Items that need to be shown in the Grid and add some temporary data.
  class Choice {
    const Choice({this.title, this.icon});
  
    final String title;
  
    final IconData icon;
  }
  
  const List<Choice> choices = const [
    const Choice(title: 'Car', icon: Icons.directions_car),
    const Choice(title: 'Bicycle', icon: Icons.directions_bike),
    const Choice(title: 'Boat', icon: Icons.directions_boat),
    const Choice(title: 'Bus', icon: Icons.directions_bus),
    const Choice(title: 'Train', icon: Icons.directions_railway),
    const Choice(title: 'Walk', icon: Icons.directions_walk),
    const Choice(title: 'Car', icon: Icons.directions_car),
    const Choice(title: 'Bicycle', icon: Icons.drafts),
    const Choice(title: 'Boat', icon: Icons.dvr),
    const Choice(title: 'Bus', icon: Icons.copyright),
    const Choice(title: 'Train', icon: Icons.cloud_off),
    const Choice(title: 'Car', icon: Icons.directions_car),
    const Choice(title: 'Bicycle', icon: Icons.directions_bike),
    const Choice(title: 'Boat', icon: Icons.directions_boat),
    const Choice(title: 'Bus', icon: Icons.directions_bus),
    const Choice(title: 'Train', icon: Icons.directions_railway),
    const Choice(title: 'Walk', icon: Icons.directions_walk),
    const Choice(title: 'Car', icon: Icons.directions_car),
    const Choice(title: 'Bicycle', icon: Icons.drafts),
    const Choice(title: 'Boat', icon: Icons.dvr),
    const Choice(title: 'Bus', icon: Icons.copyright),
    const Choice(title: 'Train', icon: Icons.cloud_off),
    const Choice(title: 'Car', icon: Icons.directions_car),
    const Choice(title: 'Bicycle', icon: Icons.directions_bike),
    const Choice(title: 'Boat', icon: Icons.directions_boat),
    const Choice(title: 'Bus', icon: Icons.directions_bus),
    const Choice(title: 'Train', icon: Icons.directions_railway),
    const Choice(title: 'Walk', icon: Icons.directions_walk),
    const Choice(title: 'Car', icon: Icons.directions_car),
    const Choice(title: 'Bicycle', icon: Icons.drafts),
    const Choice(title: 'Boat', icon: Icons.dvr),
    const Choice(title: 'Bus', icon: Icons.copyright),
    const Choice(title: 'Train', icon: Icons.cloud_off),
  ];
  


2) Create a Stateless widget. This is where we actually construct the Widget that we are going to show in the GridView. This is the layout we define for each item that needs to be shown in the GridView.
  class ChoiceCard extends StatelessWidget {
    const ChoiceCard({Key key, this.choice}) : super(key: key);
  
    final Choice choice;
  
    @override
    Widget build(BuildContext context) {
      final TextStyle textStyle = Theme.of(context).textTheme.display1;
  
      return Card(
          color: Colors.white,
          child: Center(
            child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Icon(choice.icon, size: 90.0, color: textStyle.color),
                  Text(choice.title, style: textStyle),
                ]),
          ));
    }
  }
  

You can modify to see how changes can affect your layout. 3) Call the above widget class from the body within the GridView generate method. 
GridView.count(
                crossAxisCount: 3,
                children: List.generate(choices.length, (index) {
                  return Center(
                    child: ChoiceCard(choice: choices[index]),
                  );
                }))
4) If you are seeing a message like "BOTTOM OVERFLOWED BY X.XX PIXEL". Then wrap the Icon Widget inside an Expanded Widget as below.
  Expanded(child: Icon(choice.icon, size:90.0, color: textStyle.color)),
Here is the final Main.dart code. You just need to copy it into main.dart file in your newly created flutter project and run it.   
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = "Grid List";

    return MaterialApp(
        title: title,
        home: Scaffold(
            appBar: AppBar(
              title: Text(title),
            ),
            body: GridView.count(
                crossAxisCount: 3,
                children: List.generate(choices.length, (index) {
                  return Center(
                    child: ChoiceCard(choice: choices[index]),
                  );
                }))));
  }
}

class Choice {
  const Choice({this.title, this.icon});

  final String title;

  final IconData icon;
}

const List<Choice> choices = const [
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.drafts),
  const Choice(title: 'Boat', icon: Icons.dvr),
  const Choice(title: 'Bus', icon: Icons.copyright),
  const Choice(title: 'Train', icon: Icons.cloud_off),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.drafts),
  const Choice(title: 'Boat', icon: Icons.dvr),
  const Choice(title: 'Bus', icon: Icons.copyright),
  const Choice(title: 'Train', icon: Icons.cloud_off),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.drafts),
  const Choice(title: 'Boat', icon: Icons.dvr),
  const Choice(title: 'Bus', icon: Icons.copyright),
  const Choice(title: 'Train', icon: Icons.cloud_off),
];

class ChoiceCard extends StatelessWidget {
  const ChoiceCard({Key key, this.choice}) : super(key: key);

  final Choice choice;

  @override
  Widget build(BuildContext context) {
    final TextStyle textStyle = Theme.of(context).textTheme.display1;

    return Card(
        color: Colors.white,
        child: Center(
          child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(
                    child:
                        Icon(choice.icon, size: 90.0, color: textStyle.color)),
                Text(choice.title, style: textStyle),
              ]),
        ));
  }
}




Thanks, 
Srikanth

Tags: GridView Example in Flutter; GridView in Flutter; GridView with Images in Flutter; BOTTOM OVERFLOWED BY X.XX PIXEL;








4 Comments
Login to comment.
Recent Comments

shravan_pottala at 9/5/2020

I am unable to keep a navigation event using container like Container( color: Colors.cyan[100], child: GestureDetector ( onTap:(){ Navigator.of(context).push(MaterialPageRoute(builder:(context)=>MobileAccessoriesPage(), ), ); }, child: Center (child:Text ('Mobiles'),icon: Icons.mobile_screen_share), ), ), How to use container at const List choices = const [ const Choice(title: 'Car', icon: Icons.directions_car), const Choice(title: 'Bicycle', icon: Icons.directions_bike), const Choice(title: 'Boat', icon: Icons.directions_boat), const Choice(title: 'Bus', icon: Icons.directions_bus), ]

Login to Reply.

ahasby at 8/27/2018

I have tried but i got a problen with output "BOTTOM OVERFLOADED BY 8.0 PIXEL" what the solution ?

Login to Reply.

seven.srikanth at 8/27/2018

Reduce the Icon Size. In this Example, I have set it to size:80.0. You can reduce it.

seven.srikanth at 9/11/2019

I have updated the article to fix this change. I used an Expanded widget to fix this one.












© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com