flutter NestedScrollView头部布局高度

NestedScrollView(
        physics: const NeverScrollableScrollPhysics(),
        headerSliverBuilder: (BuildContext context,bool b){
        //  return [initSliverPersistentHeader()];
          return [_headView()];
        },
        body: _tabBarView(),
      )

//SliverPersistentHeader无法包裹自适应子布局的高度,需要设置固定的最大和最小高度
initSliverPersistentHeader() {
    return SliverPersistentHeader(
      //是否固定头布局 默认false(不固定头部时,会隐藏)
        pinned: false,
        //是否浮动 默认false
        floating: false,
        //必传参数,头布局内容
        delegate: MySliverDelegate(
          //缩小后的布局高度
          minHeight: Utils().screenHeight(context) * 1.5,
          //展开后的高度
          maxHeight: Utils().screenHeight(context) * 2,
          child: Container(
              color: Colors.white,
              child: ListView(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                children: <Widget>[
                  _marketBanner(),
                  _navView(),
                  MarketPromoExpBanner(),
                  MarketPushShareListView(),
                  MarketOperationBanner(
                      controller: _swiperController,
                      bannerList: _recommendModel.operationBannerList),
                   _tabBar(),
                ],
              )),
        ));
  }

//SliverToBoxAdapter能够包裹自适应子布局的高度
_headView(){
    return SliverToBoxAdapter(
      child: ListView(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        children: <Widget>[
          _marketBanner(),
          _navView(),
          MarketPromoExpBanner(),
          MarketPushShareListView(),
          MarketOperationBanner(
              controller: _swiperController,
              bannerList: _recommendModel.operationBannerList),
          _tabBar(),
        ],
      )
    );
  }



class MySliverDelegate extends SliverPersistentHeaderDelegate {
  MySliverDelegate({
     this.minHeight,
     this.maxHeight,
     this.child,
  });
  final double minHeight; //最小高度
  final double maxHeight; //最大高度
  final Widget child; //子Widget布局

  
  double get minExtent => minHeight;

  
  double get maxExtent => max(maxHeight, minHeight);

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

   //是否需要重建
  bool shouldRebuild(MySliverDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值