| 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/animated_value.dart'; | 5 import '../animation/animated_value.dart'; |
| 6 import '../animation/animation_performance.dart'; |
| 6 import '../painting/box_painter.dart'; | 7 import '../painting/box_painter.dart'; |
| 7 import '../theme/shadows.dart'; | 8 import '../theme/shadows.dart'; |
| 8 import 'animated_component.dart'; | 9 import 'animated_component.dart'; |
| 10 import 'animated_container.dart'; |
| 9 import 'basic.dart'; | 11 import 'basic.dart'; |
| 10 import 'default_text_style.dart'; | 12 import 'default_text_style.dart'; |
| 11 import 'theme.dart'; | 13 import 'theme.dart'; |
| 12 | 14 |
| 13 enum MaterialType { canvas, card, circle, button } | 15 enum MaterialType { canvas, card, circle, button } |
| 14 | 16 |
| 15 const Map<MaterialType, double> edges = const { | 17 const Map<MaterialType, double> edges = const { |
| 16 MaterialType.canvas: null, | 18 MaterialType.canvas: null, |
| 17 MaterialType.card: 2.0, | 19 MaterialType.card: 2.0, |
| 18 MaterialType.circle: null, | 20 MaterialType.circle: null, |
| 19 MaterialType.button: 2.0, | 21 MaterialType.button: 2.0, |
| 20 }; | 22 }; |
| 21 | 23 |
| 22 const double _kAnimateShadowDurationMS = 100.0; | 24 const Duration _kAnimateShadowDuration = const Duration(milliseconds: 100); |
| 23 | 25 const Duration _kAnimateColorDuration = const Duration(milliseconds: 100); |
| 24 List<BoxShadow> _computeShadow(double level) { | |
| 25 if (level < 1.0) // shadows[1] is the first shadow | |
| 26 return null; | |
| 27 | |
| 28 int level1 = level.floor(); | |
| 29 int level2 = level.ceil(); | |
| 30 double t = level - level1.toDouble(); | |
| 31 | |
| 32 List<BoxShadow> shadow = new List<BoxShadow>(); | |
| 33 for (int i = 0; i < shadows[level1].length; ++i) | |
| 34 shadow.add(lerpBoxShadow(shadows[level1][i], shadows[level2][i], t)); | |
| 35 return shadow; | |
| 36 } | |
| 37 | 26 |
| 38 class Material extends AnimatedComponent { | 27 class Material extends AnimatedComponent { |
| 39 | 28 |
| 40 Material({ | 29 Material({ |
| 41 String key, | 30 String key, |
| 42 this.child, | 31 this.child, |
| 43 this.type: MaterialType.card, | 32 this.type: MaterialType.card, |
| 44 int level: 0, | 33 int level: 0, |
| 45 this.color | 34 Color color: null |
| 46 }) : super(key: key) { | 35 }) : super(key: key) { |
| 47 this.level = new AnimatedValue(level == null ? 0.0 : level.toDouble()); | 36 if (level == null) level = 0; |
| 48 watch(this.level); | 37 _container = new AnimatedContainer(this) |
| 38 ..shadow = new AnimatedType<double>(level.toDouble()) |
| 39 ..backgroundColor = new AnimatedColor(_getBackgroundColor(color)); |
| 40 _container.createPerformance([AnimatedContainerSlots.shadow], |
| 41 duration: _kAnimateShadowDuration); |
| 42 _container.createPerformance([AnimatedContainerSlots.backgroundColor], |
| 43 duration: _kAnimateColorDuration); |
| 49 } | 44 } |
| 50 | 45 |
| 51 Widget child; | 46 Widget child; |
| 52 MaterialType type; | 47 MaterialType type; |
| 53 AnimatedValue level; | 48 |
| 54 Color color; | 49 AnimatedContainer _container; |
| 55 | 50 |
| 56 void syncFields(Material source) { | 51 void syncFields(Material source) { |
| 57 child = source.child; | 52 child = source.child; |
| 58 type = source.type; | 53 type = source.type; |
| 59 // TODO(mpcomplete): duration is wrong, because the current level may be | 54 _container.syncFields(source._container); |
| 60 // anything. We really want |rate|. | |
| 61 if (level.value != source.level.value) | |
| 62 level.animateTo(source.level.value.toDouble(), _kAnimateShadowDurationMS); | |
| 63 color = source.color; | |
| 64 super.syncFields(source); | 55 super.syncFields(source); |
| 65 } | 56 } |
| 66 | 57 |
| 67 Color get backgroundColor { | 58 Color _getBackgroundColor(Color color) { |
| 68 if (color != null) | 59 if (color != null) |
| 69 return color; | 60 return color; |
| 70 switch(type) { | 61 switch(type) { |
| 71 case MaterialType.canvas: | 62 case MaterialType.canvas: |
| 72 return Theme.of(this).canvasColor; | 63 return Theme.of(this).canvasColor; |
| 73 case MaterialType.card: | 64 case MaterialType.card: |
| 74 return Theme.of(this).cardColor; | 65 return Theme.of(this).cardColor; |
| 75 default: | 66 default: |
| 76 return null; | 67 return null; |
| 77 } | 68 } |
| 78 } | 69 } |
| 79 | 70 |
| 80 // TODO(mpcomplete): make this animate color changes. | |
| 81 | |
| 82 Widget build() { | 71 Widget build() { |
| 83 return new Container( | 72 return _container.build( |
| 84 decoration: new BoxDecoration( | 73 new Container( |
| 85 boxShadow: _computeShadow(level.value), | 74 // TODO(mpcomplete): move the rest of this decoration into |
| 86 borderRadius: edges[type], | 75 // AnimatedContainer as non-animated values. |
| 87 backgroundColor: backgroundColor, | 76 decoration: new BoxDecoration( |
| 88 shape: type == MaterialType.circle ? Shape.circle : Shape.rectangle | 77 borderRadius: edges[type], |
| 89 ), | 78 shape: type == MaterialType.circle ? Shape.circle : Shape.rectangle |
| 90 child: new DefaultTextStyle(style: Theme.of(this).text.body1, child: child
) | 79 ), |
| 91 ); | 80 child: new DefaultTextStyle(style: Theme.of(this).text.body1, child: chi
ld) |
| 81 )); |
| 92 } | 82 } |
| 93 | 83 |
| 94 } | 84 } |
| OLD | NEW |