Lab Manual GridView
Lab Manual GridView
(Grid View)
Elements (Widgets) in Tabular Format
Automatically Scrollable
Note: We can design the mentioned design by using Rows & Columns but it is easy and
professional way to design using GridView.
Example:
Types of GridView:
GridView.count()
GridView.extent()
GridView.builder()
1) GridView.count()
CrossAxisCount is mandatory in GridView.count() (which means that
max number of items in one row)
Children are also part of GridView.count()
Code:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
@override
Widget build(BuildContext context) {
return Scaffold(
body:
GridView.count(crossAxisCount: 3,
children: [
Container(color:
Colors.amberAccent,),
Container(color:
Colors.black12,),
Container(color:
Colors.blue,),
Container(color:
Colors.orange,),
],
),
);
}
}
mainAxisSpacing ( spacing between row )
crossAxisSpacing ( spacing between column)
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor:
Colors.deepPurple),
useMaterial3: true,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
body: GridView.count(
crossAxisCount: 3,
mainAxisSpacing: 20,
crossAxisSpacing: 20,
children: [
Container(color: Colors.amberAccent,),
Container(color: Colors.black12,),
Container(color: Colors.blue,),
Container(color: Colors.orange,),
],
),
);
}
}
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
)
);
}
}
3) GridView.builder()
gridDelegate:
it is used to define the nature of the GridView..
Like ? is the grid is based on the size, or grid is based on the
total number of elements in one row etc
itemBuilder:
it uses (context,index) so that the index will use to
traverse each data from database and context is the current
state of the application.
itemCount:
it is used to define how many times the widgets(item)
will generate itself.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
Explore:
SliverGridDelegateWithMaxCrossAxisExtent
Is used to define the size of each item,
We provide maxCrossAxisExtent which tells builder to make
the item of that size.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(