Skip to main content

Command Palette

Search for a command to run...

GridView widget and Attributes

Published
2 min read
GridView widget  and Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

The GridView widget in Flutter provides a scrollable 2D array of widgets, allowing you to display a grid of items. It's a versatile and powerful widget commonly used for displaying lists of items in a grid format. In this section, we'll explore the attributes of the GridView widget and provide an example for your blog.

1. Attributes of GridView Widget:

1.1 children:

  • Description: A list of widgets to be displayed in the grid.

  • Example:

      GridView(
        children: [
          /* your widgets */,
        ],
      ),
    

1.2 gridDelegate:

  • Description: Defines the layout of the grid, such as the number of columns and their spacing.

  • Example:

      GridView(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 16.0,
          mainAxisSpacing: 16.0,
        ),
        children: [
          /* your widgets */,
        ],
      ),
    

1.3 scrollDirection:

  • Description: Specifies the scrolling direction of the grid, either horizontal or vertical.

  • Example:

      GridView(
        scrollDirection: Axis.horizontal,
        children: [
          /* your widgets */,
        ],
      ),
    

1.4 padding:

  • Description: Defines the padding around the entire grid.

  • Example:

      GridView(
        padding: EdgeInsets.all(16.0),
        children: [
          /* your widgets */,
        ],
      ),
    

2. Example of GridView Widget:

Let's create a simple Flutter application that uses the GridView widget to display a grid of images.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('GridView Example'),
        ),
        body: GridView(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            crossAxisSpacing: 16.0,
            mainAxisSpacing: 16.0,
          ),
          padding: EdgeInsets.all(16.0),
          children: [
            //chnage path according to your images path
            Image.network('https://example.com/image1.jpg'),
            Image.network('https://example.com/image2.jpg'),
            Image.network('https://example.com/image3.jpg'),
            // Add more Image widgets as needed
          ],
        ),
      ),
    );
  }
}

In this example, we've created a Flutter app with a GridView containing a grid of images. The gridDelegate is set to have two columns with spacing between them, and the padding provides some space around the entire grid.

Learn Flutter

Part 1 of 50

Explore Flutter's magic in crafting cross-platform apps effortlessly. Join the adventure!

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"