Flutter Reusable Lottie Animations技巧
正文

你是否想要在app里面新增一些炫酷的動(dòng)畫,但是呢?雖然Flutter提供了很多的動(dòng)畫,但是自己繪制吧,要么效果調(diào)整上過于耗費(fèi)時(shí)間了,要么效果不盡人意。
專業(yè)的事情交給專業(yè)的人,如果動(dòng)畫是設(shè)計(jì)師提供并且能拿來使用,那就太好了?。?!
曾經(jīng)使用過gif,現(xiàn)在發(fā)現(xiàn)lottie動(dòng)畫,太香了~
封裝相關(guān)加載數(shù)據(jù)使用的lottie動(dòng)畫
下面是我封裝的有關(guān)加載數(shù)據(jù)使用的lottie動(dòng)畫
用關(guān)鍵值在枚舉中定義動(dòng)畫,每個(gè)值都是磁盤上動(dòng)畫文件的名字。
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)畫類都從該基類派生。這個(gè)類完全負(fù)責(zé)使用磁盤上的assets來呈現(xiàn)lottie動(dòng)畫。
在build方法里面,我們通過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)畫的名字傳遞給父類,你就可以開始是了!
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(),
);
}
}

搞定,接下來我得研究研究 如何制作一個(gè)lottie動(dòng)畫了,更多關(guān)于Flutter Lottie Animations的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android實(shí)現(xiàn)粒子中心擴(kuò)散動(dòng)畫效果
粒子動(dòng)畫效果相比其他動(dòng)畫來說是非常復(fù)雜了的,主要涉及三個(gè)方面,粒子初始化、粒子位移、粒子回收等問題,本篇將實(shí)現(xiàn)兩種動(dòng)畫效果,代碼基本相同,只是旋轉(zhuǎn)速度不一樣,需要的朋友可以參考下2024-02-02
Android封裝的http請(qǐng)求實(shí)用工具類
提供一個(gè)Android封裝的http請(qǐng)求實(shí)用工具類,在做ANDROID網(wǎng)絡(luò)開發(fā)中這個(gè)經(jīng)常要用到,大家可以參考下面的工具類修改成自己的工具2013-11-11
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(七)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的第七篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android開發(fā)之時(shí)間日期組件用法實(shí)例
這篇文章主要介紹了Android開發(fā)之時(shí)間日期組件用法,主要介紹了TimePicker和DatePicker組件,對(duì)于Android程序開發(fā)有不錯(cuò)的借鑒價(jià)值,需要的朋友可以參考下2014-08-08
Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn)
這篇文章主要介紹了Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02

