2

I have a view in my flutter mobile app in which there is:

  1. A top bar that is a sliver bar (title + tabs) [red box on image]
  2. A scrollable list of data [green box on image]

When i scroll, the tabs disappear leaving only the page title.

Goal:

Intended goal

I want to add a header [blue box on image] that does not disappear when i scroll and stays between the sliver bar and the scrollable items.

I seem to only be able to put the header inside the title of the sliver bar above the tabs, or in the scrollable list in which case it disappears as i scroll

1
  • you can use SliverToBoxAdapter widget also Commented Apr 28, 2020 at 4:30

2 Answers 2

6

You can easily implement this on your own. The widget you will need is called SliverPersistentHeader with pinned parameter equal to true and custom implementation of SliverPersistentHeaderDelegate.

Take a look at this example:

https://dartpad.dartlang.org/6e0f6b4a212e572c7b74e5f301abc71b

enter image description here

import 'dart:math' as math;

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: CustomScrollView(
        slivers: <Widget>[
          SliverPersistentHeader(
            floating: true,
            delegate: SliverAppBarDelegate(
              minHeight: 60,
              maxHeight: 60,
              child: Container(
                color: Colors.red,
                child: Center(
                  child: Text('This will hide'),
                ),
              ),
            ),
          ),
          SliverPersistentHeader(
            pinned: true,
            delegate: SliverAppBarDelegate(
              minHeight: 60.0,
              maxHeight: 60.0,
              child: Container(
                color: Theme.of(context).scaffoldBackgroundColor,
                child: Column(
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 16.0,
                        vertical: 8.0,
                      ),
                      child: Text('This will remain visible',
                          style: TextStyle(fontSize: 20)),
                    ),
                    Divider(),
                  ],
                ),
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate((context, index) {
              return ListTile(
                title: Text('List Tile $index'),
              );
            }, childCount: 100),
          ),
        ],
      ),
    );
  }
}

class SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  SliverAppBarDelegate({
    required this.minHeight,
    required this.maxHeight,
    required this.child,
  });
  final double minHeight;
  final double maxHeight;
  final Widget child;

  @override
  double get minExtent => minHeight;
  @override
  double get maxExtent => math.max(maxHeight, minHeight);
  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return SizedBox.expand(child: child);
  }

  @override
  bool shouldRebuild(SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}

2
  • 3
    A long time ago now, but such a useful answer, thank you! It never occurred to me that you could put two SliverPersistentHeaders together :) Commented Aug 3, 2023 at 21:48
  • 2
    Thanks, just updated the snippet to use modern Flutter syntax Commented Aug 18, 2023 at 18:26
1

Ended up using a sticky header: https://pub.dev/packages/flutter_sticky_header And keep the same header without changing it through the scroll

Not the answer you're looking for? Browse other questions tagged or ask your own question.