transition.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'dart:collection';
  2. import 'package:animations/animations.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. ///页面过度动效
  6. class FTransitions {
  7. FTransitions._();
  8. static final _cache = HashMap<String, CustomTransition>();
  9. static CustomTransition get fadeThrough => _findInCache(() => _FadeThrough());
  10. static CustomTransition get sharedAxisHorizontal => _findInCache(
  11. () => _SharedAxis(SharedAxisTransitionType.horizontal),
  12. tag: "x",
  13. );
  14. static CustomTransition get sharedAxisVertical => _findInCache(
  15. () => _SharedAxis(SharedAxisTransitionType.vertical),
  16. tag: "y",
  17. );
  18. static CustomTransition get sharedAxisScaled => _findInCache(
  19. () => _SharedAxis(SharedAxisTransitionType.scaled),
  20. tag: "z",
  21. );
  22. static CustomTransition _findInCache<T extends CustomTransition>(
  23. T Function() builder, {
  24. String? tag,
  25. }) {
  26. final key = tag != null ? '${T.toString()}_$tag' : T.toString();
  27. if (!_cache.containsKey(key)) {
  28. _cache[key] = builder();
  29. }
  30. return _cache[key]!;
  31. }
  32. }
  33. ///公用过度动效轴
  34. class _SharedAxis extends CustomTransition
  35. with CustomTransitionListenableMixin {
  36. _SharedAxis(this.transitionType);
  37. final SharedAxisTransitionType transitionType;
  38. @override
  39. Widget buildTransition(
  40. BuildContext context,
  41. Curve? curve,
  42. Alignment? alignment,
  43. Animation<double> animation,
  44. Animation<double> secondaryAnimation,
  45. Widget child,
  46. ) {
  47. injectAnimation(animation);
  48. return SharedAxisTransition(
  49. animation: animation,
  50. secondaryAnimation: secondaryAnimation,
  51. transitionType: transitionType,
  52. child: child,
  53. );
  54. }
  55. }
  56. ///闪现过度动效
  57. class _FadeThrough extends CustomTransition
  58. with CustomTransitionListenableMixin {
  59. @override
  60. Widget buildTransition(
  61. BuildContext context,
  62. Curve? curve,
  63. Alignment? alignment,
  64. Animation<double> animation,
  65. Animation<double> secondaryAnimation,
  66. Widget child,
  67. ) {
  68. injectAnimation(animation);
  69. return FadeThroughTransition(
  70. animation: animation,
  71. secondaryAnimation: secondaryAnimation,
  72. child: child,
  73. );
  74. }
  75. }
  76. mixin CustomTransitionListenableMixin on CustomTransition {
  77. final _cbMap = <int, ValueChanged<AnimationStatus>>{};
  78. @protected
  79. void injectAnimation(Animation animation) {
  80. animation.addStatusListener((status) {
  81. if (status == AnimationStatus.completed) {
  82. for (var cb in _cbMap.values) {
  83. cb(status);
  84. }
  85. }
  86. });
  87. }
  88. void addAnimationListener(ValueChanged<AnimationStatus> callback) {
  89. _cbMap[callback.hashCode] = callback;
  90. }
  91. void removeAnimationListener(ValueChanged<AnimationStatus> callback) {
  92. _cbMap.remove(callback.hashCode);
  93. }
  94. }