[Day04] Flutter with GetX Lottie animations

Lottie

Lottie源自Airbnb开源, 各类前端的支援度不错,
结论是 可以压榨设计师用AE画出很炫泡的动画後直接放上去.

pubspec.yaml 档案里新增 dependencies
与 assets路径(动画的json档放置路径)

dependencies:
  flutter:
    sdk: flutter

  lottie: ^1.1.0
  equatable: ^2.0.3
  assets:
    - assets/
    - assets/lottie/

新增assets与lottie资料夹
把设计师辛苦设计的json档放到路径底下
https://ithelp.ithome.com.tw/upload/images/20210918/20140296ZXL27Cv96b.png

接下来封装一层 Lottie widget 也可以直接用
这个Loading动画可能不只一个地方会用到, 所以选择封装起来

import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';

class LoadingLottieView extends StatelessWidget {
  const LoadingLottieView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      //後面有用到Getx可以替换成
      //height: Get.height,
      //width: Get.width,
      height: MediaQuery.of(context).size.height * 0.2,
      width: MediaQuery.of(context).size.height * 0.2,
      child: Transform.scale(
        scale: 1.3,
        child: Lottie.asset(
          'assets/lottie/loadingrepeatWhite.json',
          fit: BoxFit.fitHeight,
        ),
      ),
    );
  }
}

接着 实际使用在指定的页面上
上半部的widget用刚刚封装好的LoadingLottieView
下半部使用network url网路资源读取, pub.dev里的范例

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:it_home/component/loadingLottie.dart';
import 'package:it_home/pages/Lottie/LottiePageController.dart';
import 'package:lottie/lottie.dart';

class LottiePage extends GetView<LottiePageController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('LottiePage')),
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Center(
              child: Container(
                color: Colors.blue[100],
                child: LoadingLottieView(),
              ),
            ),
            Lottie.network(
              'https://raw.githubusercontent.com/xvrh/lottie-flutter/master/example/assets/Mobilo/A.json',
              controller: controller.animationController,
            ),
          ],
        ),
      ),
    );
  }
}

在此有使用到GetxController, 将Lottie的动画控制用AnimationController处理

import 'package:flutter/animation.dart';
import 'package:get/get.dart';

class LottiePageController extends GetxController
    with SingleGetTickerProviderMixin {
  late AnimationController animationController;

  @override
  void onInit() {
    animationController = AnimationController(
        duration: const Duration(milliseconds: 1500), vsync: this);

    //只播放一次
    animationController.forward();

    //重复播放,reverse = true 跑完会倒着回放
    animationController.repeat(reverse: true);
    super.onInit();
  }
}

可以使用设计师用AE设计好 输出的json档 
也可用放在网路上的资源(pub.dev范例)
实际画面
https://miro.medium.com/max/404/1*s5W11gt22vQwX7AN8z614w.gif

本篇的GitHub source code

下一篇将为大家介绍 carousel_slider


<<:  [Day 3] 前後端技能这麽多,要选哪个呢?

>>:  [Day14] 引入 crate

第 29 天 - RxSwiftExt

嗨,今天讲讲GitHub - RxSwiftCommunity/RxSwiftExt,再开始IT邦系...

Day15 - BMI计算机

BMI(身体质量指数)是用自己身高、体重的比例,来当作是否过胖的「身高体重指数」 它的计算方法也很简...

30.Action

Vuex的Action,相当於component内的methods,里面宣告并使用方法,但不会直接改...

C# 入门之循环

在 C# 中,支持三类循环: do...while 循环 while 循环 for/foreach ...

Day 28 shared_preferences本机存放区

(一)介绍 Flutter推荐使用者使用shared_prederences储存资料,可以存取少量资...