Chromium Code Reviews| 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 '../animation/animation_performance.dart'; | 5 import '../animation/animation_performance.dart'; |
| 6 import '../painting/box_painter.dart'; | 6 import '../painting/box_painter.dart'; |
| 7 import 'animated_component.dart'; | 7 import 'animated_component.dart'; |
| 8 import 'animated_container.dart'; | 8 import 'animation_builder.dart'; |
| 9 import 'basic.dart'; | 9 import 'basic.dart'; |
| 10 import 'default_text_style.dart'; | 10 import 'default_text_style.dart'; |
| 11 import 'theme.dart'; | 11 import 'theme.dart'; |
| 12 | 12 |
| 13 enum MaterialType { canvas, card, circle, button } | 13 enum MaterialType { canvas, card, circle, button } |
| 14 | 14 |
| 15 const Map<MaterialType, double> edges = const { | 15 const Map<MaterialType, double> edges = const { |
| 16 MaterialType.canvas: null, | 16 MaterialType.canvas: null, |
| 17 MaterialType.card: 2.0, | 17 MaterialType.card: 2.0, |
| 18 MaterialType.circle: null, | 18 MaterialType.circle: null, |
| 19 MaterialType.button: 2.0, | 19 MaterialType.button: 2.0, |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 const Duration _kAnimateShadowDuration = const Duration(milliseconds: 100); | 22 const Duration _kAnimateShadowDuration = const Duration(milliseconds: 100); |
| 23 const Duration _kAnimateColorDuration = const Duration(milliseconds: 100); | 23 const Duration _kAnimateColorDuration = const Duration(milliseconds: 100); |
| 24 | 24 |
| 25 class Material extends AnimatedComponent { | 25 class Material extends AnimatedComponent { |
| 26 | 26 |
| 27 Material({ | 27 Material({ |
| 28 String key, | 28 String key, |
| 29 this.child, | 29 this.child, |
| 30 MaterialType type: MaterialType.card, | 30 this.type: MaterialType.card, |
| 31 int level: 0, | 31 this.level: 0, |
| 32 Color color: null | 32 this.color |
| 33 }) : super(key: key) { | 33 }) : super(key: key) { |
| 34 if (level == null) level = 0; | 34 assert(level != null); |
| 35 _container = new AnimatedContainer() | 35 } |
| 36 | |
| 37 Widget child; | |
| 38 MaterialType type; | |
| 39 int level; | |
| 40 Color color; | |
| 41 | |
| 42 AnimationBuilder _builder; | |
| 43 | |
| 44 void initState() { | |
| 45 _builder = new AnimationBuilder() | |
| 36 ..shadow = new AnimatedType<double>(level.toDouble()) | 46 ..shadow = new AnimatedType<double>(level.toDouble()) |
| 37 ..backgroundColor = _getBackgroundColor(type, color) | 47 ..backgroundColor = _getBackgroundColor(type, color) |
| 38 ..borderRadius = edges[type] | 48 ..borderRadius = edges[type] |
| 39 ..shape = type == MaterialType.circle ? Shape.circle : Shape.rectangle; | 49 ..shape = type == MaterialType.circle ? Shape.circle : Shape.rectangle; |
| 40 watchPerformance(_container.createPerformance( | 50 watchPerformance(_builder.createPerformance( |
| 41 [_container.shadow], duration: _kAnimateShadowDuration)); | 51 [_builder.shadow], duration: _kAnimateShadowDuration)); |
| 42 watchPerformance(_container.createPerformance( | 52 watchPerformance(_builder.createPerformance( |
| 43 [_container.backgroundColor], duration: _kAnimateColorDuration)); | 53 [_builder.backgroundColor], duration: _kAnimateColorDuration)); |
| 54 super.initState(); | |
| 44 } | 55 } |
| 45 | 56 |
| 46 Widget child; | |
| 47 | |
| 48 AnimatedContainer _container; | |
| 49 | |
| 50 void syncFields(Material source) { | 57 void syncFields(Material source) { |
| 51 child = source.child; | 58 child = source.child; |
| 52 _container.syncFields(source._container); | 59 type = source.type; |
| 60 level = source.level; | |
| 61 color = source.color; | |
| 62 _builder.updateFields( | |
| 63 shadow: new AnimatedType<double>(level.toDouble()), | |
| 64 backgroundColor: _getBackgroundColor(type, color), | |
| 65 borderRadius: edges[type], | |
| 66 shape: type == MaterialType.circle ? Shape.circle : Shape.rectangle | |
| 67 ); | |
|
Matt Perry
2015/07/13 17:37:45
It's unfortunate the amount of boilerplate this ad
| |
| 53 super.syncFields(source); | 68 super.syncFields(source); |
| 54 } | 69 } |
| 55 | 70 |
| 56 AnimatedColor _getBackgroundColor(MaterialType type, Color color) { | 71 AnimatedColor _getBackgroundColor(MaterialType type, Color color) { |
| 57 if (color == null) { | 72 if (color == null) { |
| 58 switch (type) { | 73 switch (type) { |
| 59 case MaterialType.canvas: | 74 case MaterialType.canvas: |
| 60 color = Theme.of(this).canvasColor; | 75 color = Theme.of(this).canvasColor; |
| 61 break; | 76 break; |
| 62 case MaterialType.card: | 77 case MaterialType.card: |
| 63 color = Theme.of(this).cardColor; | 78 color = Theme.of(this).cardColor; |
| 64 break; | 79 break; |
| 65 default: | 80 default: |
| 66 break; | 81 break; |
| 67 } | 82 } |
| 68 } | 83 } |
| 69 return color == null ? null : new AnimatedColor(color); | 84 return color == null ? null : new AnimatedColor(color); |
| 70 } | 85 } |
| 71 | 86 |
| 72 Widget build() { | 87 Widget build() { |
| 73 return _container.build( | 88 return _builder.build( |
| 74 new DefaultTextStyle(style: Theme.of(this).text.body1, child: child) | 89 new DefaultTextStyle(style: Theme.of(this).text.body1, child: child) |
| 75 ); | 90 ); |
| 76 } | 91 } |
| 77 | 92 |
| 78 } | 93 } |
| OLD | NEW |