• About WordPress
    • WordPress.org
    • Documentation
    • Support
    • Feedback
  • Log In
  • Register
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment
Cuitutorial
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment

Mobile Application Development

Home » Blog » GridView GridTile in flutter

GridView GridTile in flutter

  • Posted by saqib
  • Categories Mobile Application Development
  • Date December 29, 2022
  • Comments 0 comment

GridView GridTile in flutter

A grid view is a graphical control element used to show items in the tabular form. In this section, we are going to learn how to render items in a grid view in the Flutter application

GridView is a widget in Flutter that displays the items in a 2-D array (two-dimensional rows and columns)

It is also referred to as a scrollable 2-D array of widgets.

The grid view can be implemented in various ways, which are given below:

  1. count()
  2. builder()
  3. custom()
  4. extent()

GridView.count()

It is the most frequently used grid layout in Flutter because here, we already know the grid’s size. It allows developers to specify the fixed number of rows and columns. The GriedView.count() contains the following properties:

crossAxisCount: It is used to specify the number of columns in a grid view.

crossAxisSpacing: It is used to specify the number of pixels between each child widget listed in the cross axis.

mainAxisSpacing: It is used to specify the number of pixels between each child widget listed in the main axis.

padding(EdgeInsetsGeometry): It is used to specify the space around the whole list of widgets.

scrollDirection: It is used to specify the direction in which the items on GridView scrolls. By default, it scrolls in a vertical direction.

reverse: If it is true, it will reverse the list in the opposite direction along the main axis.

physics: It is used to determine how the list behaves when the user reaches the end or the start of the widget while scrolling.

shrinkWrap: If it is false, the scrollable list takes more space for scrolling in scroll direction. It is not good because it wastage memory and reduces app performance. Therefore, we will wrap our children widgets using shrinkWrap by setting it to true to avoid memory leakage while scrolling.

Example

  1. import ‘package:flutter/material.dart’;
  2. void main() {runApp(MyApp());}
  3. class MyApp extends StatelessWidget {
  4.   @override
  5.   Widget build(BuildContext context) {
  6.     return MaterialApp(
  7.         home: Scaffold(appBar: AppBar(
  8.           title: Text(“Flutter GridView Demo”),
  9.         ),
  10.             body: GridView.count(
  11.                 crossAxisCount: 3,
  12.                 crossAxisSpacing: 4.0,
  13.                 mainAxisSpacing: 8.0,
  14.                 children: List.generate(choices.length, (index) {
  15.                   return Center(
  16.                     child: SelectCard(choice: choices[index]),
  17.                   );
  18.                 }
  19.                 )
  20.             )
  21.         )
  22.     );
  23.   }
  24. }
  25. class Choice {
  26.   const Choice({this.title, this.icon});
  27.   final String title;
  28.   final IconData icon;
  29. }
  30. const List<Choice> choices = const <Choice>[
  31.   const Choice(title: ‘Home’, icon: Icons.home),
  32.   const Choice(title: ‘Contact’, icon: Icons.contacts),
  33.   const Choice(title: ‘Map’, icon: Icons.map),
  34.   const Choice(title: ‘Phone’, icon: Icons.phone),
  35.   const Choice(title: ‘Camera’, icon: Icons.camera_alt),
  36.   const Choice(title: ‘Setting’, icon: Icons.settings),
  37.   const Choice(title: ‘Album’, icon: Icons.photo_album),
  38.   const Choice(title: ‘WiFi’, icon: Icons.wifi),
  39. ];
  40. class SelectCard extends StatelessWidget {
  41.   const SelectCard({Key key, this.choice}) : super(key: key);
  42.   final Choice choice;
  43.   @override
  44.   Widget build(BuildContext context) {
  45.     final TextStyle textStyle = Theme.of(context).textTheme.display1;
  46.     return Card(
  47.         color: Colors.orange,
  48.         child: Center(child: Column(
  49.             crossAxisAlignment: CrossAxisAlignment.center,
  50.             children: <Widget>[
  51.               Expanded(child: Icon(choice.icon, size:50.0, color: textStyle.color)),
  52.               Text(choice.title, style: textStyle),
  53.             ]
  54.         ),
  55.         )
  56.     );
  57.   }
  58. }

GridView.builder()

This property is used when we want to display data dynamically or on-demand. In other words, if the user wants to create a grid with a large (infinite) number of children, then they can use the GridView.builder() constructor with either a SliverGridDelegateWithFixedCrossAxisCount or a SliverGridDelegateWithMaxCrossAxisExtent.

The common attributes of this widget are:

itemCount: It is used to define the amount of data to be displayed.

Example

  1. import ‘package:flutter/material.dart’;
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4.   List<String> images = [
  5.     “https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png”,
  6.     “https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png”,
  7.     “https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png”,
  8.     “https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png”
  9.   ];
  10.   @override
  11.   Widget build(BuildContext context) {
  12.     return MaterialApp(
  13.       home: Scaffold(
  14.         appBar: AppBar(
  15.           title: Text(“Flutter GridView Demo”),
  16.           backgroundColor: Colors.red,
  17.         ),
  18.         body: Container(
  19.             padding: EdgeInsets.all(12.0),
  20.             child: GridView.builder(
  21.               itemCount: images.length,
  22.               gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  23.                   crossAxisCount: 2,
  24.                   crossAxisSpacing: 4.0,
  25.                   mainAxisSpacing: 4.0
  26.               ),
  27.               itemBuilder: (BuildContext context, int index){
  28.                 return Image.network(images[index]);
  29.               },
  30.             )),
  31.       ),
  32.     );
  33.   }
  34. }

