Flutter 擴(kuò)展的聯(lián)動(dòng)Tabs

Flutter里面使用TabBar和TabBarView的時(shí)候基茵。如果是二級(jí)TabBar崖堤,產(chǎn)品說(shuō)了一個(gè)需求:二級(jí)TabBar需要是那種聯(lián)動(dòng)的,就是說(shuō)下面一層的TabBar滑不動(dòng)了,就滑動(dòng)上一層的TabBar乌昔,不知道這個(gè)效果在安卓/IOS里面叫什么,搜了下網(wǎng)上也沒(méi)看到壤追。

FlutterCandies QQ群:181398081


image

目錄

image

聯(lián)動(dòng)的TabBarView

那么我們打開(kāi)flutter\packages\flutter\lib\src\material\tabs.dart,開(kāi)始魔改磕道。

1.首先我們需要獲取到上一層的TabBarView.

 Widget build(BuildContext context) {
    if (widget.linkWithAncestor) {
      _ancestor =
          context.ancestorStateOfType(TypeMatcher<_ExtendedTabBarViewState>());
    }
  1. 不能滑動(dòng)的時(shí)候我們能拿到OverscrollNotification,看這個(gè)之前強(qiáng)烈建議去看一下NotificationListener,這個(gè)是個(gè)好東西行冰,能監(jiān)聽(tīng)各種通知溺蕉。

我們來(lái)到_handleScrollNotification這個(gè)方法添加判斷
notification is OverscrollNotification

    if (notification is OverscrollNotification && _ancestor != null) {
      var overscrollNotification = notification as OverscrollNotification;
      if (_canlinkeWithAncestorScroll(overscrollNotification.overscroll < 0)) {
        _ancestor._pageController.position.moveTo(
            _ancestor._pageController.offset +
                overscrollNotification.overscroll);
      }
    }

并且通過(guò)_canlinkeWithAncestorScroll方法判斷上一層TabBarView是否能滑動(dòng)

bool _canlinkeWithAncestorScroll(bool onLeftEdge) {
    //return false;
    if (_ancestor == null) return false;
    return (onLeftEdge &&
            _ancestor._pageController.offset !=
                _ancestor._pageController.position.minScrollExtent) ||
        ((!onLeftEdge &&
            _ancestor._pageController.offset !=
                _ancestor._pageController.position.maxScrollExtent));
  }

3.將上層TabBarView的_pageController改變?yōu)閛ffset+拖動(dòng)overscroll的。這樣效果就完成了悼做。

_ancestor._pageController.position.moveTo(
            _ancestor._pageController.offset +
                overscrollNotification.overscroll);

4.如果上層可以滑動(dòng)疯特,我們需要去掉overscroll的阻尼效果。
首先在增加對(duì)OverscrollIndicatorNotification的監(jiān)聽(tīng)

 return NotificationListener<ScrollNotification>(
      onNotification: _handleScrollNotification,
      child: NotificationListener<OverscrollIndicatorNotification>(
        onNotification: _handleGlowNotification,
        child: ExtendedPageView(
          controller: _pageController,
          physics: widget.physics == null
              ? _kTabBarViewPhysics
              : _kTabBarViewPhysics.applyTo(widget.physics),
          children: _children,
        ),
      ),
    );

判斷是否上層TabBarView能否滑動(dòng)

 bool _handleGlowNotification(OverscrollIndicatorNotification notification) {
    debugPrint("${notification.depth}++++ ${_ancestor != null}");
    if (notification.depth == 0 &&
        _canlinkeWithAncestorScroll(notification.leading)) {
      notification.disallowGlow();
      return true;
    }
    return false;
  }

產(chǎn)品要的聯(lián)動(dòng)效果就這樣搞定了肛走。漓雅。是不是很簡(jiǎn)單。朽色。多看源碼還是有很多好處的邻吞。。

TabBar色卡指示器ColorTabIndicator

這個(gè)是隨手送的功能葫男。抱冷。( ╯□╰ )就是TabBar指示器為一個(gè)色塊,代碼沒(méi)啥好說(shuō)的

class ColorTabIndicator extends Decoration {
  ColorTabIndicator(this.color);

  /// The color and weight of the horizontal line drawn below the selected tab.
  final Color color;

