flutter仿微信底部圖標漸變功能的實現(xiàn)代碼
先給大家展示下效果圖,感覺不錯請參考實例代碼。

實現(xiàn)思路
在flutter中,如果想實現(xiàn)上面的頁面切換效果,必然會想到pageView。pageView的controller可以監(jiān)聽到pageView的滾動事件,也可以獲取pageView滾動的位置,所以我們在滾動事件中根據(jù)位置去改變對應(yīng)的圖標顏色就可以實現(xiàn)了。
改變圖標顏色
圖標是從微信中提取出來的,都是webp格式的圖片。要改變圖片顏色可以使用ImageIcon這個組件。
ImageIcon會把一張圖片變成單色圖片,所以只要圖片沒有多色的要求,就可以用這個組件。
既然能改變顏色了,我們也需要知道pageView滾動的時候究竟要改什么顏色。從一個頁面滾動到另一個頁面的過程中,顏色都是線性漸變的,要獲取這個過程中的顏色可以使用flutter的Color類提供的lerp方法,作用是獲取兩種顏色之間的線性差值

里面有3個參數(shù),a和b都是顏色,t是夾在0到1之間的,當t為0時返回a,當t為1時返回b
也就是在滾動事件中,計算出 t ,根據(jù) t 改變圖標顏色就可以實現(xiàn)上面的效果了。
pageController.addListener(() {
int currentPage = pageController.page.toInt();
//當前頁面的page是double類型的, 把它減去當前頁面的int類型就可以得出當前頁面到下一個頁面的偏移量了
double t = pageController.page - currentPage;
//根據(jù)上一次的頁面位置獲得方向
if (lastPage <= pageController.page) {
//向右滑動時currentPage是當前頁
//從當前頁過渡到下一頁
streamController.sink.add(StreamModel(
timeline: t, index: currentPage, gotoIndex: currentPage + 1));
} else {
//向左滑動時currentPage是上一頁
//從當前頁過渡到上一頁
streamController.sink.add(StreamModel(
timeline: t, index: currentPage + 1, gotoIndex: currentPage));
}
lastPage = pageController.page;
});
上面代碼中currentPage的值舉個例子:當前page是1,要滑動到2,那么它的值是1.11...1.21...這樣一直到2,所以在這個過程中currentPage是當前頁。如果當前page是4,要滑動到3的時候,它的值是3.99...3.81...這樣一直到3,在這個過程中currentPage就是上一頁了。
t 的計算就更簡單了,1.11-1=0.11,3.99-3=0.99 .....
管理圖標顏色
因為我是用了自帶的底部導(dǎo)航BottomNavigationBar,在pageController的滾動事件中改變圖標顏色太麻煩了,所以用了Stream來管理圖標的狀態(tài)。使用Stream創(chuàng)建一個多訂閱的管道,讓所有圖標都訂閱它,然后在滑動事件中把需要的數(shù)據(jù)都發(fā)送給所有圖標。
需要的數(shù)據(jù):
class StreamModel {
const StreamModel({this.timeline, this.index, this.gotoIndex});
final double timeline;
final int index;
final int gotoIndex;
}
圖標組件
構(gòu)造方法設(shè)置一個index,方便判斷圖標是哪個。
使用StreamBuilder包住要改變顏色的組件,并且綁定從構(gòu)造函數(shù)設(shè)置的StreamController。
在StreamBuilder中根據(jù)pageView滾動事件傳進來的參數(shù)控制圖標顏色。
class BottomNavIcon extends StatelessWidget {
final StreamController<StreamModel> streamController;
final int index;
final String img;
final String title;
final double fontSize;
Color _color;
Color _activeColor;
final bool isActive;
BottomNavIcon(this.title, this.img, this.index,
{@required this.streamController,
this.isActive = false,
this.fontSize = 18.0,
Color color = Colors.grey,
Color activeColor = Colors.blue}) {
_color = isActive ? activeColor : color;
_activeColor = isActive ? color : activeColor;
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: streamController.stream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
final StreamModel data = snapshot.data;
double t = 0.0;
if (data != null) {
//開始的index
if (data.index == index) {
t = data.index > data.gotoIndex
? data.timeline
: 1.0 - data.timeline;
print("this${data.index}:${t}");
}
//結(jié)束的index
if (data.gotoIndex == index) {
t = data.index > data.gotoIndex
? 1.0 - data.timeline //開始的index大于結(jié)束的index方向向左
: data.timeline; //小于方向向右
//過渡到的圖標顏色的插值超過0.6時, 個人感覺當前顏色和結(jié)束的哪個顏色相差太多,
//所以超過0.6時恢復(fù)默認顏色
t = t >= 0.6 ? 1 : t;
print("goto${data.gotoIndex}:${t}");
}
}
if (t > 0.0 && t < 1.0) {
//color.lerp 獲取兩種顏色之間的線性插值
return Column(
children: <Widget>[
ImageIcon(AssetImage(this.img),
color: Color.lerp(_color, _activeColor, t)),
Text(title,
style: TextStyle(
fontSize: fontSize,
color: Color.lerp(_color, _activeColor, t))),
],
);
}
return Column(
children: <Widget>[
ImageIcon(AssetImage(this.img),
color:
Color.fromRGBO(_color.red, _color.green, _color.blue, 1)),
Text(title,
style: TextStyle(
fontSize: fontSize,
color: Color.fromRGBO(
_color.red, _color.green, _color.blue, 1))),
],
);
});
}
}
圖標的顏色都是當前的(index == data.index)漸漸變淺,要滾動到(index==data.gotoIndex)的圖標顏色漸深
創(chuàng)建多訂閱的管道(Stream)
final StreamController<StreamModel> streamController =
StreamController.broadcast();
加載圖標
for (int i = 0; i < pages.length; i++) {
TabBarModel model = pages[i];
bars.add(
BottomNavigationBarItem(
icon: BottomNavIcon(
model.title,
'assets/images/tabbar_' + model.icon + '_c.webp',
i,
streamController: streamController,
),
activeIcon: BottomNavIcon(
model.title,
'assets/images/tabbar_' + model.icon + '_s.webp',
i,
streamController: streamController,
isActive: true,
),
title: Center(),
),
);
}
上面代碼的title為Center的原因是已經(jīng)在圖標組件中創(chuàng)建了一個顯示標題的組件,方便一起設(shè)置顏色。這里就不需要了,但是它的title不允許為null,所以隨便給它一個高寬都是0的組件
結(jié)語
其實這個效果和微信的不是一模一樣,微信的應(yīng)該是選中圖標疊加到默認圖標上面。默認圖標顏色線性漸變,選中圖標透明度漸變。flutter實現(xiàn)這個用自帶的BottomNavigationBar估計不行,可能需要自定義一個底部導(dǎo)航。
第一次寫技術(shù)文章,感覺有點亂,所以貼下完整的代碼地址:
gist: gist.github.com/327100395/9 …
dartPad: dartpad.dev/9dee2497a99…(圖片讀的是本地的,在dartPad中路徑錯誤,所以圖片不顯示)
相關(guān)文章
Android 自定義彈性ListView控件實例代碼(三種方法)
關(guān)于在Android中實現(xiàn)ListView的彈性效果,有很多不同的方法,網(wǎng)上一搜,也有很多,下面貼出在項目中經(jīng)常用到的兩種實現(xiàn)ListView彈性效果的方法(基本上拿來就可以用),需要的朋友參考下本段代碼2016-01-01
Android利用LitePal操作數(shù)據(jù)庫存取圖片
這篇文章主要為大家詳細介紹了Android利用LitePal操作數(shù)據(jù)庫存取圖片的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
android 使用OkHttp上傳多張圖片的實現(xiàn)代碼
這篇文章主要介紹了android 使用OkHttp上傳多張圖片的相關(guān)資料,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
Android Flutter實現(xiàn)五種酷炫文字動畫效果詳解
animated_text_kit這一動畫庫有多種文字動畫效果,文中將利用它實現(xiàn)五種酷炫的文字動畫:波浪涌動效果、波浪線跳動文字組、彩虹動效、滾動廣告牌效果和打字效果,需要的可以參考一下2022-03-03
Android中利用App實現(xiàn)消息推送機制的代碼
Android仿支付寶自定義密碼輸入框及安全鍵盤(密碼鍵盤)

