Flutter Reusable Lottie Animations技巧
正文

你是否想要在app里面新增一些炫酷的動(dòng)畫(huà),但是呢?雖然Flutter提供了很多的動(dòng)畫(huà),但是自己繪制吧,要么效果調(diào)整上過(guò)于耗費(fèi)時(shí)間了,要么效果不盡人意。
專業(yè)的事情交給專業(yè)的人,如果動(dòng)畫(huà)是設(shè)計(jì)師提供并且能拿來(lái)使用,那就太好了?。。?/p>
曾經(jīng)使用過(guò)gif,現(xiàn)在發(fā)現(xiàn)lottie動(dòng)畫(huà),太香了~
封裝相關(guān)加載數(shù)據(jù)使用的lottie動(dòng)畫(huà)
下面是我封裝的有關(guān)加載數(shù)據(jù)使用的lottie動(dòng)畫(huà)
用關(guān)鍵值在枚舉中定義動(dòng)畫(huà),每個(gè)值都是磁盤(pán)上動(dòng)畫(huà)文件的名字。
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)建一個(gè)基類,所有其他動(dòng)畫(huà)類都從該基類派生。這個(gè)類完全負(fù)責(zé)使用磁盤(pán)上的assets來(lái)呈現(xiàn)lottie動(dòng)畫(huà)。
在build方法里面,我們通過(guò)Lottie.asset返回一個(gè)實(shí)際的小部件。
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增加一個(gè)拓展,獲取文件全路徑
extension GetFullPath on LottieAnimation {
String get fullPath => 'assets/animationss/$name.json';
}
然后定義子類,只把lottie動(dòng)畫(huà)的名字傳遞給父類,你就可以開(kāi)始是了!
class EmptyContentsAnimationView extends LottieAnimationView {
const EmptyContentssAnimationView({super.key}) : super(
animation: LottieAnimation.empty,
);
}
測(cè)試一下
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(),
);
}
}

搞定,接下來(lái)我得研究研究 如何制作一個(gè)lottie動(dòng)畫(huà)了,更多關(guān)于Flutter Lottie Animations的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android打開(kāi)GPS導(dǎo)航并獲取位置信息返回null解決方案
最近在做一個(gè) Android 項(xiàng)目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡(jiǎn)單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來(lái)與大家分享2013-01-01
android實(shí)現(xiàn)軟件自動(dòng)更新的步驟
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)軟件自動(dòng)更新的步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android自定義Gallery控件實(shí)現(xiàn)3D圖片瀏覽器
這篇文章主要介紹了Android自定義Gallery控件實(shí)現(xiàn)3D圖片瀏覽器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
android notification 的總結(jié)分析
notification是一種出現(xiàn)在任務(wù)欄的提示,特別是在4.0以后notification改進(jìn)了不少,本文內(nèi)容都是基于4.0及4.1以后總結(jié)來(lái)的2013-05-05
Android?WebView開(kāi)發(fā)之自定義WebView工具框
在WebView頁(yè)面長(zhǎng)按時(shí)會(huì)彈出一個(gè)復(fù)制框,有的時(shí)候里面的item不是我們想要,這個(gè)時(shí)候我們就可以自定義一個(gè)工具框。本文就將介紹如何通過(guò)WebView自定義工具框,需要的朋友可以參考一下2021-12-12
Android實(shí)現(xiàn)EventBus登錄界面與傳值(粘性事件)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)EventBus登錄界面與傳值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場(chǎng)評(píng)分示例
本篇文章主要介紹了Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場(chǎng)評(píng)分示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
Android自定義View實(shí)現(xiàn)箭頭沿圓轉(zhuǎn)動(dòng)實(shí)例代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)箭頭沿圓轉(zhuǎn)動(dòng)實(shí)例代碼,需要的朋友可以參考下2017-09-09

