action_dialog.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. enum ActionType {
  4. eat,
  5. checkMate,
  6. kill,
  7. }
  8. class ActionDialog extends StatefulWidget {
  9. final ActionType type;
  10. final double? delay;
  11. final void Function()? onHide;
  12. const ActionDialog(this.type, {Key? key, this.delay, this.onHide})
  13. : super(key: key);
  14. @override
  15. State<ActionDialog> createState() => _ActionDialogState();
  16. }
  17. String assets(ActionType type) {
  18. switch (type) {
  19. case ActionType.eat:
  20. return 'assets/images/action_eat.png';
  21. case ActionType.checkMate:
  22. return 'assets/images/action_checkmate.png';
  23. case ActionType.kill:
  24. return 'assets/images/action_kill.png';
  25. }
  26. }
  27. class _ActionDialogState extends State<ActionDialog>
  28. with SingleTickerProviderStateMixin {
  29. late AnimationController imageAnimation = AnimationController(
  30. duration: const Duration(milliseconds: 500),
  31. vsync: this,
  32. );
  33. late Completer<bool> showAction;
  34. @override
  35. void initState() {
  36. super.initState();
  37. showAction = Completer();
  38. imageAnimation.addListener(_onAnimate);
  39. imageAnimation.animateTo(1);
  40. if (widget.delay != null) {
  41. Future.delayed(Duration(milliseconds: (widget.delay! * 1000).toInt()))
  42. .then((value) => imageAnimation.animateTo(0));
  43. }
  44. }
  45. @override
  46. void dispose() {
  47. imageAnimation.stop(canceled: true);
  48. imageAnimation.removeListener(_onAnimate);
  49. imageAnimation.value = 0;
  50. if (!showAction.isCompleted) {
  51. showAction.complete(false);
  52. }
  53. super.dispose();
  54. }
  55. void _onAnimate() {
  56. if (showAction.isCompleted) {
  57. if (imageAnimation.value < 1) {
  58. if (imageAnimation.value == 0) {
  59. widget.onHide?.call();
  60. }
  61. setState(() {});
  62. }
  63. } else {
  64. if (imageAnimation.value == 1) {
  65. showAction.complete(true);
  66. }
  67. setState(() {});
  68. }
  69. }
  70. @override
  71. Widget build(BuildContext context) {
  72. return IgnorePointer(
  73. child: SizedBox(
  74. width: 220,
  75. height: 220,
  76. child: Opacity(
  77. opacity: showAction.isCompleted ? imageAnimation.value : 1,
  78. child: Stack(
  79. children: [
  80. Center(
  81. child: FutureBuilder<bool>(
  82. future: showAction.future,
  83. initialData: false,
  84. builder:
  85. (BuildContext context, AsyncSnapshot<bool> snapshot) {
  86. if (snapshot.hasData && snapshot.data!) {
  87. return Image.asset(assets(widget.type));
  88. }
  89. return const SizedBox();
  90. },
  91. ),
  92. ),
  93. Center(
  94. child: Opacity(
  95. opacity: showAction.isCompleted ? 1 : imageAnimation.value,
  96. child: Image.asset('assets/images/action_background.png'),
  97. ),
  98. ),
  99. ],
  100. ),
  101. ),
  102. ),
  103. );
  104. }
  105. }