How to Create Expanding and Collapsing Header in Flutter

Expanding and collapsing headers look beautiful when you have a list of things which the user scroll through. Unlike other mobile app development platforms, creating expanding and collapsing header in flutter is easy.

We need the help of multiple widgets to create the header. As we are trying to create a scrolling effect we definitely need CustomScrollView widget. We need SliverAppBar widget which acts as the header of the list. FlexibleSpaceBar widget defines the area which expands, stretches and collapses. The SliverList widget constitutes the remaining portion of scroll view by providing children in a linear array.

Go through the following Flutter example code for more understanding.

import 'package:flutter/material.dart';

class ScrollingHeaderExample extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Expanding & Collapsing Header',
      home: MainScreen()

/// This is the stateless widget that the main application instantiates.
class MainScreen extends StatelessWidget {
  final data=['One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten'];
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
  slivers: <Widget>[
      pinned: true,
      expandedHeight: 250.0,
      flexibleSpace: FlexibleSpaceBar(
        title: Text('Collapsing Header'),
                      fit: BoxFit.cover,
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return Container(
            height: 100,
            child: Text('${data[index]}'),
         childCount: data.length,

And following is the output of this Flutter tutorial.

I hope this flutter tutorial will help you to create beautiful user interface.

Leave a Comment

Your email address will not be published.