flutter 的lottie执行一次动画后关闭

复制代码
flutter_gen  管理静态资源,包含lottile的管理

CBAnimationBuilder 自动管理controller

Dart 复制代码
import 'package:flutter/material.dart';

typedef CBAnimationWidgetBuilder = Widget Function(
  BuildContext context,
  Widget? child,
  Animation animation,
  AnimationController controller,
);

typedef CBAnimationEndCallback = void Function(
    Animation animation, AnimationController controller);

class CBAnimationBuilder extends StatefulWidget {
  final Duration duration;
  final bool repeat;
  final Widget? child;
  final CBAnimationWidgetBuilder builder;
  final CBAnimationEndCallback? onEnd;

  const CBAnimationBuilder({
    super.key,
    this.duration = const Duration(milliseconds: 300),
    this.repeat = false,
    this.child,
    required this.builder,
    this.onEnd,
  });

  @override
  State<StatefulWidget> createState() => _CBAnimationBuilderState();
}

class _CBAnimationBuilderState extends State<CBAnimationBuilder>
    with TickerProviderStateMixin {
  late final AnimationController controller;
  late final Animation<double> animation;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      vsync: this,
      duration: widget.duration,
    );
    animation = Tween<double>(begin: 0, end: 1).animate(controller);
    controller.forward();
    if (widget.repeat) {
      controller.repeat();
    }
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: animation,
      child: widget.child,
      builder: (context, child) {
        if (animation.isCompleted || animation.isDismissed) {
          widget.onEnd?.call(animation, controller);
        }
        return widget.builder(context, child, animation, controller);
      },
    );
  }
}

使用

Dart 复制代码
import 'package:bcf/app/bc_widget/animation_builder.dart';
import 'package:bcf/app/bc_widget/bc_widgets.dart';
import 'package:bcf/gen/assets.gen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

enum LottieType {
  peach,
  eggplant,
  fire,
  rainbow,
  chili;

  Widget get lottie {
    return switch (this) {
      LottieType.peach => CBAnimationBuilder(
        repeat: false,
        duration: const Duration(seconds: 1),
        builder: (ctx, Widget? w, a, c) {
          return Assets.lottie.commentAnimatPeach.lottie(controller: c);
        },
        onEnd: (a, c) => Get.back(),
      ),
      LottieType.eggplant => CBAnimationBuilder(
        repeat: false,
        duration: const Duration(seconds: 1),
        builder: (ctx, Widget? w, a, c) {
          return Assets.lottie.commentAnimatEggplant.lottie(controller: c);
        },
        onEnd: (a, c) => Get.back(),
      ),
      LottieType.fire => CBAnimationBuilder(
        repeat: false,
        duration: const Duration(seconds: 1),
        builder: (ctx, Widget? w, a, c) {
          return Assets.lottie.commentAnimatFire.lottie(controller: c);
        },
        onEnd: (a, c) => Get.back(),
      ),
      LottieType.rainbow => CBAnimationBuilder(
        repeat: false,
        duration: const Duration(seconds: 1),
        builder: (ctx, Widget? w, a, c) {
          return Assets.lottie.commentAnimatRainbow.lottie(controller: c);
        },
        onEnd: (a, c) => Get.back(),
      ),
      LottieType.chili => CBAnimationBuilder(
        repeat: false,
        duration: const Duration(seconds: 1),
        builder: (ctx, Widget? w, a, c) {
          return Assets.lottie.commentAnimatChili.lottie(controller: c);
        },
        onEnd: (a, c) => Get.back(),
      ),
    };
  }
}

class CommentLottiePanel extends StatelessWidget {
  const CommentLottiePanel({super.key, this.onComplete});

  final Function(LottieType)? onComplete;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.symmetric(horizontal: 22.w, vertical: 10.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          DefaultInWell(onTap: () => openShowLottie(LottieType.peach), child: Assets.images.icPeach.image(height: 49.h)),
          DefaultInWell(onTap: () => openShowLottie(LottieType.eggplant), child: Assets.images.icEggplant.image(height: 49.h)),
          DefaultInWell(onTap: () => openShowLottie(LottieType.fire), child: Assets.images.icFire.image(height: 49.h)),
          DefaultInWell(onTap: () => openShowLottie(LottieType.rainbow), child: Assets.images.icRainbow.image(height: 49.h)),
          DefaultInWell(onTap: () => openShowLottie(LottieType.chili), child: Assets.images.icChili.image(height: 49.h)),
        ],
      ),
    );
  }

  openShowLottie(LottieType type) async {
    await Get.dialog(
        barrierDismissible: false,
        SizedBox(height: 1.sh, width: 2.sw, child: type.lottie));
    onComplete?.call(type);
  }
}
相关推荐
代码游侠5 分钟前
学习笔记——ESP8266 WiFi模块
服务器·c语言·开发语言·数据结构·算法
行者966 分钟前
Flutter在OpenHarmony平台的文件上传组件深度实践
flutter·harmonyos·鸿蒙
行者969 分钟前
Flutter跨平台开发适配OpenHarmony:进度条组件的深度实践
开发语言·前端·flutter·harmonyos·鸿蒙
cn_mengbei11 分钟前
Flutter for OpenHarmony 实战:RangeSlider 范围滑块详解
flutter
奋斗的小青年!!12 分钟前
Flutter适配OpenHarmony:打造无缝国际化用户体验的实战指南
flutter·harmonyos·鸿蒙
奋斗的小青年!!13 分钟前
Flutter跨平台数据筛选器:深度适配OpenHarmony实战指南
flutter·harmonyos·鸿蒙
DYS_房东的猫14 分钟前
《 C++ 零基础入门教程》第3章:结构体与类 —— 用面向对象组织代码
开发语言·c++
向量引擎17 分钟前
复刻“疯狂的鸽子”?用Python调用Sora2与Gemini-3-Pro实现全自动热点视频流水线(附源码解析)
开发语言·人工智能·python·gpt·ai·ai编程·api调用
CoderCodingNo20 分钟前
【GESP】C++五级练习(贪心思想考点) luogu-P1115 最大子段和
开发语言·c++·算法
a程序小傲22 分钟前
得物Java面试被问:边缘计算的数据同步和计算卸载
java·开发语言·数据库·后端·面试·golang·边缘计算