Education
Software Development Executive - I
Software Development Executive - II
Last updated on Sep 15, 2023
Last updated on Aug 21, 2023
In the world of mobile apps, the user interface plays a significant role in attracting and retaining users. Flutter, a widget-based framework, offers an extensive range of widgets to create visually appealing and highly functional interfaces. Among these widgets, lists, and grids in Flutter are fundamental components that every Flutter developer should master.
Lists and grids are used to display data in an organized manner. They provide a visual structure to the data, making it easier for users to understand and interact with the app. Whether you want to display only a few items or a large set of data, lists, and grids are the go-to widgets in Flutter.
In this blog post, we will delve into the details of how to create and customize lists and grids in Flutter. We will also discuss some common issues that you might encounter and how to solve them. By the end of this post, you will have a solid understanding of how to effectively use lists and grids in your Flutter apps.
Flutter is loved by developers worldwide for several reasons. It allows developers to create beautiful, natively compiled applications for mobile, web, and desktop from a single codebase. This means you can use the same code to create an app for different platforms, which saves a lot of time and effort.
Moreover, Flutter provides a rich set of pre-designed widgets that are customizable and extensible. This makes it easier for developers to create complex UIs. The hot-reload feature in Flutter allows developers to experiment, build UIs, add features, and fix bugs faster.
In Flutter, a list is a scrollable arrangement of widgets that are lined up on the main axis. The main axis in Flutter can be either horizontal or vertical. Lists can be used to display a large number of similar items, like a list of messages in a messaging app or a list of articles in a news app.
Lists in Flutter are created using the ListView widget. The ListView widget supports both a horizontal and vertical arrangement of children widgets.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter List View'), 8 ), 9 body: ListView( 10 children: <Widget>[ 11 ListTile( 12 leading: Icon(Icons.map), 13 title: Text('Map'), 14 ), 15 ListTile( 16 leading: Icon(Icons.photo_album), 17 title: Text('Album'), 18 ), 19 ListTile( 20 leading: Icon(Icons.phone), 21 title: Text('Phone'), 22 ), 23 ], 24 ), 25 ), 26 ); 27 } 28 } 29 30 void main() { 31 runApp(const MyApp()); 32 } 33
In the above code, we have created a simple list using the ListView widget. The list contains three items, each represented by a ListTile widget. Each ListTile contains an icon and a text widget.
Grids in Flutter are similar to lists, but instead of arranging widgets linearly, grids arrange widgets in a two-dimensional array (rows and columns). Grids are useful when you need to display a collection of items that are best represented visually, like a gallery of images.
In Flutter, grids are created using the GridView widget. The GridView widget supports both a fixed number of tiles in the cross-axis and a maximum cross-axis extent.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter Grid View'), 8 ), 9 body: GridView.count( 10 crossAxisCount: 2, 11 children: List.generate(100, (index) { 12 return Center( 13 child: Text( 14 'Item $index', 15 style: Theme.of(context).textTheme.headline5, 16 ), 17 ); 18 }), 19 ), 20 ), 21 ); 22 } 23 } 24 25 void main() { 26 runApp(const MyApp()); 27 } 28
In the above code, we have created a simple grid using the GridView.count constructor. The grid contains 100 items, each represented by a Center widget containing a Text widget. The crossAxisCount property specifies the number of columns in the grid.
Creating lists in Flutter is straightforward. The ListView widget is used to create a scrollable list of items. The simplest way to create a list is by providing a list of children to the ListView widget.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter List View'), 8 ), 9 body: ListView( 10 children: <Widget>[ 11 ListTile( 12 leading: Icon(Icons.map), 13 title: Text('Map'), 14 ), 15 ListTile( 16 leading: Icon(Icons.photo_album), 17 title: Text('Album'), 18 ), 19 ListTile( 20 leading: Icon(Icons.phone), 21 title: Text('Phone'), 22 ), 23 ], 24 ), 25 ), 26 ); 27 } 28 } 29 30 void main() { 31 runApp(const MyApp()); 32 } 33
In the above code, we have created a simple list using the ListView widget. The list contains three items, each represented by a ListTile widget. Each ListTile contains an icon and a text widget.
Flutter provides a lot of flexibility in customizing lists. You can change the scroll direction, item extent, padding, and much more. You can also create a horizontal list by setting the scroll direction to Axis.horizontal.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter Horizontal List View'), 8 ), 9 body: ListView( 10 scrollDirection: Axis.horizontal, 11 children: <Widget>[ 12 Container( 13 width: 160.0, 14 color: Colors.red, 15 ), 16 Container( 17 width: 160.0, 18 color: Colors.blue, 19 ), 20 Container( 21 width: 160.0, 22 color: Colors.green, 23 ), 24 Container( 25 width: 160.0, 26 color: Colors.yellow, 27 ), 28 Container( 29 width: 160.0, 30 color: Colors.orange, 31 ), 32 ], 33 ), 34 ), 35 ); 36 } 37 } 38 39 void main() { 40 runApp(const MyApp()); 41 } 42
In the above code, we have created a horizontal list using the ListView widget. The list contains five items, each represented by a Container widget with a different color.
While working with lists in Flutter, you might encounter a few issues. One common issue is handling large lists where only a few items are visible on the screen. In such cases, it's inefficient to build all items at once.
To solve this issue, Flutter provides the ListView.builder constructor. It works in a similar way to the ListView widget, but instead of taking a list of children, it takes a builder function. This function is called only for those items that are actually visible.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter List View Builder'), 8 ), 9 body: ListView.builder( 10 itemCount: 100, 11 itemBuilder: (BuildContext context, int index) { 12 return ListTile( 13 title: Text('Item $index'), 14 ); 15 }, 16 ), 17 ), 18 ); 19 } 20 } 21 22 void main() { 23 runApp(const MyApp()); 24 } 25
In the above code, we have created a list with 100 items using the ListView.builder constructor. The builder function takes two parameters: BuildContext and the current index. It returns a ListTile for each item.
Creating grids in Flutter is similar to creating lists. The GridView widget is used to create a scrollable grid of items. The simplest way to create a grid is by providing a list of children to the GridView widget.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter Grid View'), 8 ), 9 body: GridView.count( 10 crossAxisCount: 2, 11 children: List.generate(100, (index) { 12 return Center( 13 child: Text( 14 'Item $index', 15 style: Theme.of(context).textTheme.headline5, 16 ), 17 ); 18 }), 19 ), 20 ), 21 ); 22 } 23 } 24 25 void main() { 26 runApp(const MyApp()); 27 } 28
In the above code, we have created a simple grid using the GridView.count constructor. The grid contains 100 items, each represented by a Center widget containing a Text widget. The crossAxisCount property specifies the number of columns in the grid.
Flutter provides a lot of flexibility in customizing grids. You can change the cross-axis count, main-axis spacing, cross-axis spacing, child aspect ratio, and much more.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter Custom Grid View'), 8 ), 9 body: GridView.count( 10 crossAxisCount: 2, 11 crossAxisSpacing: 10, 12 mainAxisSpacing: 10, 13 childAspectRatio: 3/2, 14 children: List.generate(100, (index) { 15 return Container( 16 padding: const EdgeInsets.all(8), 17 child: const Text('Item'), 18 color: Colors.teal[100], 19 ); 20 }), 21 ), 22 ), 23 ); 24 } 25 } 26 27 void main() { 28 runApp(const MyApp()); 29 } 30
In the above code, we have created a custom grid using the GridView.count constructor. The grid contains 100 items, each represented by a Container widget. The crossAxisCount property specifies the number of columns in the grid. The crossAxisSpacing and mainAxisSpacing properties control the spacing between the grid items. The childAspectRatio property controls the aspect ratio of the grid items.
While working with grids in Flutter, you might encounter a few issues. One common issue is handling large grids where only a few items are visible on the screen. In such cases, it's inefficient to build all items at once.
To solve this issue, Flutter provides the GridView.builder constructor. It works in a similar way to the GridView widget, but instead of taking a list of children, it takes a builder function. This function is called only for those items that are actually visible.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter Grid View Builder'), 8 ), 9 body: GridView.builder( 10 gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( 11 crossAxisCount: 2, 12 ), 13 itemCount: 100, 14 itemBuilder: (BuildContext context, int index) { 15 return Container( 16 padding: const EdgeInsets.all(8), 17 child: const Text('Item'), 18 color: Colors.teal[100], 19 ); 20 }, 21 ), 22 ), 23 ); 24 } 25 } 26 27 void main() { 28 runApp(const MyApp()); 29 } 30
In the above code, we have created a grid with 100 items using the GridView.builder constructor. The builder function takes two parameters: BuildContext and the current index. It returns a Container for each item. The gridDelegate property controls the layout of the grid items.
Infinite scrolling is a popular technique where data loads continuously as users scroll down the page, eliminating the need for pagination. It provides a seamless experience for users and is particularly useful when displaying a large amount of data.
In Flutter, you can implement infinite scrolling in both lists and grids using the ListView.builder and GridView.builder constructors.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter Infinite Scrolling List'), 8 ), 9 body: ListView.builder( 10 itemBuilder: (BuildContext context, int index) { 11 return ListTile( 12 title: Text('Item $index'), 13 ); 14 }, 15 ), 16 ), 17 ); 18 } 19 } 20 21 void main() { 22 runApp(const MyApp()); 23 } 24
In the above code, we have created an infinite scrolling list using the ListView.builder constructor. The builder function takes two parameters: BuildContext and the current index. It returns a ListTile for each item. As the user scrolls down, more items are loaded automatically.
Dynamic loading of content, also known as lazy loading, is a technique where content is loaded when it is needed or when it becomes visible. This can significantly improve performance for large lists or grids.
In Flutter, you can implement dynamic loading of content in both lists and grids using the ListView.builder and GridView.builder constructors.
1 class MyApp extends StatelessWidget { 2 @override 3 Widget build(BuildContext context) { 4 return MaterialApp( 5 home: Scaffold( 6 appBar: AppBar( 7 title: const Text('Flutter Dynamic Loading Grid'), 8 ), 9 body: GridView.builder( 10 gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( 11 crossAxisCount: 2, 12 ), 13 itemBuilder: (BuildContext context, int index) { 14 return Container( 15 padding: const EdgeInsets.all(8), 16 child: Text('Item $index'), 17 color: Colors.teal[100], 18 ); 19 }, 20 ), 21 ), 22 ); 23 } 24 } 25 26 void main() { 27 runApp(const MyApp()); 28 } 29
In the above code, we have created a grid with dynamic loading using the GridView.builder constructor. The builder function takes two parameters: BuildContext and the current index. It returns a Container for each item. As the user scrolls down, more items are loaded automatically.
In conclusion, mastering lists and grids in Flutter is crucial for any developer aiming to build efficient and visually appealing mobile applications. These widgets not only provide a structured way to display data but also offer extensive customization options to cater to various design requirements. With the ability to handle large data sets efficiently through techniques like infinite scrolling and dynamic loading, Flutter ensures optimal performance for your apps. By understanding and implementing these concepts, you can significantly enhance the user experience and functionality of your Flutter applications.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.