import 'dart:collection'; import 'package:flutter/cupertino.dart'; import 'package:yumi/ui_kit/widgets/room/anim/l_gift_animal_view.dart'; class GiftAnimationManager extends ChangeNotifier { Queue pendingAnimationsQueue = Queue(); List animationControllerList = []; //每个控件正在播放的动画 Map giftMap = {0: null, 1: null, 2: null, 3: null}; bool get _controllersReady => animationControllerList.length >= giftMap.length; void enqueueGiftAnimation(LGiftModel giftModel) { pendingAnimationsQueue.add(giftModel); proceedToNextAnimation(); } ///开始播放 proceedToNextAnimation() { if (pendingAnimationsQueue.isEmpty || !_controllersReady) { return; } var playGift = pendingAnimationsQueue.first; for (var key in giftMap.keys) { var value = giftMap[key]; if (value == null) { giftMap[key] = playGift; pendingAnimationsQueue.removeFirst(); notifyListeners(); animationControllerList[key].controller.forward(from: 0); break; } else { if (value.labelId == playGift.labelId) { playGift.giftCount = value.giftCount + playGift.giftCount; if (playGift.sendUserName.isEmpty) { playGift.sendUserName = value.sendUserName; } if (playGift.sendToUserName.isEmpty) { playGift.sendToUserName = value.sendToUserName; } if (playGift.sendUserPic.isEmpty) { playGift.sendUserPic = value.sendUserPic; } if (playGift.giftPic.isEmpty) { playGift.giftPic = value.giftPic; } if (playGift.giftName.isEmpty) { playGift.giftName = value.giftName; } if (playGift.rewardAmountText.isEmpty) { playGift.rewardAmountText = value.rewardAmountText; } playGift.showLuckyRewardFrame = playGift.showLuckyRewardFrame || value.showLuckyRewardFrame; giftMap[key] = playGift; pendingAnimationsQueue.removeFirst(); notifyListeners(); animationControllerList[key].controller.forward(from: 0.45); break; } } } } void attachAnimationControllers(List anins) { animationControllerList = anins; proceedToNextAnimation(); } void cleanupAnimationResources() { pendingAnimationsQueue.clear(); giftMap[0] = null; giftMap[1] = null; giftMap[2] = null; giftMap[3] = null; for (var element in animationControllerList) { element.controller.dispose(); } } void markAnimationAsFinished(int index) { giftMap[index] = null; notifyListeners(); proceedToNextAnimation(); } }