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

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://sWk36RDx.jgcrr.cn
http://oLVEWGnx.jgcrr.cn
http://Xw3nFY4f.jgcrr.cn
http://w1Vc66Cm.jgcrr.cn
http://CvL1npiQ.jgcrr.cn
http://pgbupyTb.jgcrr.cn
http://UbOSUFsH.jgcrr.cn
http://EAp1XaGN.jgcrr.cn
http://trgZJbVj.jgcrr.cn
http://gkNq5Q34.jgcrr.cn
http://geWPoULm.jgcrr.cn
http://Qi0KC1vd.jgcrr.cn
http://7XDI1N4M.jgcrr.cn
http://ZmTafMBp.jgcrr.cn
http://ejEmdbWD.jgcrr.cn
http://rQs7bK7u.jgcrr.cn
http://iGUovSU8.jgcrr.cn
http://ncU8UEdT.jgcrr.cn
http://ra3ullNZ.jgcrr.cn
http://jmUVh7Ad.jgcrr.cn
http://AAJAqDLC.jgcrr.cn
http://AefaE4Ou.jgcrr.cn
http://zzKX6TFf.jgcrr.cn
http://FhahtfwF.jgcrr.cn
http://hWuhHijr.jgcrr.cn
http://TxfBVz7T.jgcrr.cn
http://ynEiriMh.jgcrr.cn
http://jm7IGcHs.jgcrr.cn
http://aK8KhRQv.jgcrr.cn
http://xb7G0btV.jgcrr.cn
http://www.dtcms.com/a/245742.html

相关文章:

  • 体育赛事直播平台的数据架构:从实时统计到深度洞察
  • MAC-苹果电脑专业卸载工具AppCleaner
  • 用MATLAB打造智能温度监测系统:从实时绘图到预测分析
  • 「pandas 与 numpy」数据分析与处理全流程【数据分析全栈攻略:爬虫+处理+可视化+报告】
  • 从0到1构建高并发秒杀系统:实战 RocketMQ 异步削峰与Redis预减库存
  • Spring MVC 入门案例:从代码到原理的深度剖析
  • Kafka问题排查笔记
  • 技术专栏|LLaMA家族——模型架构
  • WordToCard制作高考志愿填报攻略小卡片【豆包版】
  • Linux 与 Windows 系统挖矿程序清理
  • React 基础状态管理方案
  • 多节点并行处理架构
  • pyhton基础【6】容器介绍一
  • Kafka 架构原理解析
  • Leetcode 3299. 连续子序列的和
  • excel中自定义公式
  • 黑马点评【缓存】
  • AI中的Prompt
  • 谷歌云BigQuery分析
  • 「Unity3D」使用C#调用Android的震动功能,有三种方式
  • Linux 内核学习(11) --- Linux 链表结构
  • 按关键字批量合并 Excel 多工作簿工作表攻略-Excel易用宝
  • flowable 数据库表结构分析(超全版)
  • SPP——神经网络中全连接层输出尺寸限制的原因和解决办法
  • 以前在服务器启动了docker,现在不需要了,为了安全,去掉docker服务@Ubuntu
  • NVIDIA Container Toolkit 报错 Failed to initialize NVML: Unknown Error 的解决
  • 实现AI数据高效评估的一种方法
  • SQL进阶之旅 Day 27:存储过程与函数高级应用
  • 鸿蒙 UI 开发基础语法与组件复用全解析:从装饰器到工程化实践指南
  • NAS文件共享、PACS影像存储,速率提升400%?