import 'dart:collection'; import 'package:flutter/material.dart'; import 'package:flutter_svga/flutter_svga.dart'; import 'package:get/get.dart'; /// SVGA 动画项 class SvgaAnimationItem { final String svgaFile; final int? targetUserId; // 接收礼物的用户ID final int? senderUserId; // 发送礼物的用户ID final String? giftProductId; // 礼物产品ID SvgaAnimationItem({ required this.svgaFile, this.targetUserId, this.senderUserId, this.giftProductId, }); } /// SVGA 动画播放队列管理器 /// 注意:由于 SVGAAnimationController 需要 vsync,实际播放需要在 Widget 中完成 /// 这个管理器只负责管理队列,实际的播放需要通过回调通知外部 Widget class SvgaPlayerManager extends GetxController { static SvgaPlayerManager? _instance; static SvgaPlayerManager get instance { _instance ??= Get.put(SvgaPlayerManager()); return _instance!; } final Queue _animationQueue = Queue(); final Rx currentItem = Rx(null); final RxBool isPlaying = false.obs; /// 添加动画到队列 void addToQueue(SvgaAnimationItem item) { _animationQueue.add(item); _playNext(); } /// 播放下一个动画 void _playNext() { if (isPlaying.value || _animationQueue.isEmpty) { return; } final item = _animationQueue.removeFirst(); currentItem.value = item; isPlaying.value = true; print('✅ SVGA 动画已添加到播放队列: ${item.svgaFile}'); } /// 标记当前动画播放完成 void onAnimationFinished() { print('✅ SVGA 动画播放完成55'); isPlaying.value = false; currentItem.value = null; // 播放下一个 _playNext(); } /// 标记当前动画播放失败 void onAnimationError(String error) { print('❌ SVGA 动画播放失败: $error'); isPlaying.value = false; currentItem.value = null; // 继续播放下一个 _playNext(); } /// 停止当前播放 void stop() { isPlaying.value = false; currentItem.value = null; } /// 清空队列 void clearQueue() { stop(); _animationQueue.clear(); } /// 获取队列长度 int get queueLength => _animationQueue.length; }