Beautiful Grid View Builder In Flutter App-Detailed Customization

20220304 190842 0000

In this tutorial, we’ll learn about Flutter grid view builder in detail. What it really is, its role, and its usage in flutter apps. We’ll understand it using practical Flutter code examples to better understand it.

What is Flutter Grid View Builder?

Grid view, as the name suggests, is a grid of widgets, you give it one widget and it will build multiple widgets using its builder from that widget. Grid view is a really cool way to showcase your widgets in the flutter app. Top App bar that you see in the design, if you want to know how to implement it then we have written a complete blog on it as well. Click here to understand how it is implemented.

By the end of this article, you will be able to design the above-mentioned design in the flutter app and all the details that you would need to implement a grid view like this, so you can customize the grid view in the flutter app according to your desired requirements. Let’s start now.

Implementing Grid View Builder

GridView.builder(

        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(

            mainAxisSpacing: 5,

            crossAxisSpacing: 5,

            crossAxisCount: 3,

            childAspectRatio: 1.2),

        itemBuilder: (context, index) {

          return Container();

        },

      )

Grid view class is used for that and by using its builder function, we have implemented our grid. Using its grid delegate constructor, we can customize the size, etc. of the grid items and the number of rows as well. Let’s see how:

Extend Body Behind App Bar

extendBodyBehindAppBar: true
It is the scaffold’s constructor and if set to true and even if the app bar is implemented then still the body starts from the very top of the screen and not after the app bar. You can test it yourself.

In our case, it is true because when you scroll then it does not show any white background app bar on the sides. I know it’s confusing but you will understand it when you test your app after compiling it.

Cross Axis Count

 crossAxisCount: 3
Cross axis count constructor of the silver grid delegate with a fixed cross-axis count class defines how many rows can you see in a grid view. We have set it to 3, you can choose 4, 5, etc.

Child Aspect Ratio

childAspectRatio: 1.2
The child aspect ratio constructor of the silver grid delegate with fixed cross-axis count class defines the ratio of the cross-axis to the main axis extent of each child. The greater value you give it, the smaller the grid item you get. You can play with the values to get your desired item size.

Main Axis Spacing

mainAxisSpacing: 5
Main axis spacing is the space between the grid items vertically, from top to bottom.

Cross Axis Spacing

crossAxisSpacing: 5
Cross-axis spacing is the space between the grid items vertically, from left to right.

Item Count

  itemCount: 10
This constructor defines how many items the grid view build will have. In our case, we have set it to 10 means 10 items will be shown on the screen.

Padding

padding: EdgeInsets.all(10).copyWith(top: 150)
We can use the padding constructor to give padding to our grid view from left, right, top, and bottom, not between items.

As you can see we have given 10 padding to all but we want to give 150 to the top, so we can do it by using the copy with method in which the edge in the sets class extends and specify top, bottom, or other constructors values.

Item Builder

 itemBuilder: (context, index) {

          return Container(

            color: Colors.green,

          );

        },

The item builder constructor is what builds and returns the list of widgets. It has two parameters, build context and index. The index starts from 0 to infinity if you don’t specify the number of items.

In our case, we have given it a container widget. If you want to know how to customize a container then click here. It will return an infinite number of containers if we didn’t specify the item count.

Container Modifications

itemBuilder: (context, index) {

          return Container(

              alignment: Alignment.center,

              margin: EdgeInsets.all(5),

              decoration: BoxDecoration(

                  shape: BoxShape.circle,

                  color: Colors.pink.shade300,

                  boxShadow: [

                    BoxShadow(

                        color: Colors.black54,

                        spreadRadius: 2,

                        blurRadius: 4,

                        offset: Offset(2, 2))

                  ]),

              child: Container(

                  alignment: Alignment.center,

                  margin: EdgeInsets.all(15),

                  decoration: BoxDecoration(

                      shape: BoxShape.circle,

                      color: Colors.blue.shade500,

                      boxShadow: [

                        BoxShadow(

                            color: Colors.black54,

                            spreadRadius: 2,

                            blurRadius: 4,

                            offset: Offset(2, 2))

                      ]),

                  child: Text(

                    index.toString(),

                    style: TextStyle(

                        color: Colors.white,

                        fontWeight: FontWeight.bold,

                        fontSize: 20),

                  )));

        }

Let’s understand the container item that the grid view builder is returning in the flutter app. We used a margin in the container and made its shape circular. We have also given it a shadow and color. Now in its child constructor, we have given it another container with shadow, color, and margin and in that container’s child constructor, we have given it a text widget.

If you are interested in understanding how the text widget works then you can click here.

Conclusion

We hope you now have learned how to properly use the Flutter grid view builder. Don’t forget to leave your feedback in the comment section.

We’d also be glad to see you visit our other tutorials on Flutter app development and Python programming. Thank you for reading this one.

Leave a Comment

Your email address will not be published. Required fields are marked *