使用Flutter自帶的TabBar切換動(dòng)畫文字會(huì)有抖動(dòng)問題,也可以修改源碼,我覺得沒必要。
我們可以不使用這兩個(gè)參數(shù)描融,自己通過部件的刷新來實(shí)現(xiàn)切換文字變大的效果就可以解決抖動(dòng)問題。
主要是利用了AnimatedDefaultTextStyle衡蚂。
??
1稼稿、替換tab自實(shí)現(xiàn)text改變動(dòng)畫后,此時(shí)滑動(dòng)切換tabView上面tabbar不會(huì)聯(lián)動(dòng)讳窟,可以用TabController監(jiān)聽實(shí)現(xiàn)
2、TabController監(jiān)聽是在滑動(dòng)結(jié)束后才執(zhí)行的敞恋,會(huì)有一個(gè)延遲效果丽啡,如果效果不滿意可以用NotificationListener包一層監(jiān)聽滑動(dòng)過程實(shí)現(xiàn)迅速動(dòng)畫
3、再進(jìn)一步硬猫,我看有的根據(jù)滑動(dòng)距離實(shí)時(shí)更改文字大小补箍,更麻煩一點(diǎn)改执,但我覺得真沒必要,我的項(xiàng)目就做到第二步坑雅,效果設(shè)計(jì)就覺得可以了
AnimatedScale(
scale: 1 + progress * 0.3,
duration: const Duration(milliseconds: 100),
child: Text(tabName),
),
demo:
import 'package:flutter/material.dart';
class HomePage4 extends StatefulWidget {
@override
_HomePage4State createState() => _HomePage4State();
}
class _HomePage4State extends State<HomePage4> with TickerProviderStateMixin {
final List<String> _tabs = ['哈哈哈1', '哈哈哈2', '哈哈哈3', '哈哈哈4', '哈哈哈5', '哈哈哈6'];
late TabController _controller;
int _currentIndex = 0;
@override
void initState() {
super.initState();
_controller = TabController(length: _tabs.length, vsync: this);
// _controller.addListener(() {
// if (_controller.index == _controller.animation?.value) {
// setState(() {
// _currentIndex = _controller.index;
// });
// }
// });
}
///build
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
height: 100,
color: Colors.blue,
),
SizedBox(
height: 60,
child: TabBar(
isScrollable: true,
labelPadding: const EdgeInsets.only(left: 8, right: 8),
indicator: const BoxDecoration(),
overlayColor: MaterialStateProperty.all(Colors.transparent),
controller: _controller,
// 不使用自帶改變效果
// labelColor: Colors.black,
// labelStyle: const TextStyle(fontSize: 25),
// unselectedLabelColor: Colors.black45,
// unselectedLabelStyle: const TextStyle(fontSize: 15),
// tabs: _tabs!.map((e) => Text(e)).toList(),
tabs: _tabs.asMap().entries.map((e) {
return _animatedTab(e.key, e.value);
}).toList(),
// onTap: (index) {
// setState(() {
// _currentIndex = index;
// });
// },
),
),
Expanded(
child: NotificationListener(
onNotification: (ScrollNotification scrollNotification) {
// 監(jiān)聽實(shí)時(shí)更新辈挂,迅速動(dòng)畫,TabController監(jiān)聽有效的是ScrollEndNotification
if (scrollNotification is ScrollUpdateNotification) {
// 一般頁(yè)面都是左右滾動(dòng)
if (scrollNotification.metrics.axisDirection == AxisDirection.right) {
// 滾動(dòng)百分比裹粤,0.0-1.0
double progress = scrollNotification.metrics.pixels / scrollNotification.metrics.maxScrollExtent;
double unit = 1.0 / _tabs.length;
int index = progress ~/ unit;
if (index != _currentIndex && index < _tabs.length) {
setState(() {
_currentIndex = index;
print('tabbar選中:$_currentIndex');
});
}
}
}
return true;
},
child: TabBarView(
controller: _controller,
children: _tabs.asMap().entries.map((e) {
return _content(e.key, e.value);
}).toList(),
),
),
),
],
),
);
}
Widget _animatedTab(int index, String text) {
TextStyle normalStyle = const TextStyle(
color: Colors.black45,
fontSize: 15,
);
TextStyle selectedStyle = const TextStyle(
color: Colors.black,
fontSize: 25,
);
return Tab(
child: AnimatedDefaultTextStyle(
style: _currentIndex == index ? selectedStyle : normalStyle,
duration: const Duration(milliseconds: 100),
child: Text(text),
),
);
}
Widget _content(int index, String text) {
return Container(
color: Colors.lightBlueAccent,
alignment: Alignment.center,
child: Text('$index-$text'),
);
}
}