In this lesson you will learn how to use GridView in beat.
When creating mobile applications with flutter, GridView is used to display data in rows and columns. You can use GridView to display widgets such as text, an image, a map, a button, etc. GridView scrolls, that is, when you create this widget, you need to specify the scrolling direction.
You can create GridView in two different ways:
- the builder of GridView.count(() and
- GridView.builder()
GridView.count() Designer
GridView.count() is the constructor with whom we can make the GridView widget. Let’s take a look at the very simple code of a mobile flutter application that the GridView.count() constructor uses to create a grid view.
infeed ‘packaging: flatter/material.dart
void main() => runApp(MyApp()) ;
The MyApp class expands the StatelessWidget {@overrideWidget build(BuildContext context) { returns MaterialApp(home : Scaffolding(appBar : AppBar(header: Text (‘GridView’),), body : Centre(child : Container(// Height : 200,child : GridView.count (scrollDirection: Axis.vertical, crossAxisCount: 2, children: Card (color: colors.teal,), Card (color: colors.cyan,), Card (color: colors.yellowAccent,), Card (color: colors.deepOrange,), Card (color: colors.deepOrange,).red,),Card (color: colors.red,),Card (color: colors.yellow,),Card (color: colors.purpleAccent,),Card (color: colors.indigo,),Card (color: colors.black,),Card (color: colors.pinkAccent,);};}.
As you can see in the example above, the GridView properties we use are the following:
- Scrolling tube : Vertical.axis – here we set the scroll direction to the vertical. If your mobile application needs to scroll horizontally, you can use Axis.horizontal instead.
- CrossAxisCont: 2 – this means that our elements are displayed in two vertical columns. If your mobile application needs more than two vertical columns, you can enter an additional value here.
If you run the above example code in your Flutter development environment, you should get a mobile application that looks like the image below:
GridView.builder()
GridView.builder() creates a 2D drop-down array with on-demand widgets. We use GridView.builder() if we want to display data dynamically in the grid. Usually used in a gallery that reads advertising images from your backend. Here’s a sample code for him:
infeed ‘packaging: flatter/material.dart
void main() => runApp(MyApp()) ;
The MyApp class extends the StatelessWidget {@overrideWidget build(BuildContext-Context) {Settings in the list = [assets/Apples.png,assets/Bananas.png,assets/Cherries.png,activa/Grapes.png,activa/Oranges.png,activa/Peaches.png,activa/Plumbs.png,activa/Rasberries.png,activa/Strabberries.png,];returns MaterialApp(home: scaffold(appBar : AppBar(name: Text (‘GridView’),), body : Centre(child : Centre(child : Container (child : GridView.builder(itemTelling: Images.length,gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3,crossAxisSpacing: 5,0,mainAxisSpacing: 5,0,),itemBuilder : (BuildContext context, int index) {return Image.asset(images [index]);},);};}.
As you can see, we have a list of URLs called images. In GridView.builder() we set itemCount to the length of the list images and the grid delegation property to the SliverGridDelegateWithFixedCrossAxisCount object. It is important to note that itemCount and SliverGridDelegateWithFixedCrossAxisCount are mandatory properties and their values cannot be zero.
In the object SliverGridDelegateWithFixedCrossAxisCount we have configured three properties :
- crossAxisCount – set to 3 (three vertical columns),
- CrossAxisSpacing is the vertical distance between the elements, and
- mainAxisSpacing is a horizontal distance.
The second required function is itemBuilder. We use the itemBuilder property to define a function that accepts two parameters (context and index) and returns an Image.asset widget for each image URL stored in the image list.
Mount and run the above example code in your Flutter development environment. If all goes well, your mobile application will look exactly like the image below.
I hope that beat was good for you. If you are interested in flutter, read the other flutter tutorials on this site. Some of them contain video courses.
Good mobile application development with Flutter!
Related Tags:
gridview builder flutter,flutter gridview example,flutter staggered grid view,flutter gridview height,flutter gridview ontap,gridview inside column flutter,flutter github projects,flutter engine github,flutter shopping app github,flutter ecommerce github,flutter-ui github,flutter layout example github,flutter gridview example github,flutter web demo,flutter web performance,is flutter web production ready,flutter desktop example,flutter web tutorial,flutter web roadmap,gridview.builder flutter example,dynamic gridview in flutter,flutter gridview in listview,flutter gridview in column