Flutter AppBar-滑動漸變-懸停-TabBar嵌套效果合集

效果圖

AppBar-漸變-懸停-嵌套效果.gif

關(guān)鍵代碼

1.NestedScrollView + SliverAppBar + AppBar + TabBar

TabController _tabController;
_tabController = TabController(initialIndex: 1, length: 2, vsync: this);

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled){
            return <Widget>[
              SliverAppBar(
                primary: true,
                //是否隨著滑動隱藏標(biāo)題
                floating: true,
                //是否固定在頂部
                pinned: false,
                //可折疊的應(yīng)用欄
                flexibleSpace: AppBar(
                  title: TabBar(
                    tabs: [
                      Tab(text: '好友'),
                      Tab(text: '心動'),
                    ],
                    controller: _tabController,
                    indicatorWeight: ScreenUtil().setHeight(6),
                    indicatorPadding: EdgeInsets.only(
                        left: ScreenUtil().setWidth(30),
                        right: ScreenUtil().setWidth(30)),
                    labelPadding: EdgeInsets.symmetric(
                        horizontal: ScreenUtil().setWidth(30)),
                    isScrollable: true,
                    indicatorColor: Color(0xffFF7E98),
                    labelColor: Color(0xffFF7E98),
                    labelStyle: TextStyle(
                      fontSize: ScreenUtil().setSp(42),
                      color: Color(0xffFF7E98),
                      fontWeight: FontWeight.bold,
                    ),
                    unselectedLabelColor: Color(0xffAAAAAA),
                    unselectedLabelStyle: TextStyle(
                      fontSize: ScreenUtil().setSp(36),
                      color: Color(0xffAAAAAA),
                    ),
                    indicatorSize: TabBarIndicatorSize.label,
                  ),
                  backgroundColor: Colors.white,
                  elevation: 0,
                ),
                snap: true,
              ),
            ];
          },
          body: TabBarView(
            children: [
              GroupList(),
              HeartBeatList(),
            ],
            controller: _tabController,
          ),
      ),
    );
  }

2.二級嵌套

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        flexibleSpace: FlexibleSpaceBar(
          centerTitle: true,
          title: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                // 設(shè)置陰影
                boxShadow: [
                  BoxShadow(
                    color: Color(0xffD9D9D9),
                    blurRadius: 9.0,
                    spreadRadius: -7.0,
                    offset: Offset(0, -7),
                  )
                ],
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(ScreenUtil().setWidth(30)),
                  topRight: Radius.circular(ScreenUtil().setWidth(30)),
                ),
              ),
              //alignment: Alignment.topCenter,
              child: TabBar(
                tabs: [
                  Badge(
                    child: Tab(text: '我心動的'),
                    align: Alignment(2.5, -0.6),
                    text: unreadMsg != null &&
                        unreadMsg.ilike != null &&
                        unreadMsg.ilike > 0
                        ? unreadMsg.ilike.toString()
                        : '',
                    padding: 3,
                    backgroundColor: Color(0xffFA5151),
                    textStyle: TextStyle(
                      fontSize: ScreenUtil().setSp(22),
                      color: Colors.white,
                    ),
                    visible: unreadMsg != null &&
                        unreadMsg.ilike != null &&
                        unreadMsg.ilike > 0
                        ? true
                        : false,
                  ),
                  Badge(
                    child: Tab(text: '心動我的'),
                    align: Alignment(2.5, -0.6),
                    text: unreadMsg != null && unreadMsg.likeMe > 0
                        ? unreadMsg.likeMe.toString()
                        : '',
                    padding: 3,
                    backgroundColor: Color(0xffFA5151),
                    textStyle: TextStyle(
                      fontSize: ScreenUtil().setSp(22),
                      color: Colors.white,
                    ),
                    visible:
                    unreadMsg != null && unreadMsg.likeMe > 0 ? true : false,
                  ),
                ],
                controller: _tabController,
                indicatorWeight: ScreenUtil().setHeight(6),
                indicatorPadding: EdgeInsets.only(
                    left: ScreenUtil().setWidth(40),
                    right: ScreenUtil().setWidth(40)),
                labelPadding:
                EdgeInsets.symmetric(horizontal: ScreenUtil().setWidth(30)),
                isScrollable: false,
                indicatorColor: Color(0xffFF7E98),
                labelColor: Color(0xffFF7E98),
                labelStyle: TextStyle(
                  fontSize: ScreenUtil().setSp(30),
                  color: Color(0xffFF7E98),
                ),
                unselectedLabelColor: Color(0xffAAAAAA),
                unselectedLabelStyle: TextStyle(
                  fontSize: ScreenUtil().setSp(28),
                  color: Color(0xffAAAAAA),
                ),
                indicatorSize: TabBarIndicatorSize.label,
              )),
        ),
        backgroundColor: Colors.white,
        elevation: 0,
      ),
      body: TabBarView(
        children: [
          MyHeartBeatList(),
          HeartBeatMineList(),
        ],
        controller: _tabController,
      ),
    );
  }

