当前位置: 首页 > web >正文

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

flutter_gen  管理静态资源,包含lottile的管理

CBAnimationBuilder 自动管理controller

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,});@overrideState<StatefulWidget> createState() => _CBAnimationBuilderState();
}class _CBAnimationBuilderState extends State<CBAnimationBuilder>with TickerProviderStateMixin {late final AnimationController controller;late final Animation<double> animation;@overridevoid 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();}}@overridevoid dispose() {controller.dispose();super.dispose();}@overrideWidget 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);},);}
}

使用

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;@overrideWidget 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);}
}

http://www.xdnf.cn/news/13866.html

相关文章:

  • Windows美化终端、安装wsl和docker
  • Python Day49
  • 一阶/二阶Nomoto模型(野本模型)为何“看不到”船速对回转角速度/角加速度的影响?
  • 智慧管廊数字化运维管理平台
  • 规则引擎rule-engine v1.0实现解释(一)-规则,执行,容器
  • Vue3中组件Ref打印Proxy(Object)与defineExpose的深度解析
  • navicat 有免费版了,navicat 官方免费版下载
  • vue2项目开发中遇到的小问题
  • Goland使用手册(1)
  • 【亲测有效】MybatisPlus中MetaObjectHandler自动填充字段失效
  • Tess4J:基于 Java 的 OCR 解决方案
  • php反序列化漏洞学习
  • 电脑PC端使用的备忘录记事软件推荐哪个
  • 【3】使用TRAE AI在已有框架中新增页面并实现切换的痛苦经历
  • boa 找不到动态库的解决办法:
  • 【零基础勇闯嵌入式岗】从单片机低功耗中获得的启发
  • 正则表达式入门
  • 【AIGC】Qwen3-Embedding:Embedding与Rerank模型新标杆
  • 【狂飙AGI】第2课:大模型方向市场分析
  • # Flask:Python的轻量级Web框架入门之旅(超级实用!)
  • 测试过程中有哪些风险?
  • KU115LPE-V10型FPGA加速卡
  • Linux操作系统之文件系统下
  • 友思特方案 | 友思特车载双目相机技术赋能农业:Monarch 智能拖拉机解决方案
  • linux基础day01
  • [25-cv-06422]David律所代理Dreams USA玩具手办商标维权
  • Java日期格式化
  • Swift 解法详解:如何在二叉树中寻找最长连续序列
  • NAS文件共享、PACS影像存储,速率提升400%?
  • PostgreSQL认证怎么选?PGCP中级认证、PGCM高级认证