Flutter Framework Grids, Lists, and Data Sources


Practically every mobile app now is expected to have placed in its layouts using a scroll-able list is often the most approach to display a large character of similar items on a small screen. Flutter framework extends several widgets you can use to efficiently at a minimal code, to create and display such lists.

Showing a Non-Scroll-able List

If you want to present a miniature number of related items, and the user’s screen will be able to serve all of them at the same time, using a column widget is the most effective thing to implement. To construct a list in your Flutter app, you require to use the List class that the Dart code language provided. After constructing the list, you need to use its add() method to combine any number of items to it. The code below shows how to create a list containing RaisedButton Wiget: 

Do Note that individual object in the list has an empty onPressed event handler connected with it because, externally, the item would be disabled. Then you can directly assign it to the children section of the Column widget to be displayed. however, you’ll also want to define where the list items should be set on the screen, you can constrain the positions of the items along its main axis and cross axis using the mainAxisAlignment & crossAxisAlignment properties. By default, for the Column widget, the main axis is the vertical axis, and the cross axis is the horizontal axis. The following code below shows you how to stretch the three buttons across the horizontal axis and place them in the center of the screen vertically.

Shows a Simple Scrollable List

For somewhat larger lists, a list whose contents are likely to overflow to the screen, you must consider using a ListView widget because it will support the scrolling. As you may have noticed that the code above to create a list was both lengthy and monotonous. however, creating a larger list using the same approach can be very tedious. An easy alternative approach is to use two lists instead: one containing the data, and one containing the widgets.

Here’s how you can use the [ ] operator to instantly produce a data list, which, for now, only includes several strings.

To transform the above list of strings into a list of RaisedButton widgets, you can use the map() & toList() methods. With the map() method, then you can generate a new RaisedButton widget. And with the toList() method, you can also transform the Iterable object returned by the map() into an actual List object.

As you can see in the above code the code shows you how to create an onPressed event handler that uses canLaunch() & launch() methods provided by the url_launcher package to launch the website. However ones the list is ready, you can pass it to the property children of the ListView widget.

Constructing the Grid

ListView widget enables you to assign only one item on its cross axis, this item will be stretched to fill all the space available on the axis. if you want to make it more flexible, you can use GridView widget which enables you to specify how many items you want on the cross axis to be placed. the following code below uses a GridView.count() constructor to create a GridView widget that will show two items in a row.

Showing Large Lists

Implementing a large quality of list items, you should consider avoiding generating widget lists manually, the method you did earlier, please do note that a large list of widget s can consume a lot of memory. Instead, you should consider using an indexedWidgetBuilder function, which lets you generate widgets only when the user interacts with.
This technique can easily generate a widget as the user scroll through the list. usually, you would be fetching data from a remote server.

MyState class specified in the above code doesn’t exist yet, you need to create & override its build() method.

The next implementation is, to add a List object as one of the member variables of the class we will be used to store the list of items listed from https://jsonplaceholder.typicode.com/users API endpoint as a variable.

To override the iniState() method and get a call to a new async method named loadData(). The iniState() method is to download automatically when the widget is initializing.

Within the loadData() method, we can do the get() function of DART http package to get the data from the API. The API endpoint will return a JSON data which is we can parse by using the json.decode() function available in DART.

When the JSON data converted into an object we can use the value correlated with its items key to initialize the variables. however, it is part of the widget state. accordingly, we need to make sure that the update onto it’s inside the setState() method.

In this case, we can now create a new ListView widget utilizing the ListView.builder() constructor, which is to assume an IndexedWidgetBuilder function along with item count as the arguments. at this time, out item count is nothing but data list. however, you need to add the following code within the build() method of the MyState class.

Within the builder function, you need to build a widget tree to show various details about the list you get from the API. Flutter’s material package offers you a widget named ListTile, which allows you to create a tree by following the Material Design guidelines. The below code shows us to display the user list data properties of the ListTile widget.

Code a new Scaffold widget specify to the ListView widget to its property then return it from the build() method so it can be used with the MaterialApp widget, Please do note you can also add an AppBar widget to the Scaffold widget.

To read more about Flutter widget you can refer to the official document at this link.


Leave a Reply

Your email address will not be published. Required fields are marked *