Output

GridView.extent()

This property is used when we want to create a grid with custom extent values. It means each tile has a maximum cross-axis extent.

Example

  1. import ‘package:flutter/material.dart’;
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4.   // This widget is the root of your application.
  5.   @override
  6.   Widget build(BuildContext context) {
  7.     return MaterialApp(
  8.       home: MyGridScreen(),
  9.     );
  10.   }
  11. }
  12. class MyGridScreen extends StatefulWidget {
  13.   MyGridScreen({Key key}) : super(key: key);
  14.   @override
  15.   _MyGridScreenState createState() => _MyGridScreenState();
  16. }
  17. class _MyGridScreenState extends State<MyGridScreen> {
  18.   @override
  19.   Widget build(BuildContext context) {
  20.     return Scaffold(
  21.       appBar: AppBar(
  22.         title: Text(“Flutter GridView Demo”),
  23.         backgroundColor: Colors.green,
  24.       ),
  25.       body: Center(
  26.           child: GridView.extent(
  27.             primary: false,
  28.             padding: const EdgeInsets.all(16),
  29.             crossAxisSpacing: 10,
  30.             mainAxisSpacing: 10,
  31.             maxCrossAxisExtent: 200.0,
  32.             children: <Widget>[
  33.               Container(
  34.                 padding: const EdgeInsets.all(8),
  35.                 child: const Text(‘First’, style: TextStyle(fontSize: 20)),
  36.                 color: Colors.yellow,
  37.               ),
  38.               Container(
  39.                 padding: const EdgeInsets.all(8),
  40.                 child: const Text(‘Second’, style: TextStyle(fontSize: 20)),
  41.                 color: Colors.blue,
  42.               ),
  43.               Container(
  44.                 padding: const EdgeInsets.all(8),
  45.                 child: const Text(‘Third’, style: TextStyle(fontSize: 20)),
  46.                 color: Colors.blue,
  47.               ),
  48.               Container(
  49.                 padding: const EdgeInsets.all(8),
  50.                 child: const Text(‘Four’, style: TextStyle(fontSize: 20)),
  51.                 color: Colors.yellow,
  52.               ),
  53.               Container(
  54.                 padding: const EdgeInsets.all(8),
  55.                 child: const Text(‘Fifth’, style: TextStyle(fontSize: 20)),
  56.                 color: Colors.yellow,
  57.               ),
  58.               Container(
  59.                 padding: const EdgeInsets.all(8),
  60.                 child: const Text(‘Six’, style: TextStyle(fontSize: 20)),
  61.                 color: Colors.blue,
  62.               ),
  63.             ],
  64.           )),
  65.     );
  66.   }
  67. }

Output

From Last Lecture

just replace the list view code with the following grid view code with additional decoration

  • Share:
author avatar
saqib

Previous post

Json Mapping-Data Class Generator-ProgressIndicator
December 29, 2022

Next post

Round Robin Scheduling Algorithm in Operating system
January 2, 2023

You may also like

Beautiful UI design in flutter
5 January, 2023

Beautiful UI design in flutter In project home page import velocity and put the previous lecture list with the following properties import ‘package:flutter/material.dart’; import ‘package:flutter/services.dart’; import ‘dart:convert’; import ‘package:flutter_catalog/models/catalog.dart’; import ‘package:flutter_catalog/widgets/drawer.dart’; import ‘package:flutter_catalog/widgets/item_widget.dart’; import ‘package:flutter_catalog/widgets/themes.dart’; import ‘package:velocity_x/velocity_x.dart’; class HomePage extends …

Json Mapping-Data Class Generator-ProgressIndicator
20 December, 2022

Json Mapping-Data Class Generator-Progress Indicator in Flutter if we have to get data in json formate than we first have to map the data in catalog.dart file like this import ‘dart:convert’; class CatalogModel { static List<Item> items; } class Item …

List view Builder in Flutter
20 December, 2022

List view Builder in Flutter The first basic step is to import the required material design package. Let’s import it. import ‘package:flutter/material.dart’; 2. Create Custom Data Class In this step, I will create a Company Stocks class that has names …

Leave A Reply Cancel reply

You must be logged in to post a comment.

admin@cuitutorial.com
Facebook-f Twitter Youtube Linkedin Instagram Stack-overflow Pinterest Github Quora Whatsapp
Courses
  • All Courses
  • Past Paper
  • Final year projects
  • Interview Questions
  • Contact
Important Pages
  • Privacy Policy
  • Terms of Service
  • Cookie Policy
Links
  • University Events
  • Team
Education & learning platform for All Computer science subjects
Final year projects
Past Paper
Interview questions
Programming, C/C++, Asp.net/MVC. Android, MySql, Jquery, Ajax, javascript, Php, Html5, Bootstrap4.
NTS, GAT, PPSC, FPSC

Copyright © 2021 | Cuitutorial