Flutter listview builder with header
Your comment on this question: Show
Your comment on this answer:
Related Questions In Others
Contents You might need to create lists that display different types of content. For example, you might be working on a list that shows a heading followed by a few items related to the heading, followed by another heading, and so on. Here’s how you can create such a structure with Flutter:
1. Create a data source with different types of itemsTypes of itemsTo represent different types of items in a list, define a class for each type of item. In this example, create an app that shows a header followed by five messages. Therefore, create three classes: ListItem, HeadingItem, and MessageItem. Create a list of itemsMost of the time, you would fetch data from the internet or a local database and convert that data into a list of items. For this example, generate a list of items to work with. The list contains a header followed by five messages. Each message has one of 3 types: ListItem, HeadingItem, or MessageItem. To convert each item into a widget, use the ListView.builder() constructor. In general, provide a builder function that checks for what type of item you’re dealing with, and returns the appropriate widget for that type of item. Interactive exampleimport 'package:flutter/material.dart'; void main() { runApp( MyApp( items: ListListView Widget has been introduced to reduce the overload of having various layouts performing the same task. So in this article, we will learn about How to Add a Header Row to a ListView in Flutter? Return the header as the first row by itemBuilder. Consider a Code snippet as below: ListView.builder( itemCount: data == null ? 1 : data.length + 1, itemBuilder: (BuildContext context, int index) { if (index == 0) { // return the header return new Column(...); } index -= 1; // return row var row = data[index]; return new InkWell(... with row ...); }, );Users can add Container Widget and ListView Widget in Column Widget. So the code snippet will look like this below: You can add a column to the first item in the item list like this. new ListView.builder( itemCount: litems.length, itemBuilder: (BuildContext ctxt, int index) { if (index == 0) { return Column( children:You could use the listview_utils package to append header and footer to ListView easily, like that: Import package like below: import 'package:listview_utils/listview_utils.dart';CustomListView( header: Container( child: Text('Header'), ), itemCount: items.length, itemBuilder: (BuildContext context, int index, _) { return ListTile( title: Text(item['title']), ); }, );Conclusion:In this article, we have been through How to Add Header Row to a ListView in Flutter? Keep Learning!! Keep Fluttering !!! And also, check out this article on the listview to column in the flutter… Still, confused about something on flutter development?? Do let us know… FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc. FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter. |