Flutter Reusable Lottie Animations技巧
正文

你是否想要在app里面新增一些炫酷的動畫,但是呢?雖然Flutter提供了很多的動畫,但是自己繪制吧,要么效果調(diào)整上過于耗費時間了,要么效果不盡人意。
專業(yè)的事情交給專業(yè)的人,如果動畫是設(shè)計師提供并且能拿來使用,那就太好了?。?!
曾經(jīng)使用過gif,現(xiàn)在發(fā)現(xiàn)lottie動畫,太香了~
封裝相關(guān)加載數(shù)據(jù)使用的lottie動畫
下面是我封裝的有關(guān)加載數(shù)據(jù)使用的lottie動畫
用關(guān)鍵值在枚舉中定義動畫,每個值都是磁盤上動畫文件的名字。
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)建一個基類,所有其他動畫類都從該基類派生。這個類完全負責(zé)使用磁盤上的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動畫了,更多關(guān)于Flutter Lottie Animations的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android視頻播放器屏幕左側(cè)邊隨手指上下滑動亮度調(diào)節(jié)功能的原理實現(xiàn)
這篇文章主要介紹了Android視頻播放器屏幕左側(cè)邊隨手指上下滑動亮度調(diào)節(jié)功能的原理實現(xiàn),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02

