<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
在2021年末,酷狗釋出了最新版11.0.0版本,這是一次重大的UI重構,更新完開啟著實讓我耳目一新。在原有風格上,整個App變得更加清爽,流暢。其中Tabbar的風格讓我非常感興趣,如果用Flutter來實現,或許是一個很有趣的事情。
研究酷狗Tabbar的動畫可以發現,預設狀態下在當前Tab的中心處展示圓點,滑動時的效果拆分成兩個以下部分:
從上面的分析可以明確,指示器的滑動效果一定跟每個Tab的size和offset相關。那在Flutter中,獲取渲染資訊我們馬上能想到GlobalKey,通過GlobalKey的currentContext物件獲取Rander資訊,但這必須在檢視渲染完成後才能獲取,也就是說Tab渲染完才能開始計算並渲染指示器。很顯然不符合體驗要求,同時頻繁使用GlobalKey也會導致效能較差。
轉變思路,我們需要在Tab渲染的不斷把資訊傳給指示器,然後更新指示器,這種方式自然想到了CustomPainter【之前寫了很多Canvas的控制元件,都是根據傳入的值進行繪製,從而實現控制元件的變化了layout類】。在Tab updateWidget的時候,不斷把Rander的資訊傳給畫筆Painter,然後更新繪製,理論上這樣做是完全行得通的。
為了驗證我的思路,我開始研究官方Tabbar是如何寫的:
Widget build(BuildContext context) { // ...此處省略部分程式碼... final List<Widget> wrappedTabs = List<Widget>.generate(widget.tabs.length, (int index) { const double verticalAdjustment = (_kTextAndIconTabHeight - _kTabHeight)/2.0; EdgeInsetsGeometry? adjustedPadding; // 這裡為tab加入Globalkey,以便後續獲取Tab的渲染資訊 if (widget.tabs[index] is PreferredSizeWidget) { final PreferredSizeWidget tab = widget.tabs[index] as PreferredSizeWidget; if (widget.tabHasTextAndIcon && tab.preferredSize.height == _kTabHeight) { if (widget.labelPadding != null || tabBarTheme.labelPadding != null) { adjustedPadding = (widget.labelPadding ?? tabBarTheme.labelPadding!).add(const EdgeInsets.symmetric(vertical: verticalAdjustment)); } else { adjustedPadding = const EdgeInsets.symmetric(vertical: verticalAdjustment, horizontal: 16.0); } } } // ...此處省略部分程式碼... // 可以看到指示器是CustomPaint物件 Widget tabBar = CustomPaint( painter: _indicatorPainter, child: _TabStyle( animation: kAlwaysDismissedAnimation, selected: false, labelColor: widget.labelColor, unselectedLabelColor: widget.unselectedLabelColor, labelStyle: widget.labelStyle, unselectedLabelStyle: widget.unselectedLabelStyle, child: _TabLabelBar( onPerformLayout: _saveTabOffsets, children: wrappedTabs, ), ), );
class _TabLabelBar extends Flex { _TabLabelBar({ Key? key, List<Widget> children = const <Widget>[], required this.onPerformLayout, }) : super( key: key, children: children, direction: Axis.horizontal, mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, verticalDirection: VerticalDirection.down, ); final _LayoutCallback onPerformLayout; @override RenderFlex createRenderObject(BuildContext context) { // 檢視下_TabLabelBarRenderer return _TabLabelBarRenderer( direction: direction, mainAxisAlignment: mainAxisAlignment, mainAxisSize: mainAxisSize, crossAxisAlignment: crossAxisAlignment, textDirection: getEffectiveTextDirection(context)!, verticalDirection: verticalDirection, onPerformLayout: onPerformLayout, ); } @override void updateRenderObject(BuildContext context, _TabLabelBarRenderer renderObject) { super.updateRenderObject(context, renderObject); renderObject.onPerformLayout = onPerformLayout; } }
檢視真實的渲染物件:_TabLabelBarRenderer,在performLayout中返回渲染的size和offset,並通過TabBar傳入的_saveTabOffsets方法儲存到_indicatorPainter中;_saveTabOffsets尤為重要,把Tabbar的渲染位移通知給Painter,從而讓Painter可以輕鬆算出tab之間的寬度差
class _TabLabelBarRenderer extends RenderFlex { _TabLabelBarRenderer({ List<RenderBox>? children, required Axis direction, required MainAxisSize mainAxisSize, required MainAxisAlignment mainAxisAlignment, required CrossAxisAlignment crossAxisAlignment, required TextDirection textDirection, required VerticalDirection verticalDirection, required this.onPerformLayout, }) : assert(onPerformLayout != null), assert(textDirection != null), super( children: children, direction: direction, mainAxisSize: mainAxisSize, mainAxisAlignment: mainAxisAlignment, crossAxisAlignment: crossAxisAlignment, textDirection: textDirection, verticalDirection: verticalDirection, ); _LayoutCallback onPerformLayout; @override void performLayout() { super.performLayout(); // xOffsets will contain childCount+1 values, giving the offsets of the // leading edge of the first tab as the first value, of the leading edge of // the each subsequent tab as each subsequent value, and of the trailing // edge of the last tab as the last value. RenderBox? child = firstChild; final List<double> xOffsets = <double>[]; while (child != null) { final FlexParentData childParentData = child.parentData! as FlexParentData; xOffsets.add(childParentData.offset.dx); assert(child.parentData == childParentData); child = childParentData.nextSibling; } assert(textDirection != null); switch (textDirection!) { case TextDirection.rtl: xOffsets.insert(0, size.width); break; case TextDirection.ltr: xOffsets.add(size.width); break; } onPerformLayout(xOffsets, textDirection!, size.width); } }
@override void didChangeDependencies() { super.didChangeDependencies(); assert(debugCheckHasMaterial(context)); final TabBarTheme tabBarTheme = TabBarTheme.of(context); _updateTabController(); _initIndicatorPainter(adjustedPadding, tabBarTheme); } @override void didUpdateWidget(KuGouTabBar oldWidget) { super.didUpdateWidget(oldWidget); final TabBarTheme tabBarTheme = TabBarTheme.of(context); if (widget.controller != oldWidget.controller) { _updateTabController(); _initIndicatorPainter(adjustedPadding, tabBarTheme); } else if (widget.indicatorColor != oldWidget.indicatorColor || widget.indicatorWeight != oldWidget.indicatorWeight || widget.indicatorSize != oldWidget.indicatorSize || widget.indicator != oldWidget.indicator) { _initIndicatorPainter(adjustedPadding, tabBarTheme); } if (widget.tabs.length > oldWidget.tabs.length) { final int delta = widget.tabs.length - oldWidget.tabs.length; _tabKeys.addAll(List<GlobalKey>.generate(delta, (int n) => GlobalKey())); } else if (widget.tabs.length < oldWidget.tabs.length) { _tabKeys.removeRange(widget.tabs.length, oldWidget.tabs.length); } }
通過理解Flutter Tabbar的實現思路,大體跟我們預想的差不多。不過官方繼承了Flex來計算Offset和size,實現起來很優雅。所以我也不班門弄斧了,直接改動官方的Tabbar就可以了。
2. 重點修改_IndicatorPainter,根據我們的需求來繪製指示器。在painter方法中,我們可以通過controller拿到當前tab的index以及animation!.value, 我們模擬下切換的過程,當tab從第0個移到第1個,動畫的值從0變成1,然後動畫走到0.5時,tab的index會從0突然變為1,指示器應該是先變長,然後在動畫走到0.5時,再變短。因此動畫0.5之前,我們用動畫的value-index作為指示器縮放的倍數,指示器不斷增大;動畫0.5之後,用index-value作為縮放倍數,不斷縮小。
final double index = controller.index.toDouble(); final double value = controller.animation!.value; /// 改動 ltr為false,表示索引還是0,動畫執行未超過50%;ltr為true,表示索引變為1,動畫執行超過50% final bool ltr = index > value; final int from = (ltr ? value.floor() : value.ceil()).clamp(0, maxTabIndex); final int to = (ltr ? from + 1 : from - 1).clamp(0, maxTabIndex); /// 改動 通過ltr來決定是放大還是縮小倍數,可以得出公式:ltr ? (index - value) : (value - index) final Rect fromRect = indicatorRect(size, from, ltr ? (index - value) : (value - index)); /// 改動 final Rect toRect = indicatorRect(size, to, ltr ? (index - value) : (value - index)); _currentRect = Rect.lerp(fromRect, toRect, (value - from).abs());
而指示器接收縮放倍數的前提還需要計算指示器最大的寬度,並且上面是根據動畫的0.5作為最大的寬度,也就是移動到一半的時候,指示器應該達到最大寬度。因此指示器最大的寬度是需要✖️2的。請看下面程式碼:
class _IndicatorPainter extends CustomPainter { ......此處省略部分程式碼...... void saveTabOffsets(List<double>? tabOffsets, TextDirection? textDirection) { _currentTabOffsets = tabOffsets; _currentTextDirection = textDirection; } // _currentTabOffsets[index] is the offset of the start edge of the tab at index, and // _currentTabOffsets[_currentTabOffsets.length] is the end edge of the last tab. int get maxTabIndex => _currentTabOffsets!.length - 2; double centerOf(int tabIndex) { assert(_currentTabOffsets != null); assert(_currentTabOffsets!.isNotEmpty); assert(tabIndex >= 0); assert(tabIndex <= maxTabIndex); return (_currentTabOffsets![tabIndex] + _currentTabOffsets![tabIndex + 1]) / 2.0; } /// 接收上面程式碼分析中傳入的倍數 scale Rect indicatorRect(Size tabBarSize, int tabIndex, double scale) { assert(_currentTabOffsets != null); assert(_currentTextDirection != null); assert(_currentTabOffsets!.isNotEmpty); assert(tabIndex >= 0); assert(tabIndex <= maxTabIndex); double tabLeft, tabRight, tabWidth = 0; switch (_currentTextDirection!) { case TextDirection.rtl: tabLeft = _currentTabOffsets![tabIndex + 1]; tabRight = _currentTabOffsets![tabIndex]; break; case TextDirection.ltr: tabLeft = _currentTabOffsets![tabIndex]; tabRight = _currentTabOffsets![tabIndex + 1]; break; } /// 改動,通過GlobalKey計算出渲染的文字的寬度 tabWidth = tabKeys[tabIndex].currentContext!.size!.width; final double delta = ((tabRight - tabLeft) - tabWidth) / 2.0; tabLeft += delta; tabRight -= delta; final EdgeInsets insets = indicatorPadding.resolve(_currentTextDirection); /// 改動,算出指示器的最大寬度,記得*2 double maxLen = (tabRight - tabLeft + insets.horizontal) * 2; double res = scale == 0 ? minWidth : maxLen * (scale < 0.5 ? scale : 1 - scale); /// 改動 final Rect rect = Rect.fromLTWH(tabLeft + tabWidth / 2 - minWidth / 2, 0.0, res > minWidth ? res : minWidth, tabBarSize.height); if (!(rect.size >= insets.collapsedSize)) { throw FlutterError( 'indicatorPadding insets should be less than Tab Sizen' 'Rect Size : ${rect.size}, Insets: ${insets.toString()}', ); } return insets.deflateRect(rect); } }
【由於時間和精力問題,我並沒有去做這一步的實現,而且酷狗那邊動畫跟滑動邏輯的關係需要UI給出具體的公式,才能百分百還原。】
最後就是加多一個引數,讓業務方傳入指示器的最小寬度。
/// 指示器的最小寬度 final double indicatorMinWidth;
在上面我們已經把簡單的動畫效果改完了,接下來就是傳入圓角的indicator、最小寬度indicatorMinWidth,就可以正常使用啦。
import 'package:flutter/material.dart'; class RRecTabIndicator extends Decoration { const RRecTabIndicator( {this.borderSide = const BorderSide(width: 2.0, color: Colors.white), this.insets = EdgeInsets.zero, this.radius = 0, this.color = Colors.white}); final double radius; final Color color; final BorderSide borderSide; final EdgeInsetsGeometry insets; @override Decoration? lerpFrom(Decoration? a, double t) { if (a is RRecTabIndicator) { return RRecTabIndicator( borderSide: BorderSide.lerp(a.borderSide, borderSide, t), insets: EdgeInsetsGeometry.lerp(a.insets, insets, t)!, ); } return super.lerpFrom(a, t); } @override Decoration? lerpTo(Decoration? b, double t) { if (b is RRecTabIndicator) { return RRecTabIndicator( borderSide: BorderSide.lerp(borderSide, b.borderSide, t), insets: EdgeInsetsGeometry.lerp(insets, b.insets, t)!, ); } return super.lerpTo(b, t); } @override _UnderlinePainter createBoxPainter([VoidCallback? onChanged]) { return _UnderlinePainter(this, onChanged); } Rect _indicatorRectFor(Rect rect, TextDirection textDirection) { final Rect indicator = insets.resolve(textDirection).deflateRect(rect); return Rect.fromLTWH( indicator.left, indicator.bottom - borderSide.width, indicator.width, borderSide.width, ); } @override Path getClipPath(Rect rect, TextDirection textDirection) { return Path()..addRect(_indicatorRectFor(rect, textDirection)); } } class _UnderlinePainter extends BoxPainter { _UnderlinePainter(this.decoration, VoidCallback? onChanged) : super(onChanged); final RRecTabIndicator decoration; @override void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) { final Rect rect = offset & configuration.size!; final TextDirection textDirection = configuration.textDirection!; final Rect indicator = decoration._indicatorRectFor(rect, textDirection); final Paint paint = decoration.borderSide.toPaint() ..strokeCap = StrokeCap.square ..color = decoration.color; final RRect rRect = RRect.fromRectAndRadius(indicator, Radius.circular(decoration.radius)); canvas.drawRRect(rRect, paint); } }
Scaffold( appBar: AppBar( // Here we take the value from the MyHomePage object that was created by // the App.build method, and use it to set our appbar title. title: Text(widget.title), bottom: KuGouTabBar( tabs: const [Tab(text: "音樂"), Tab(text: "動態"), Tab(text: "語文")], // labelPadding: EdgeInsets.symmetric(horizontal: 8), controller: _tabController, // indicatorSize: TabBarIndicatorSize.label, // isScrollable: true, padding: EdgeInsets.zero, indicator: const RRecTabIndicator( radius: 4, insets: EdgeInsets.only(bottom: 5)), indicatorMinWidth: 6, ), ), );
模仿酷狗的Tabbar效果,就分享到這裡啦,重點在於實現步驟的第2、3步,涉及到一些簡單的數學知識。說說心得吧,Flutter UI層面的問題,其實技術棧已經很單一了。只要跟著官方的實現思路,能寫出跟其類似的程式碼,把Rander層理解透徹,筆者認為已經足夠了。往深了還是得往原生、混編、解決Flutter痛點問題為主。 希望一起共勉!!!
https://github.com/WxqKb/KuGouTabbar.git
到此這篇關於如何利用Flutter實現酷狗流暢Tabbar效果的文章就介紹到這了,更多相關Flutter實現酷狗Tabbar效果內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45