SliverHeader behaviour management


My flutter app has a NestedScrollView with a headerSliverBuilder.

My SliverAppBar inside this builder is like this:

    elevation: 0,
    snap: true,
    pinned: false,
    floating: true,
    forceElevated: false,
    primary: false,
    automaticallyImplyLeading: false,
    backgroundColor: Colors.white,
    expandedHeight: 65.0 + 75,
    flexibleSpace: Container(
      child: Column(children: <Widget>[
        MyTabBar(true, 65, () {}, () {}),
        Container(height: 75, color:
      decoration: BoxDecoration(boxShadow: [
          color: Colors.black26,
          blurRadius: 4.0,
          spreadRadius: .0,
      ], color: Colors.white),

Now, when scrolling the behaviour is kind of weird. I’d like it to scroll away the whole SliverAppBar including everything in flexibleSpace. But this is what happens:

enter image description here

It scrolls underneath the Orange part of my header and then scrolls away the tab-part above. And the moment the tab-part disappears, the orange part vanishes too. Huhh..

And the same when I get the bar back from scrolling down. Then the orange part just appears on the spot and the top part slides in. I’d like to have it all sliding in.

The whole behavior changed for me since the last flutter update.

Does anyone know what I need to do different to achieve my expected behavior?

Here is my full code of this simple example:


How about using AnimatedContainer inside a Stack widget?

Something like this…

import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart' hide Colors;

void main() {

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    final ScrollController scrollController = ScrollController();

    return MaterialApp(
      theme: ThemeData(
      home: Scaffold(
        body: Stack(
          children: <Widget>[
              child: ListView(
                controller: scrollController,
                padding: const EdgeInsets.only(top: 100),
                children: List.generate(
                  (index) => ListTile(
                    title: Text('This is item #$index'),
              scrollController: scrollController,
              child: PreferredSize(
                preferredSize: Size.fromHeight(100),
                child: const Center(child: Text('APP BAR')),

class AnimatedAppBar extends StatefulWidget {
    required this.scrollController,

  final ScrollController scrollController;
  final PreferredSize? child;
  final double? toolbarHeight;

  _AnimatedAppBarState createState() => _AnimatedAppBarState();

class _AnimatedAppBarState extends State<AnimatedAppBar> {
  late double _totalHeight;
  late double _lastPosition;
  late bool _isShown;

  void initState() {

    _totalHeight = widget.toolbarHeight ??
        kToolbarHeight + (widget.child?.preferredSize.height ?? 0.0);
    _lastPosition = widget.scrollController.offset;
    _isShown = true;


  void _scrollListener() {
    if (_isShown && _lastPosition < widget.scrollController.offset) {
      setState(() => _isShown = false);
    } else if (!_isShown && _lastPosition > widget.scrollController.offset) {
      setState(() => _isShown = true);
    _lastPosition = widget.scrollController.offset;

  Widget build(BuildContext context) {
    return AnimatedContainer(
      width: double.infinity,
      height: _totalHeight,
      padding: EdgeInsets.only(top: kToolbarHeight),
      duration: const Duration(milliseconds: 250),
      curve: Curves.easeIn,
      transform: Matrix4.translation(
        Vector3(0, _isShown ? 0 : -_totalHeight, 0),
      color: Theme.of(context).primaryColor,
      child: widget.child,

  void dispose() {

Answered By – rickimaru

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *