Flutter Reusable Lottie Animations技巧
正文
你是否想要在app里面新增一些炫酷的動畫,但是呢?雖然Flutter提供了很多的動畫,但是自己繪制吧,要么效果調(diào)整上過于耗費時間了,要么效果不盡人意。
專業(yè)的事情交給專業(yè)的人,如果動畫是設計師提供并且能拿來使用,那就太好了!?。?/p>
曾經(jīng)使用過gif,現(xiàn)在發(fā)現(xiàn)lottie動畫,太香了~
封裝相關加載數(shù)據(jù)使用的lottie動畫
下面是我封裝的有關加載數(shù)據(jù)使用的lottie動畫
用關鍵值在枚舉中定義動畫,每個值都是磁盤上動畫文件的名字。
enum LottieAnimation { dataNotFound(name: 'data_not_found'), empty(name: 'empty'), loading(mame: 'loading'), error(name: 'error'), smallError(name: 'small_error'); final String name; const LottieAnimation({ required this.name, }); }
創(chuàng)建一個基類,所有其他動畫類都從該基類派生。這個類完全負責使用磁盤上的assets來呈現(xiàn)lottie動畫。
在build方法里面,我們通過Lottie.asset返回一個實際的小部件。
class LottieAnimationView extends StatelessWidget { final LottieAnimation animation; final bool repeat; final bool reverse; const LottieAnimationView({ super.key, required this.animation, this.repeat = true, this.reverse = false, }); @override Widget build(BuildContext context) => Lottie.asset( animation.fullPath, reverse: reverse, repeat: repeat, ); }
給LottieAnimation增加一個拓展,獲取文件全路徑
extension GetFullPath on LottieAnimation { String get fullPath => 'assets/animationss/$name.json'; }
然后定義子類,只把lottie動畫的名字傳遞給父類,你就可以開始是了!
class EmptyContentsAnimationView extends LottieAnimationView { const EmptyContentssAnimationView({super.key}) : super( animation: LottieAnimation.empty, ); }
測試一下
class HomePage extends StatelessWidget { const HomePage({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Home Page'), ), body: const EmptyCOntentsAnimationView(), ); } }
搞定,接下來我得研究研究 如何制作一個lottie動畫了,更多關于Flutter Lottie Animations的資料請關注腳本之家其它相關文章!
相關文章
Android打開GPS導航并獲取位置信息返回null解決方案
最近在做一個 Android 項目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來與大家分享2013-01-01Android自定義Gallery控件實現(xiàn)3D圖片瀏覽器
這篇文章主要介紹了Android自定義Gallery控件實現(xiàn)3D圖片瀏覽器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Android?WebView開發(fā)之自定義WebView工具框
在WebView頁面長按時會彈出一個復制框,有的時候里面的item不是我們想要,這個時候我們就可以自定義一個工具框。本文就將介紹如何通過WebView自定義工具框,需要的朋友可以參考一下2021-12-12Android實現(xiàn)EventBus登錄界面與傳值(粘性事件)
這篇文章主要為大家詳細介紹了Android實現(xiàn)EventBus登錄界面與傳值,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11Android自定義View實現(xiàn)箭頭沿圓轉動實例代碼
這篇文章主要介紹了Android自定義View實現(xiàn)箭頭沿圓轉動實例代碼,需要的朋友可以參考下2017-09-09