  @override
  Decoration lerpFrom(Decoration a, double t) {
    return super.lerpFrom(a, t);
  }

  @override
  Decoration lerpTo(Decoration b, double t) {
    return super.lerpTo(b, t);
  }

  @override
  _ColorPainter createBoxPainter([VoidCallback onChanged]) {
    return _ColorPainter(this, onChanged);
  }
}

class _ColorPainter extends BoxPainter {
  _ColorPainter(this.decoration, VoidCallback onChanged)
      : assert(decoration != null),
        super(onChanged);

  final ColorTabIndicator decoration;

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    assert(configuration != null);
    assert(configuration.size != null);
    final Rect rect = offset & configuration.size;
    final Paint paint = Paint();
    paint.color = decoration.color;
    canvas.drawRect(rect, paint);
  }
}

控制緩存頁(yè)數(shù)CacheExtent

  /// cache page count
  /// default is 0.
  /// if cacheExtent is 1, it has two pages in cache
  /// null is infinity, it will cache all pages
  final int cacheExtent;

控制TabBarView緩存頁(yè)面的個(gè)數(shù)梢褐,通過(guò)重寫(xiě)了PageView中的Viewport的cacheExtent值來(lái)實(shí)現(xiàn)旺遮。

ExtendedPageView的build方法中,增加了對(duì)Viewport的cacheExtend的設(shè)置。

 child: Scrollable(
        axisDirection: axisDirection,
        controller: widget.controller,
        physics: physics,
        viewportBuilder: (BuildContext context, ViewportOffset position) {
          if (widget.cacheExtent > 0) {
            return LayoutBuilder(
                builder: (BuildContext context, BoxConstraints constraints) {
              return Viewport(
                cacheExtent: widget.cacheExtent * constraints.maxWidth,
                axisDirection: axisDirection,
                offset: position,
                slivers: <Widget>[
                  SliverFillViewport(
                      viewportFraction: widget.controller.viewportFraction,
                      delegate: widget.childrenDelegate),
                ],
              );
            });
          } else {
            return Viewport(
              cacheExtent: widget.cacheExtent == null ? double.infinity : 0.0,
              axisDirection: axisDirection,
              offset: position,
              slivers: <Widget>[
                SliverFillViewport(
                    viewportFraction: widget.controller.viewportFraction,
                    delegate: widget.childrenDelegate),
              ],
            );
          }
        },
      ),

最后放上 Github extended_tabs盈咳,如果你有什么不明白的地方趣效,請(qǐng)告訴我。

image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末猪贪,一起剝皮案震驚了整個(gè)濱河市跷敬,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖西傀,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件斤寇,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡拥褂,警方通過(guò)查閱死者的電腦和手機(jī)娘锁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)饺鹃,“玉大人莫秆,你說(shuō)我怎么就攤上這事』谙辏” “怎么了镊屎?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)茄螃。 經(jīng)常有香客問(wèn)我缝驳,道長(zhǎng),這世上最難降的妖魔是什么归苍? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任用狱,我火速辦了婚禮,結(jié)果婚禮上拼弃,老公的妹妹穿的比我還像新娘夏伊。我一直安慰自己,他們只是感情好吻氧,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布溺忧。 她就那樣靜靜地躺著,像睡著了一般医男。 火紅的嫁衣襯著肌膚如雪砸狞。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,443評(píng)論 1 302
  • 那天镀梭,我揣著相機(jī)與錄音刀森,去河邊找鬼。 笑死报账,一個(gè)胖子當(dāng)著我的面吹牛研底,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播透罢,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼榜晦,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了羽圃?” 一聲冷哼從身側(cè)響起乾胶,我...
    開(kāi)封第一講書(shū)人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后识窿,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體斩郎,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年喻频,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了缩宜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡甥温,死狀恐怖锻煌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情姻蚓,我是刑警寧澤宋梧,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站史简,受9級(jí)特大地震影響乃秀,放射性物質(zhì)發(fā)生泄漏肛著。R本人自食惡果不足惜圆兵,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望枢贿。 院中可真熱鬧殉农,春花似錦、人聲如沸局荚。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)耀态。三九已至轮傍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間首装,已是汗流浹背创夜。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留仙逻,地道東北人驰吓。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像系奉,于是被迫代替她去往敵國(guó)和親檬贰。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

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