Flutter Development (Day -07):
- All source code : Click Here
- Today we are going to talk more about lists and its types.
- Lists are very important in any field like if we want to return large amount of data we can't assing every single value to a String variable and display it is a tedious task to do , but Flutter makes it easy for us just wrap the list in a ListView widget then it is ready.
Types of Lists.
1. Horizontal List
2. Vertical List
3. Grid List
Horizontal List:
- Same as returning the ListView in previous tutorials just add a property scrollDirection:Axis.horizontal.
Vertical List:
- We already discussed in our previous tutorials.
Grid List:
- Sometimes you might want to display your items as a grid rather than a normal list of items that come one after the next. For this task, use the GridView widget.
Ex.
import 'Package:flutter/material.dart';
import 'package:random_color/random_color.dart';
class WorkingWithLists extends StatefulWidget {
@override
_WorkingWithListsState createState() => _WorkingWithListsState();
}
class _WorkingWithListsState extends State<WorkingWithLists> {
RandomColor randomColor = RandomColor();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Working with Lists"),
centerTitle: true,
backgroundColor: RandomColor().randomColor(),
),
body: GridView(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
scrollDirection: Axis.vertical,
children: <Widget>[
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
_showGrid(),
],
),
);
}
Widget _showGrid() {
return Container(
margin: const EdgeInsets.all(2.0),
color: RandomColor().randomColor(),
child: Center(
child: Text(
"Grid",
style: TextStyle(
color: Colors.white, fontSize: 30.0, fontWeight: FontWeight.bold),
),
),
);
}
}
- Here I used some extra stuff like returning a function instead typing again and again same code.
- Used random_color: ^1.0.3 package to generate random colors.
- Grid view is also a type of Scrollable widget.It requires a crossAxisCount which is nothing but it returns two 2 columns change the number and scrollDirection and see the magic.
- For more info on any other widgets visit here.
Comments
Post a Comment