先给大家展示下效果图,感觉不错请参考实例代码。
实现思路
在flutter中,如果想实现上面的页面切换效果,必然会想到pageview。pageview的controller可以监听到pageview的滚动事件,也可以获取pageview滚动的位置,所以我们在滚动事件中根据位置去改变对应的图标颜色就可以实现了。
改变图标颜色
图标是从微信中提取出来的,都是webp格式的图片。要改变图片颜色可以使用imageicon这个组件。
imageicon会把一张图片变成单色图片,所以只要图片没有多色的要求,就可以用这个组件。
既然能改变颜色了,我们也需要知道pageview滚动的时候究竟要改什么颜色。从一个页面滚动到另一个页面的过程中,颜色都是线性渐变的,要获取这个过程中的颜色可以使用flutter的color类提供的lerp方法,作用是获取两种颜色之间的线性差值
里面有3个参数,a和b都是颜色,t是夹在0到1之间的,当t为0时返回a,当t为1时返回b
也就是在滚动事件中,计算出 t ,根据 t 改变图标颜色就可以实现上面的效果了。
pagecontroller.addlistener(() {
int currentpage = pagecontroller.page.toint();
//当前页面的page是double类型的, 把它减去当前页面的int类型就可以得出当前页面到下一个页面的偏移量了
double t = pagecontroller.page - currentpage;
//根据上一次的页面位置获得方向
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 .....
管理图标颜色
因为我是用了自带的底部导航bottomnavigationbar,在pagecontroller的滚动事件中改变图标颜色太麻烦了,所以用了stream来管理图标的状态。使用stream创建一个多订阅的管道,让所有图标都订阅它,然后在滑动事件中把需要的数据都发送给所有图标。
需要的数据:
class streammodel {
const streammodel({this.timeline, this.index, this.gotoindex});
final double timeline;
final int index;
final int gotoindex;
}
图标组件
构造方法设置一个index,方便判断图标是哪个。
使用streambuilder包住要改变颜色的组件,并且绑定从构造函数设置的streamcontroller。
在streambuilder中根据pageview滚动事件传进来的参数控制图标颜色。
class bottomnavicon extends statelesswidget {
final streamcontroller 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}");
}
//结束的index
if (data.gotoindex == index) {
t = data.index > data.gotoindex
? 1.0 - data.timeline //开始的index大于结束的index方向向左
: data.timeline; //小于方向向右
//过渡到的图标颜色的插值超过0.6时, 个人感觉当前颜色和结束的哪个颜色相差太多,
//所以超过0.6时恢复默认颜色
t = t >= 0.6 ? 1 : t;
print("goto${data.gotoindex}:${t}");
}
}
if (t > 0.0 && t < 1.0) {
//color.lerp 获取两种颜色之间的线性插值
return column(
children: [
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: [
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)的图标颜色渐深
创建多订阅的管道(stream)
final streamcontrollerstreamcontroller = 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的原因是已经在图标组件中创建了一个显示标题的组件,方便一起设置颜色。这里就不需要了,但是它的title不允许为null,所以随便给它一个高宽都是0的组件
结语
其实这个效果和微信的不是一模一样,微信的应该是选中图标叠加到默认图标上面。默认图标颜色线性渐变,选中图标透明度渐变。flutter实现这个用自带的bottomnavigationbar估计不行,可能需要自定义一个底部导航。
第一次写技术文章,感觉有点乱,所以贴下完整的代码地址:
gist: gist.github.com/327100395/9 …
dartpad: dartpad.dev/9dee2497a99…(图片读的是本地的,在dartpad中路径错误,所以图片不显示)
醉着的马儿不由缰