補充

Demo案例代碼

import 'package:flutter/material.dart';

class TestWidget extends StatefulWidget {
  final String title;

  TestWidget({Key key, this.title}) : super(key: key);

  @override
  _TestWidgetState createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget>
    with SingleTickerProviderStateMixin {
  TabController tabController;

  @override
  void initState() {
    super.initState();
    this.tabController = TabController(length: 2, vsync: this);
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled){
            return <Widget>[
              SliverAppBar(
                //展開高度
                expandedHeight: 80.0,
                //是否隨著滑動隱藏標(biāo)題
                floating: true,
                //是否固定在頂部
                pinned: false,
                //可折疊的應(yīng)用欄
                flexibleSpace: FlexibleSpaceBar(
                  background: Image.network(
                    'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1549129578802&di=f866c638ea12ad13c5d603bcc008a6c2&imgtype=0&src=http%3A%2F%2Fpic2.16pic.com%2F00%2F07%2F66%2F16pic_766297_b.jpg',
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ];
          },
          body: Center(
            child: Text('向上提拉,查看效果'),
          )
      ),
    );
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市雏亚,隨后出現(xiàn)的幾起案子摇天,更是在濱河造成了極大的恐慌荔棉,老刑警劉巖濒募,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件坪郭,死亡現(xiàn)場離奇詭異,居然都是意外死亡侣监,警方通過查閱死者的電腦和手機鸭轮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來橄霉,“玉大人窃爷,你說我怎么就攤上這事⌒辗洌” “怎么了按厘?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長钱慢。 經(jīng)常有香客問我逮京,道長,這世上最難降的妖魔是什么束莫? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任懒棉,我火速辦了婚禮,結(jié)果婚禮上览绿,老公的妹妹穿的比我還像新娘策严。我一直安慰自己,他們只是感情好饿敲,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布妻导。 她就那樣靜靜地躺著,像睡著了一般诀蓉。 火紅的嫁衣襯著肌膚如雪栗竖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天渠啤,我揣著相機與錄音狐肢,去河邊找鬼。 笑死沥曹,一個胖子當(dāng)著我的面吹牛份名,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播妓美,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼僵腺,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了壶栋?” 一聲冷哼從身側(cè)響起辰如,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎贵试,沒想到半個月后琉兜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凯正,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年豌蟋,在試婚紗的時候發(fā)現(xiàn)自己被綠了廊散。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡梧疲,死狀恐怖允睹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情幌氮,我是刑警寧澤宅荤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布题涨,位于F島的核電站踏拜,受9級特大地震影響凹嘲,放射性物質(zhì)發(fā)生泄漏柒爸。R本人自食惡果不足惜捧书,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一垮抗、第九天 我趴在偏房一處隱蔽的房頂上張望匾效。 院中可真熱鬧陆盘,春花似錦普筹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至酸员,卻和暖如春蜒车,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背幔嗦。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工酿愧, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人邀泉。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓嬉挡,卻偏偏與公主長得像,于是被迫代替她去往敵國和親汇恤。 傳聞我的和親對象是個殘疾皇子庞钢,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 5CoordinatorLayout與AppBarLayout--嵌套滑動 上文我們說了AppBarLayout的...
    chefish閱讀 6,181評論 4 19
  • 尊敬的區(qū)政府财岔、鎮(zhèn)政府风皿、僑冠居委會饭冬、招商局的各位領(lǐng)導(dǎo)們,您們好揪阶! 我是惠州仲愷高新區(qū)潼僑鎮(zhèn)僑冠居委會的居民昌抠,也是僑冠...
    潘潘的成長之路閱讀 343評論 0 0
  • 新年快樂,愿你快樂[em]e166[/em] 1鲁僚、過去不回頭 未來不將就 余華曾在《活著》一文中寫: 沒有什么比時...
    南容555閱讀 286評論 0 0
  • 《日精進(jìn)打卡第10天》 劉金璐17178121717【日精進(jìn)第10天】 一炊苫、【學(xué)~勤學(xué)】 ①日常課誦 十項職責(zé) ②...
    劉金璐閱讀 185評論 0 0