This is the eighth lesson of the Flutter series. Today we are going to learn on adding list in Flutter UI.
First in the home let us delete the Home() class. Now let us define a function that creates list view. Define the variable ListView that equals to the ListView constructor. As a parameter you have to define the children of ListView. That means what are the widgets that you want as list items in the list view. Most commonly used list widget for list view is “ListTile”. In the first parameter we can define leading icon. There are predefined icons and we can use those icons here. Then define the title as a text. We can define a subtitle too. We can also define trailing icon.
In the end of this function we can return list view.
Then we can call this function within main method through scaffold widget. Within scaffold widget we can use body as the listview.
Now this ListTile contains few properties such as leading icon (icon in the left), title, subtitle, trailing (icon in the right).
This is the output that you will get.
As the same that we used ListTile we can use more ListTiles. In listView property children is an array of widgets.
var listView = ListView(
But it is not mandatory to use list tile within our listView. We can use any of the widgets there.
Here also we can add appBar to give more look. Now you can see the items of the list is not clickable. We can add onTap method here for the first icon and let us use debugPrint. debugPrint means when use tap on the icon you can print something in your console.
You can see the following output.
I/flutter (23574): Tapped
·List view is not suitable for large number of items since it is not memory efficient.
· Wrap ListView as “home” of Scaffold widget as it is scrollable and might overflow beyond the screen.
Hope you got some understanding on ListView in flutter. See you in another article. Happy learning!!!