OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 import 'package:vector_math/vector_math.dart'; | 5 import 'package:vector_math/vector_math.dart'; |
6 | 6 |
7 import '../animation/animation_performance.dart'; | 7 import '../animation/animation_performance.dart'; |
8 import '../animation/curves.dart'; | 8 import '../animation/curves.dart'; |
9 import '../base/lerp.dart'; | 9 import '../base/lerp.dart'; |
10 import '../painting/box_painter.dart'; | 10 import '../painting/box_painter.dart'; |
11 import '../theme/shadows.dart'; | 11 import '../theme/shadows.dart'; |
12 import 'basic.dart'; | 12 import 'basic.dart'; |
13 | 13 |
14 // This class builds a Container object from a collection of optionally- | 14 // This class builds a Container object from a collection of optionally- |
15 // animated properties. Use syncFields to update the Container's properties, | 15 // animated properties. Use syncFields to update the Container's properties, |
16 // which will optionally animate them using an AnimationPerformance. | 16 // which will optionally animate them using an AnimationPerformance. |
17 class AnimatedContainer { | 17 class AnimatedContainer { |
18 AnimatedType<Point> position; | 18 AnimatedType<Point> position; |
19 AnimatedType<double> shadow; | 19 AnimatedType<double> shadow; |
20 AnimatedColor backgroundColor; | 20 AnimatedColor backgroundColor; |
21 | 21 |
| 22 // These don't animate, but are used to build the Container anyway. |
| 23 double borderRadius; |
| 24 Shape shape; |
| 25 |
22 Map<AnimatedVariable, AnimationPerformance> _variableToPerformance = | 26 Map<AnimatedVariable, AnimationPerformance> _variableToPerformance = |
23 new Map<AnimatedVariable, AnimationPerformance>(); | 27 new Map<AnimatedVariable, AnimationPerformance>(); |
24 | 28 |
25 AnimatedContainer(); | 29 AnimatedContainer(); |
26 | 30 |
27 AnimationPerformance createPerformance(AnimatedType variable, | 31 AnimationPerformance createPerformance(AnimatedType variable, |
28 {Duration duration}) { | 32 {Duration duration}) { |
29 AnimationPerformance performance = new AnimationPerformance() | 33 AnimationPerformance performance = new AnimationPerformance() |
30 ..duration = duration | 34 ..duration = duration |
31 ..variable = variable; | 35 ..variable = variable; |
32 _variableToPerformance[variable] = performance; | 36 _variableToPerformance[variable] = performance; |
33 return performance; | 37 return performance; |
34 } | 38 } |
35 | 39 |
36 Widget build(Widget child) { | 40 Widget build(Widget child) { |
37 Widget current = child; | 41 Widget current = child; |
38 if (shadow != null || backgroundColor != null) { | 42 if (shadow != null || backgroundColor != null || |
| 43 borderRadius != null || shape != null) { |
39 current = new DecoratedBox( | 44 current = new DecoratedBox( |
40 decoration: new BoxDecoration( | 45 decoration: new BoxDecoration( |
| 46 borderRadius: borderRadius, |
| 47 shape: shape, |
41 boxShadow: shadow != null ? _computeShadow(shadow.value) : null, | 48 boxShadow: shadow != null ? _computeShadow(shadow.value) : null, |
42 backgroundColor: backgroundColor != null ? backgroundColor.value : nul
l), | 49 backgroundColor: backgroundColor != null ? backgroundColor.value : nul
l), |
43 child: current); | 50 child: current); |
44 } | 51 } |
45 | 52 |
46 if (position != null) { | 53 if (position != null) { |
47 Matrix4 transform = new Matrix4.identity(); | 54 Matrix4 transform = new Matrix4.identity(); |
48 transform.translate(position.value.x, position.value.y); | 55 transform.translate(position.value.x, position.value.y); |
49 current = new Transform(transform: transform, child: child); | 56 current = new Transform(transform: transform, child: child); |
50 } | 57 } |
51 | 58 |
52 return current; | 59 return current; |
53 } | 60 } |
54 | 61 |
55 void syncFields(AnimatedContainer source) { | 62 void syncFields(AnimatedContainer source) { |
56 _syncField(position, source.position); | 63 _syncField(position, source.position); |
57 _syncField(shadow, source.shadow); | 64 _syncField(shadow, source.shadow); |
58 _syncField(backgroundColor, source.backgroundColor); | 65 _syncField(backgroundColor, source.backgroundColor); |
| 66 |
| 67 borderRadius = source.borderRadius; |
| 68 shape = source.shape; |
59 } | 69 } |
60 | 70 |
61 void _syncField(AnimatedType variable, AnimatedType sourceVariable) { | 71 void _syncField(AnimatedType variable, AnimatedType sourceVariable) { |
62 if (variable == null) | 72 if (variable == null) |
63 return; // TODO(mpcomplete): Should we handle transition from null? | 73 return; // TODO(mpcomplete): Should we handle transition from null? |
64 | 74 |
65 AnimationPerformance performance = _variableToPerformance[variable]; | 75 AnimationPerformance performance = _variableToPerformance[variable]; |
66 if (performance == null) { | 76 if (performance == null) { |
67 // If there's no performance, no need to animate. | 77 // If there's no performance, no need to animate. |
68 if (sourceVariable != null) | 78 if (sourceVariable != null) |
(...skipping 27 matching lines...) Expand all Loading... |
96 | 106 |
97 int level1 = level.floor(); | 107 int level1 = level.floor(); |
98 int level2 = level.ceil(); | 108 int level2 = level.ceil(); |
99 double t = level - level1.toDouble(); | 109 double t = level - level1.toDouble(); |
100 | 110 |
101 List<BoxShadow> shadow = new List<BoxShadow>(); | 111 List<BoxShadow> shadow = new List<BoxShadow>(); |
102 for (int i = 0; i < shadows[level1].length; ++i) | 112 for (int i = 0; i < shadows[level1].length; ++i) |
103 shadow.add(lerpBoxShadow(shadows[level1][i], shadows[level2][i], t)); | 113 shadow.add(lerpBoxShadow(shadows[level1][i], shadows[level2][i], t)); |
104 return shadow; | 114 return shadow; |
105 } | 115 } |
OLD | NEW |