1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | /// @docImport 'package:flutter/material.dart'; |
6 | library; |
7 | |
8 | import 'package:flutter/foundation.dart'; |
9 | |
10 | import 'curves.dart'; |
11 | import 'tween.dart'; |
12 | |
13 | /// Used to override the default parameters of an animation. |
14 | /// |
15 | /// Currently, this class is used by the following widgets: |
16 | /// - [ExpansionTile] |
17 | /// - [MaterialApp] |
18 | /// - [PopupMenuButton] |
19 | /// - [ScaffoldMessengerState.showSnackBar] |
20 | /// - [showBottomSheet] |
21 | /// - [showModalBottomSheet] |
22 | /// |
23 | /// If [duration] and [reverseDuration] are set to [Duration.zero], the |
24 | /// corresponding animation will be disabled. |
25 | /// |
26 | /// All of the parameters are optional. If no parameters are specified, |
27 | /// the default animation will be used. |
28 | @immutable |
29 | class AnimationStyle with Diagnosticable { |
30 | /// Creates an instance of Animation Style class. |
31 | const AnimationStyle({this.curve, this.duration, this.reverseCurve, this.reverseDuration}); |
32 | |
33 | /// Creates an instance of Animation Style class with no animation. |
34 | static const AnimationStyle noAnimation = AnimationStyle( |
35 | duration: Duration.zero, |
36 | reverseDuration: Duration.zero, |
37 | ); |
38 | |
39 | /// When specified, the animation will use this curve. |
40 | final Curve? curve; |
41 | |
42 | /// When specified, the animation will use this duration. |
43 | final Duration? duration; |
44 | |
45 | /// When specified, the reverse animation will use this curve. |
46 | final Curve? reverseCurve; |
47 | |
48 | /// When specified, the reverse animation will use this duration. |
49 | final Duration? reverseDuration; |
50 | |
51 | /// Creates a new [AnimationStyle] based on the current selection, with the |
52 | /// provided parameters overridden. |
53 | AnimationStyle copyWith({ |
54 | final Curve? curve, |
55 | final Duration? duration, |
56 | final Curve? reverseCurve, |
57 | final Duration? reverseDuration, |
58 | }) { |
59 | return AnimationStyle( |
60 | curve: curve ?? this.curve, |
61 | duration: duration ?? this.duration, |
62 | reverseCurve: reverseCurve ?? this.reverseCurve, |
63 | reverseDuration: reverseDuration ?? this.reverseDuration, |
64 | ); |
65 | } |
66 | |
67 | /// Linearly interpolate between two animation styles. |
68 | static AnimationStyle? lerp(AnimationStyle? a, AnimationStyle? b, double t) { |
69 | if (identical(a, b)) { |
70 | return a; |
71 | } |
72 | return AnimationStyle( |
73 | curve: t < 0.5 ? a?.curve : b?.curve, |
74 | duration: t < 0.5 ? a?.duration : b?.duration, |
75 | reverseCurve: t < 0.5 ? a?.reverseCurve : b?.reverseCurve, |
76 | reverseDuration: t < 0.5 ? a?.reverseDuration : b?.reverseDuration, |
77 | ); |
78 | } |
79 | |
80 | @override |
81 | bool operator ==(Object other) { |
82 | if (identical(this, other)) { |
83 | return true; |
84 | } |
85 | if (other.runtimeType != runtimeType) { |
86 | return false; |
87 | } |
88 | return other is AnimationStyle && |
89 | other.curve == curve && |
90 | other.duration == duration && |
91 | other.reverseCurve == reverseCurve && |
92 | other.reverseDuration == reverseDuration; |
93 | } |
94 | |
95 | @override |
96 | int get hashCode => Object.hash(curve, duration, reverseCurve, reverseDuration); |
97 | |
98 | @override |
99 | void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
100 | super.debugFillProperties(properties); |
101 | properties.add(DiagnosticsProperty<Curve>('curve' , curve, defaultValue: null)); |
102 | properties.add(DiagnosticsProperty<Duration>('duration' , duration, defaultValue: null)); |
103 | properties.add(DiagnosticsProperty<Curve>('reverseCurve' , reverseCurve, defaultValue: null)); |
104 | properties.add( |
105 | DiagnosticsProperty<Duration>('reverseDuration' , reverseDuration, defaultValue: null), |
106 | ); |
107 | } |
108 | } |
109 | |