Search code examples
flutterflutter-sliverappbar

How to override the minimum height in SliverAppBar


I am having a problem where I can't decrease the SliverAppBar as I need. I found out that it has some kind of minHeight constraint that has to deal with some animation and layout stuff. I have a TextButton.icon and some Text in it which don't take up much space. I need the behavior of the SliverAppBar of floating. Is there a way to decrease the height freely without any constraints?


Solution

  • You can use SliverToBoxAdapter and then use a Sizedbox or for more flexibility you can use SliverPersistentHeader.

    class MySliverAppBarDelegant extends SliverPersistentHeaderDelegate {
      @override
      Widget build(
          BuildContext context, double shrinkOffset, bool overlapsContent) {
        return Container();
      }
    
      @override
      double get maxExtent => 40; //use same for fixed height
      @override
      double get minExtent => 30;
    
      @override
      bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) =>
          false;
    }
    

    And use like

    SliverPersistentHeader(
      delegate: MySliverAppBarDelegant(),
      pinned: true,
    )