Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: sky/sdk/lib/widgets/animated_container.dart

Issue 1223073002: AnimatedContainer: generalized Container widget that handles animating values (Closed) Base URL: git@github.com:/domokit/mojo.git@master
Patch Set: . Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/sdk/lib/widgets/animated_component.dart ('k') | sky/sdk/lib/widgets/drawer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 import 'dart:async';
6
7 import 'package:vector_math/vector_math.dart';
8
9 import '../animation/animation_performance.dart';
10 import '../animation/curves.dart';
11 import '../base/lerp.dart';
12 import '../painting/box_painter.dart';
13 import '../theme/shadows.dart';
14 import 'animated_component.dart';
15 import 'basic.dart';
16
17 // This class builds a Container object from a collection of optionally-
18 // animated properties. Use syncFields to update the Container's properties,
19 // which will optionally animate them using an AnimationPerformance.
20 class AnimatedContainer {
21 AnimatedType<Point> position;
22 AnimatedType<double> shadow;
23 AnimatedColor backgroundColor;
24
25 Map<AnimatedVariable, AnimationPerformance> _variableToPerformance =
26 new Map<AnimatedVariable, AnimationPerformance>();
27
28 // TODO(mpcomplete): don't rely on AnimatedComponent.
29 AnimatedComponent _component;
abarth-chromium 2015/07/09 17:51:46 We shouldn't have an animated component...
Matt Perry 2015/07/09 17:59:17 OK. It makes the client code a bit more complicate
30 AnimatedContainer([this._component]);
31
32 AnimationPerformance createPerformance(AnimatedType variable,
33 {Duration duration}) {
34 AnimationPerformance performance = new AnimationPerformance()
35 ..duration = duration
36 ..variable = variable;
37 _variableToPerformance[variable] = performance;
38 if (_component != null)
39 _component.watch(performance.timeline);
40 return performance;
41 }
42
43 Widget build(Widget child) {
44 Widget current = child;
45 if (shadow != null || backgroundColor != null) {
46 current = new DecoratedBox(
47 decoration: new BoxDecoration(
48 boxShadow: shadow != null ? _computeShadow(shadow.value) : null,
49 backgroundColor: backgroundColor != null ? backgroundColor.value : nul l),
50 child: current);
51 }
52
53 if (position != null) {
54 Matrix4 transform = new Matrix4.identity();
55 transform.translate(position.value.x, position.value.y);
56 current = new Transform(transform: transform, child: child);
57 }
58
59 return current;
60 }
61
62 void syncFields(AnimatedContainer source) {
63 _syncField(position, source.position);
64 _syncField(shadow, source.shadow);
65 _syncField(backgroundColor, source.backgroundColor);
66 }
67
68 void _syncField(AnimatedType variable, AnimatedType sourceVariable) {
69 if (variable == null)
70 return; // TODO(mpcomplete): Should we handle transition from null?
71
72 AnimationPerformance performance = _variableToPerformance[variable];
73 if (performance == null) {
74 // If there's no performance, no need to animate.
75 if (sourceVariable != null)
76 variable.value = sourceVariable.value;
77 return;
78 }
79
80 if (variable.value != sourceVariable.value) {
81 variable
82 ..begin = variable.value
83 ..end = sourceVariable.value;
84 performance
85 ..progress = 0.0
86 ..play();
87 }
88 }
89 }
90
91 class AnimatedColor extends AnimatedType<Color> {
92 AnimatedColor(Color begin, {Color end, Curve curve: linear})
93 : super(begin, end: end, curve: curve);
94
95 void setFraction(double t) {
96 value = lerpColor(begin, end, t);
97 }
98 }
99
100 List<BoxShadow> _computeShadow(double level) {
101 if (level < 1.0) // shadows[1] is the first shadow
102 return null;
103
104 int level1 = level.floor();
105 int level2 = level.ceil();
106 double t = level - level1.toDouble();
107
108 List<BoxShadow> shadow = new List<BoxShadow>();
109 for (int i = 0; i < shadows[level1].length; ++i)
110 shadow.add(lerpBoxShadow(shadows[level1][i], shadows[level2][i], t));
111 return shadow;
112 }
OLDNEW
« no previous file with comments | « sky/sdk/lib/widgets/animated_component.dart ('k') | sky/sdk/lib/widgets/drawer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698