user1506104
user1506104

Reputation: 7086

Flutter: Cannot set the preferred height of SliverPersistentHeader with TabBar

I am trying to make my TabBar's height a little bigger. It only works up to a certain value. Here is my SliverPersistentHeader:

SliverPersistentHeader(
  pinned: true,
  delegate: _SliverAppBarTabDelegate(
    child: PreferredSize(
      preferredSize: Size.fromHeight(45.0),
      child: Container(
        color: Colors.white,
        child: TabBar(
          isScrollable: true,
          labelColor: Colors.black,
          unselectedLabelColor: Colors.black38,
          indicatorColor: Theme.of(context).primaryColor,
          tabs: createTabList(),
          controller: tabBarController,
        ),
      ),
    ),
  ),
),

And this is my delegate:

class _SliverAppBarTabDelegate extends SliverPersistentHeaderDelegate {
  final PreferredSize child;

  _SliverAppBarTabDelegate({this.child});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return child;
  }

  @override
  double get maxExtent => child.preferredSize.height;

  @override
  double get minExtent => child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}

Why it does not allow me to set the size to a bigger value?

EDIT: Here is the overall structure of my stateful widget:

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: CustomScrollView(
      slivers: <Widget>[
        SliverAppBar(),
        SliverPersistentHeader(),
        SliverList(),
      ],
    ),
  );
}

Upvotes: 2

Views: 3994

Answers (1)

Mobina
Mobina

Reputation: 7109

Wrap the _SliverAppBarTabDelegate's child with a SizedBox.expand:

class _SliverAppBarTabDelegate extends SliverPersistentHeaderDelegate {
  final PreferredSize child;

  _SliverAppBarTabDelegate({this.child});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return SizedBox.expand(child: child);
  }

  @override
  double get maxExtent => child.preferredSize.height;

  @override
  double get minExtent => child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}

Upvotes: 2

Related Questions