- tabbar在中文字體大小變化過程中會(huì)出現(xiàn)抖動(dòng)問題,比較影響使用體驗(yàn)蛤奥,網(wǎng)上文章中有兩種解決方案:
- 方案1:修改flutter源碼,這種方法的弊端是在flutter更新后會(huì)丟失原有改動(dòng)。
- 方案2:自定義tab的child楼誓,在tabbar點(diǎn)擊切換索引時(shí)修改tab的child中TextStyle,這種方案簡單一些名挥,但是會(huì)存在滾動(dòng)切換時(shí)文本變化更不上的情況慌随,也是比較影響用戶體驗(yàn)。
- 我在方案二的基礎(chǔ)上優(yōu)化了一下切換索引的實(shí)現(xiàn)方案躺同,通過監(jiān)聽tabbarView的滾動(dòng)來實(shí)現(xiàn)切換索引阁猜,比原方案用戶體驗(yàn)更好。
代碼:
int _index = 0;
set index(int i) {
if (_index != i) {
_index = i;
setState(() {});
}
}
// tabbarView的滾動(dòng)監(jiān)聽
NotificationListener(
child: TabBarView(
controller: _tabController,
children: [
Container(),
Container(),
Container(),
Container(),
Container(),
],
),
onNotification: (scrollNotification) {
// print('$scrollNotification');
if (scrollNotification is ScrollUpdateNotification) {
double progress = scrollNotification.metrics.pixels /
scrollNotification.metrics.maxScrollExtent;
// 這里需要根據(jù)自己tabbar的數(shù)量修改計(jì)算方法
index = (progress + 0.125) ~/ 0.25;
}
return true;
},
)
ps:如果TabBarView的children里面包含scroll蹋艺,需要children實(shí)現(xiàn)NotificationListener并在onNotification里面返回true剃袍。