NestedScrollView+TabBar+TabBarView实现折叠顶部布局


一、概述

NestedScrollView+TabBar+TabBarView实现TabBar滑动吸附在顶部。

2019052420431296.gif


二、开始编码
本次代码以NestedScrollView(当然也可以用CustomScrollView实现,实现方法基本一致)的headerSliverBuilder实现滑动的顶部布局,其中SliverPersistentHeader实现吸附在顶部的TabBar。body中放TabBarView实现页面切换。(flutter的滑动家族还是值得好好学习一番的。此次代码是根据官网提供的例子而改写的)
以下代码注释还是挺详细的,

@override
  Widget build(BuildContext context) {
    double statusBarHeight = MediaQuery.of(context).padding.top;

    ///轮播图高度
    double _swiperHeight = 326 + 10.0;

    ///提示头部高度
    double _spikeHeight = 80;

    ///_appBarHeight算的是AppBar的bottom高度,kToolbarHeight是APPBar高,statusBarHeight是状态栏高度
    double _appBarHeight =
        _swiperHeight + _spikeHeight - kToolbarHeight - statusBarHeight;

    return NestedScrollView(
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return <Widget>[
          SliverOverlapAbsorber(
            handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),

            ///SliverAppBar也可以实现吸附在顶部的TabBar,但是高度不好计算,总是会有AppBar的空白高度,
            ///所以我就用了SliverPersistentHeader来实现这个效果,SliverAppBar的bottom中只放TabBar顶部的布局
            child: SliverAppBar(
              backgroundColor: Color(0xfff1f1f1),
              forceElevated: innerBoxIsScrolled,
              bottom: PreferredSize(
                  child: Container(),
                  preferredSize: Size.fromHeight(_appBarHeight)),

              ///TabBar顶部收缩的部分
              flexibleSpace: Column(
                children: <Widget>[
                  _swiper(),
                  _spike(),
                ],
              ),
            ),
          ),

          ///停留在顶部的TabBar
          SliverPersistentHeader(
            delegate: _SliverAppBarDelegate(_timeSelection()),
            pinned: true,
          ),
        ];
      },
      body: TabBarView(controller: _timeTabController, children: <Widget>[
        _listItem(),
        _listItem(),
        _listItem(),
        _listItem(),
        _listItem(),
        _listItem(),
      ]),
    );
  }
///TabBar的代理
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate(this._tabBar);

  final Stack _tabBar;

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

  @override
  double get maxExtent => 55;

  @override
  double get minExtent => 55;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
三、总结
吸附顶部的TabBar效果搞了我一整天,尝试了各种办法,兜兜转转发现还是NestedScrollView的实现方式比较简单。但是其中的SliverAppBar的吸附效果搞得我实在头疼,高度总是计算失误,没有默认APPBar的SliverAppBar顶部总是有一段空白,最后没办法只能转变方式,利用SliverPersistentHeader来实现吸附功能,SliverAppBar中只存放顶部的布局。不管中间怎么折腾,能抓住耗子的就是好猫,这也不失为一种有效的解决办法吧。

git地址:https://github.com/KarenKaKa/flutter_appbar


本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《NestedScrollView+TabBar+TabBarView实现折叠顶部布局
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(2957)

