How to Add Header Row to a ListView in Flutter?
ListView 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?
How to Add 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:
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override void initState() { // TODO: implement initState super.initState(); } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( title: Text("Demo App1"), ), body: Column( children: <Widget>[ Container( height: 40.0, child: Row( children: <Widget>[ Container( padding: EdgeInsets.all(4.0), width: 100.0, child: Text( "Name", style: TextStyle(fontSize: 18), )), Container( padding: EdgeInsets.all(4.0), width: 100.0, child: Text( "Age", style: TextStyle(fontSize: 18), )), ], ), ), Expanded( child: ListView.builder( itemCount: 100, itemBuilder: (BuildContext context, int index) { return Row( children: <Widget>[ Container( padding: EdgeInsets.all(4.0), width: 100.0, child: Text( "Name $index", style: TextStyle(fontSize: 18), )), Container( padding: EdgeInsets.all(4.0), width: 100.0, child: Text( "Age $index", style: TextStyle(fontSize: 18), ), ) ], ); }, ), ), ], ), ), ); } }
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: <Widget>[ Header(), rowContent(index), ], ); } else { return rowContent(index); } }, )
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.