最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

flutter仿微信底部圖標漸變功能的實現(xiàn)代碼

 更新時間:2020年04月15日 14:55:46   作者:今天你摸魚了嗎  
這篇文章主要介紹了flutter仿微信底部圖標漸變功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

先給大家展示下效果圖,感覺不錯請參考實例代碼。

實現(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中利用App實現(xiàn)消息推送機制的代碼

    Android中利用App實現(xiàn)消息推送機制的代碼

    Android中利用App實現(xiàn)消息推送機制的代碼,需要的朋友可以參考下。
    2011-05-05
  • Android仿支付寶自定義密碼輸入框及安全鍵盤(密碼鍵盤)

    Android仿支付寶自定義密碼輸入框及安全鍵盤(密碼鍵盤)

    這篇文章主要介紹了Android仿支付寶自定義密碼輸入框及安全鍵盤(密碼鍵盤),需要的朋友可以參考下
    2018-01-01
  • 最新評論

    图木舒克市| 吐鲁番市| 大同市| 富蕴县| 鹿邑县| 华阴市| 陕西省| 固原市| 沐川县| 湘潭市| 奉贤区| 临洮县| 和政县| 柘荣县| 无极县| 大埔县| 张家界市| 巴中市| 三亚市| 云和县| 青铜峡市| 汝阳县| 永宁县| 固镇县| 宜宾市| 辉南县| 沈丘县| 江达县| 马鞍山市| 英德市| 镇远县| 高淳县| 中牟县| 聂荣县| 红河县| 卓资县| 如东县| 长岛县| 宜黄县| 石狮市| 巢湖市|