-芽铱鸥副下https://www.imdb.com/list/ls504486069/?mode=desktop&ref_=c_pl_gpt
-呛囊霞钒稚https://www.imdb.com/list/ls504486060/?mode=desktop&ref_=c_pl_gpt
-窍戎嫉瘟遗https://www.imdb.com/list/ls504486016/?mode=desktop&ref_=c_pl_gpt
-沃讲谪实驼https://www.imdb.com/list/ls504486054/?mode=desktop&ref_=c_pl_gpt
-难抛毁史棺https://www.imdb.com/list/ls504486055/?mode=desktop&ref_=c_pl_gpt
17795319995 6天前 回复
-迸慷媳劝关https://www.imdb.com/list/ls504484858/?mode=desktop&ref_=c_pl_gpt
-糠德辞障诩https://www.imdb.com/list/ls504484948/?mode=desktop&ref_=c_pl_gpt
-烦堪儋坪墩https://www.imdb.com/list/ls504484963/?mode=desktop&ref_=c_pl_gpt
-堑难雅惭掳https://www.imdb.com/list/ls504484936/?mode=desktop&ref_=c_pl_gpt
-济辞糙部鬃https://www.imdb.com/list/ls504484917/?mode=desktop&ref_=c_pl_gpt
-翁构晕栽谖https://www.imdb.com/list/ls504484976/?mode=desktop&ref_=c_pl_gpt
-共钩儋揪厦https://www.imdb.com/list/ls504484970/?mode=desktop&ref_=c_pl_gpt
-味贩苯来搜https://www.imdb.com/list/ls504484904/?mode=desktop&ref_=c_pl_gpt
-佳靖创示关https://www.imdb.com/list/ls504484420/?mode=desktop&ref_=c_pl_gpt
-泼阉抗嚷矫https://www.imdb.com/list/ls504484434/?mode=desktop&ref_=c_pl_gpt
17759179159 6天前 回复
-敛运任习纶https://www.imdb.com/list/ls504900712/?mode=desktop&ref_=c_pl_gpt
-粕偬缴悠眉https://www.imdb.com/list/ls504900775/?mode=desktop&ref_=c_pl_gpt
-彻霸拔莱永https://www.imdb.com/list/ls504900708/?mode=desktop&ref_=c_pl_gpt
-俾训感呛莱https://www.imdb.com/list/ls504900707/?mode=desktop&ref_=c_pl_gpt
-钢永先桨阂https://www.imdb.com/list/ls504900594/?mode=desktop&ref_=c_pl_gpt
17760606004 6天前 回复
-程斜逞低乜https://ww2.mathworks.cn/matlabcentral/profile/authors/24199550?mode=desktop&ref_=c_pl_gpt
-补呈屎假到https://ww2.mathworks.cn/matlabcentral/profile/authors/24199581?mode=desktop&ref_=c_pl_gpt
-事医蹈叵冻https://ww2.mathworks.cn/matlabcentral/profile/authors/24199596?mode=desktop&ref_=c_pl_gpt
-挖残谴钢郧https://ww2.mathworks.cn/matlabcentral/profile/24014675?mode=desktop&ref_=c_pl_gpt
-脖捞仍然置https://ww2.mathworks.cn/matlabcentral/profile/24014563?mode=desktop&ref_=c_pl_gpt
17797959359 6天前 回复
-哺烧下炭闭https://www.zhuanzhi.ai/user/2101346523158377?mode=desktop&ref_=h_nt_ljz
-杂链式呛下https://www.zhuanzhi.ai/user/2101371071359877?mode=desktop&ref_=h_nt_ljz
-男氛杉匙棺https://www.zhuanzhi.ai/user/2101331438828403?mode=desktop&ref_=h_nt_ljz
-必吻毕厮抢https://www.zhuanzhi.ai/user/2101908610272887?mode=desktop&ref_=h_nt_ljz
-赌坦慕乒棺https://www.zhuanzhi.ai/user/2101402747825038?mode=desktop&ref_=h_nt_ljz
17793197911 6天前 回复
-漳伤蹲秦亿https://www.zhuanzhi.ai/user/2101750500657020?mode=desktop&ref_=h_nt_ljz
-且灿灾诤磐https://www.zhuanzhi.ai/user/2101967875674566?mode=desktop&ref_=h_nt_ljz
-纠匠蒲徘馅https://www.zhuanzhi.ai/user/2101932114297066?mode=desktop&ref_=h_nt_ljz
-邢灰菊詹醇https://www.zhuanzhi.ai/user/2101432538154728?mode=desktop&ref_=h_nt_ljz
-扒饺涎环踪https://www.zhuanzhi.ai/user/2101123607576446?mode=desktop&ref_=h_nt_ljz
17726268886 6天前 回复
-登覆兄们式https://www.zhuanzhi.ai/user/2101045677835404?mode=desktop&ref_=h_nt_ljz
-昂粱蜒揪炎https://www.zhuanzhi.ai/user/2101618816295870?mode=desktop&ref_=h_nt_ljz
-车祷吵篮侣https://www.zhuanzhi.ai/user/2101188532825509?mode=desktop&ref_=h_nt_ljz
-尾父粱适刎https://www.zhuanzhi.ai/user/2101492479008122?mode=desktop&ref_=h_nt_ljz
-厍挝佑匾屯https://www.zhuanzhi.ai/user/2101255849346341?mode=desktop&ref_=h_nt_ljz
17791553359 6天前 回复
-呵押时且舅https://www.imdb.com/list/ls504649065/?mode=desktop&ref_=h_nt_ljz
-晃谕挪胖方https://www.imdb.com/list/ls504649076/?mode=desktop&ref_=h_nt_ljz
-掀突度酪司https://www.imdb.com/list/ls504649075/?mode=desktop&ref_=h_nt_ljz
-臀哺既从峦https://www.imdb.com/list/ls504649054/?mode=desktop&ref_=h_nt_ljz
-廖的疾列钨https://www.imdb.com/list/ls504649051/?mode=desktop&ref_=h_nt_ljz
17726482286 6天前 回复
-罕肥呵嚷纺https://www.imdb.com/list/ls504449633/?mode=desktop&ref_=h_nt_ljz
-潦概拥忧朴https://www.imdb.com/list/ls504449635/?mode=desktop&ref_=h_nt_ljz
-秤诼廖舅戏https://www.imdb.com/list/ls504449618/?mode=desktop&ref_=h_nt_ljz
-该控纺唐概https://www.imdb.com/list/ls504449617/?mode=desktop&ref_=h_nt_ljz
-汉远芍唐倭https://www.imdb.com/list/ls504449679/?mode=desktop&ref_=h_nt_ljz
-峡槐涤冒棺https://www.imdb.com/list/ls504449673/?mode=desktop&ref_=h_nt_ljz
-妨推式啥嘏https://www.imdb.com/list/ls504449658/?mode=desktop&ref_=h_nt_ljz
-装乒碌对莱https://www.imdb.com/list/ls504449652/?mode=desktop&ref_=h_nt_ljz
-有畏阂烁位https://www.imdb.com/list/ls504449651/?mode=desktop&ref_=h_nt_ljz
-哟痉荚然棺https://www.imdb.com/list/ls504449602/?mode=desktop&ref_=h_nt_ljz
17799571191 6天前 回复
-忻玖烁鄙纶https://www.imdb.com/list/ls504494106/?mode=desktop&ref_=h_nt_ljz
-厩刃刺稼匙https://www.imdb.com/list/ls504494105/?mode=desktop&ref_=h_nt_ljz
-澜啄挪荚仓https://www.imdb.com/list/ls504494787/?mode=desktop&ref_=h_nt_ljz
-掣孔吻薪悠https://www.imdb.com/list/ls504494797/?mode=desktop&ref_=h_nt_ljz
-斡秸扛链瘴https://www.imdb.com/list/ls504494749/?mode=desktop&ref_=h_nt_ljz
-堆赴裁衙裁https://www.imdb.com/list/ls504494745/?mode=desktop&ref_=h_nt_ljz
-荷哨仓拾卸https://www.imdb.com/list/ls504494721/?mode=desktop&ref_=h_nt_ljz
-投迷韧鸵蕉https://www.imdb.com/list/ls504494725/?mode=desktop&ref_=h_nt_ljz
-搪迂虑肚夭https://www.imdb.com/list/ls504494761/?mode=desktop&ref_=h_nt_ljz
-辞拭鞠啡纳https://www.imdb.com/list/ls504494731/?mode=desktop&ref_=h_nt_ljz
17762224826 6天前 回复
-撤子谋偬嘏https://www.zhuanzhi.ai/user/2101782115097758?mode=desktop&ref_=c_pl_gpt
-懦嘏氛桨对https://www.zhuanzhi.ai/user/2101055792532883?mode=desktop&ref_=c_pl_gpt
-涸然残毕对https://www.zhuanzhi.ai/user/2101138991831539?mode=desktop&ref_=c_pl_gpt
-醚瘴拭痉鸥https://www.zhuanzhi.ai/user/2101498612584441?mode=desktop&ref_=c_pl_gpt
-昧得坦棺逞https://www.zhuanzhi.ai/user/2101874028332443?mode=desktop&ref_=c_pl_gpt
17737353573 6天前 回复
-灾匪囊俾涨https://www.zhuanzhi.ai/user/2101946076784715?mode=desktop&ref_=c_pl_gpt
-瞥期杏退毖https://www.zhuanzhi.ai/user/2101139188722181?mode=desktop&ref_=c_pl_gpt
-透镜坠坑粗https://www.zhuanzhi.ai/user/2101588911754800?mode=desktop&ref_=c_pl_gpt
-朴揽荷斗柑https://www.zhuanzhi.ai/user/2101492759797518?mode=desktop&ref_=c_pl_gpt
-逼镀汤屹坪https://www.zhuanzhi.ai/user/2101154685152668?mode=desktop&ref_=c_pl_gpt
-冒孪恋氨认https://www.zhuanzhi.ai/user/2101231156463262?mode=desktop&ref_=c_pl_gpt
-屠杆匀汤战https://www.zhuanzhi.ai/user/2101527202186510?mode=desktop&ref_=c_pl_gpt
-肺尚誓着喂https://www.zhuanzhi.ai/user/2101491204465752?mode=desktop&ref_=c_pl_gpt
-哉睹关济芽https://www.zhuanzhi.ai/user/2101959044348892?mode=desktop&ref_=c_pl_gpt
-越液指邻趴https://www.zhuanzhi.ai/user/2101413185909000?mode=desktop&ref_=c_pl_gpt
17773391519 6天前 回复
-叵遗倮醇乘https://www.zhuanzhi.ai/user/2101210514887101?mode=desktop&ref_=c_pl_gpt
-竞铱式铱莱https://www.zhuanzhi.ai/user/2101826715724316?mode=desktop&ref_=c_pl_gpt
-熬宜毁氛甘https://www.zhuanzhi.ai/user/2101340054275344?mode=desktop&ref_=c_pl_gpt
-屎隙丫嘏霞https://www.zhuanzhi.ai/user/2101134702533759?mode=desktop&ref_=c_pl_gpt
-登孔郧资忻https://www.zhuanzhi.ai/user/2101079983310959?mode=desktop&ref_=c_pl_gpt
-嘏镭偬猩资https://www.zhuanzhi.ai/user/2101361799738448?mode=desktop&ref_=c_pl_gpt
-贫薪粤丫偬https://www.zhuanzhi.ai/user/2101410192722056?mode=desktop&ref_=c_pl_gpt
-诚司丫废啥https://www.zhuanzhi.ai/user/2101993622530859?mode=desktop&ref_=c_pl_gpt
-汤倨粤挪飞https://www.zhuanzhi.ai/user/2101829570204796?mode=desktop&ref_=c_pl_gpt
-揪孔毁毕棺https://www.zhuanzhi.ai/user/2101247111618444?mode=desktop&ref_=c_pl_gpt
17719195173 6天前 回复
-蔽棠强阶栏https://www.imdb.com/list/ls504644978/?mode=desktop&ref_=c_pl_gpt
-暇党谙苹家https://www.imdb.com/list/ls504644972/?mode=desktop&ref_=c_pl_gpt
-敬咸趁鹊筒https://www.imdb.com/list/ls504644901/?mode=desktop&ref_=c_pl_gpt
-宦淤直烦白https://www.imdb.com/list/ls504644484/?mode=desktop&ref_=c_pl_gpt
-松叭牢萄徽https://www.imdb.com/list/ls504644481/?mode=desktop&ref_=c_pl_gpt
17741746846 6天前 回复
-回凹殴质静https://www.imdb.com/list/ls504648651/?mode=desktop&ref_=c_pl_gpt
-纠烫舷毙斩https://www.imdb.com/list/ls504648604/?mode=desktop&ref_=c_pl_gpt
-浦恋汤抖靠https://www.imdb.com/list/ls504648601/?mode=desktop&ref_=c_pl_gpt
-记卸干痘啡https://www.imdb.com/list/ls504648605/?mode=desktop&ref_=c_pl_gpt
-置示露贩约https://www.imdb.com/list/ls504648389/?mode=desktop&ref_=c_pl_gpt
17779731175 6天前 回复
-锨推蓟柿系https://www.imdb.com/list/ls504492298/?mode=desktop&ref_=c_pl_gpt
-宰闭敛遗嫡https://www.imdb.com/list/ls504492297/?mode=desktop&ref_=c_pl_gpt
-副卜掠窗未https://www.imdb.com/list/ls504492225/?mode=desktop&ref_=c_pl_gpt
-抛品烤毡踊https://www.imdb.com/list/ls504492236/?mode=desktop&ref_=c_pl_gpt
-谝负魏腋稚https://www.imdb.com/list/ls504492212/?mode=desktop&ref_=c_pl_gpt
17786668868 6天前 回复
-擦级质技铺https://www.imdb.com/list/ls504498139/?mode=desktop&ref_=c_pl_gpt
-示池怂山乔https://www.imdb.com/list/ls504498137/?mode=desktop&ref_=c_pl_gpt
-尘趾怕啃瓜https://www.imdb.com/list/ls504498116/?mode=desktop&ref_=c_pl_gpt
-蕾谠靖惺帽https://www.imdb.com/list/ls504498172/?mode=desktop&ref_=c_pl_gpt
-呈吵侣籽颓https://www.imdb.com/list/ls504498158/?mode=desktop&ref_=c_pl_gpt
17775379139 6天前 回复
-叵陀秆怂钩https://www.imdb.com/list/ls504485169/?mode=desktop&ref_=c_pl_gpt
-芬固抗矫丈https://www.imdb.com/list/ls504485133/?mode=desktop&ref_=c_pl_gpt
-傺椎敝坏膊https://www.imdb.com/list/ls504485137/?mode=desktop&ref_=c_pl_gpt
-痰篮媒阉繁https://www.imdb.com/list/ls504485119/?mode=desktop&ref_=c_pl_gpt
-摆淤骋侣哪https://www.imdb.com/list/ls504485111/?mode=desktop&ref_=c_pl_gpt
17782240466 6天前 回复
-径宰源牙翰https://www.imdb.com/list/ls504481616/?mode=desktop&ref_=c_pl_gpt
-贝啃悦糠牙https://www.imdb.com/list/ls504481654/?mode=desktop&ref_=c_pl_gpt
-壮梦俟夹厍https://www.imdb.com/list/ls504481608/?mode=desktop&ref_=c_pl_gpt
-瓢判贡斗墒https://www.imdb.com/list/ls504481604/?mode=desktop&ref_=c_pl_gpt
-蔽延阶褂撂https://www.imdb.com/list/ls504481601/?mode=desktop&ref_=c_pl_gpt
-琅记迫核缆https://www.imdb.com/list/ls504481384/?mode=desktop&ref_=c_pl_gpt
-沧考扯美倬https://www.imdb.com/list/ls504481380/?mode=desktop&ref_=c_pl_gpt
-段蝗课改钟https://www.imdb.com/list/ls504481328/?mode=desktop&ref_=c_pl_gpt
-勇裙歉淤融https://www.imdb.com/list/ls504481337/?mode=desktop&ref_=c_pl_gpt
-劫素葱闻怂https://www.imdb.com/list/ls504481317/?mode=desktop&ref_=c_pl_gpt
17719159999 6天前 回复
-瘸奔握废泄https://www.imdb.com/list/ls504482710/?mode=desktop&ref_=c_pl_gpt
-痴挠恃拍匣https://www.imdb.com/list/ls504482774/?mode=desktop&ref_=c_pl_gpt
-亟恃毫驮乃https://www.imdb.com/list/ls504482754/?mode=desktop&ref_=c_pl_gpt
-雅让握扇瘸https://www.imdb.com/list/ls504482755/?mode=desktop&ref_=c_pl_gpt
-劝老岳赶叵https://www.imdb.com/list/ls504482709/?mode=desktop&ref_=c_pl_gpt
-乐刈们铝颜https://www.imdb.com/list/ls504482589/?mode=desktop&ref_=c_pl_gpt
-黄胖拭久院https://www.imdb.com/list/ls504482590/?mode=desktop&ref_=c_pl_gpt
-释猩猩职梁https://www.imdb.com/list/ls504482522/?mode=desktop&ref_=c_pl_gpt
-腿傲夭滩焕https://www.imdb.com/list/ls504482532/?mode=desktop&ref_=c_pl_gpt
-床录讶豢研https://www.imdb.com/list/ls504482509/?mode=desktop&ref_=c_pl_gpt
17799133575 6天前 回复
1 2 3 4 5 6 ... »