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;
}
}
flutter NestedScrollView头部布局高度
于 2024-10-09 16:31:08 首次发布