| 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 'dart:sky' as sky; | 5 import 'dart:sky' as sky; |
| 6 | 6 |
| 7 import 'package:vector_math/vector_math.dart'; | 7 import 'package:vector_math/vector_math.dart'; |
| 8 import 'package:sky/animation/animation_performance.dart'; | 8 import 'package:sky/animation/animation_performance.dart'; |
| 9 import 'package:sky/base/lerp.dart'; | 9 import 'package:sky/base/lerp.dart'; |
| 10 import 'package:sky/painting/text_style.dart'; | 10 import 'package:sky/painting/text_style.dart'; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 const double _kDismissCardThreshold = 0.70; | 22 const double _kDismissCardThreshold = 0.70; |
| 23 | 23 |
| 24 class CardCollectionApp extends App { | 24 class CardCollectionApp extends App { |
| 25 | 25 |
| 26 final TextStyle cardLabelStyle = | 26 final TextStyle cardLabelStyle = |
| 27 new TextStyle(color: White, fontSize: 18.0, fontWeight: bold); | 27 new TextStyle(color: White, fontSize: 18.0, fontWeight: bold); |
| 28 | 28 |
| 29 CardCollectionApp() { | 29 CardCollectionApp() { |
| 30 _activeCardAnimation = new AnimationPerformance() | 30 _activeCardAnimation = new AnimationPerformance() |
| 31 ..variable = new AnimatedType(0.0, 1.0) | 31 ..variable = new AnimatedType(0.0, 1.0) |
| 32 ..duration = new Duration(milliseconds: _kCardDismissFadeoutMS); | 32 ..duration = new Duration(milliseconds: _kCardDismissFadeoutMS) |
| 33 _activeCardAnimation.timeline.onValueChanged.listen(_handleAnimationProgress
Changed); | 33 ..addListener(_handleAnimationProgressChanged); |
| 34 } | 34 } |
| 35 | 35 |
| 36 int _activeCardIndex = -1; | 36 int _activeCardIndex = -1; |
| 37 AnimationPerformance _activeCardAnimation; | 37 AnimationPerformance _activeCardAnimation; |
| 38 double _activeCardWidth; | 38 double _activeCardWidth; |
| 39 double _activeCardDragX = 0.0; | 39 double _activeCardDragX = 0.0; |
| 40 bool _activeCardDragUnderway = false; | 40 bool _activeCardDragUnderway = false; |
| 41 Set<int> _dismissedCardIndices = new Set<int>(); | 41 Set<int> _dismissedCardIndices = new Set<int>(); |
| 42 | 42 |
| 43 double get _activeCardOpacity => 1.0 - _activeCardAnimation.progress; | 43 double get _activeCardOpacity => 1.0 - _activeCardAnimation.progress; |
| 44 | 44 |
| 45 double get _activeCardOffset { | 45 double get _activeCardOffset { |
| 46 return _activeCardAnimation.progress * _activeCardDragX.sign * _activeCardWi
dth * _kDismissCardThreshold; | 46 return _activeCardAnimation.progress * _activeCardDragX.sign * _activeCardWi
dth * _kDismissCardThreshold; |
| 47 } | 47 } |
| 48 | 48 |
| 49 void _handleAnimationProgressChanged(_) { | 49 void _handleAnimationProgressChanged() { |
| 50 setState(() { | 50 setState(() { |
| 51 if (_activeCardAnimation.isCompleted && !_activeCardDragUnderway) | 51 if (_activeCardAnimation.isCompleted && !_activeCardDragUnderway) |
| 52 _dismissedCardIndices.add(_activeCardIndex); | 52 _dismissedCardIndices.add(_activeCardIndex); |
| 53 }); | 53 }); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void _handleSizeChanged(Size newSize) { | 56 void _handleSizeChanged(Size newSize) { |
| 57 _activeCardWidth = newSize.width; | 57 _activeCardWidth = newSize.width; |
| 58 } | 58 } |
| 59 | 59 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 if (_activeCardWidth == null || _activeCardIndex < 0) | 94 if (_activeCardWidth == null || _activeCardIndex < 0) |
| 95 return; | 95 return; |
| 96 | 96 |
| 97 _activeCardDragUnderway = false; | 97 _activeCardDragUnderway = false; |
| 98 double velocityX = event.velocityX / 1000; | 98 double velocityX = event.velocityX / 1000; |
| 99 if (velocityX.abs() >= _kMinCardFlingVelocity) { | 99 if (velocityX.abs() >= _kMinCardFlingVelocity) { |
| 100 double distance = 1.0 - _activeCardAnimation.progress; | 100 double distance = 1.0 - _activeCardAnimation.progress; |
| 101 if (distance > 0.0) { | 101 if (distance > 0.0) { |
| 102 double duration = 150.0 * distance / velocityX.abs(); | 102 double duration = 150.0 * distance / velocityX.abs(); |
| 103 _activeCardDragX = velocityX.sign; | 103 _activeCardDragX = velocityX.sign; |
| 104 _activeCardAnimation.timeline.animateTo(1.0, duration); | 104 _activeCardAnimation.timeline.animateTo(1.0, duration: duration); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 Widget _buildCard(int index, Color color) { | 109 Widget _buildCard(int index, Color color) { |
| 110 Widget label = new Center(child: new Text("Item ${index}", style: cardLabelS
tyle)); | 110 Widget label = new Center(child: new Text("Item ${index}", style: cardLabelS
tyle)); |
| 111 Widget card = new Card( | 111 Widget card = new Card( |
| 112 child: new Padding(child: label, padding: const EdgeDims.all(8.0)), | 112 child: new Padding(child: label, padding: const EdgeDims.all(8.0)), |
| 113 color: color | 113 color: color |
| 114 ); | 114 ); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 body: _buildCardCollection( | 171 body: _buildCardCollection( |
| 172 [48.0, 64.0, 82.0, 46.0, 60.0, 55.0, 84.0, 96.0, 50.0, | 172 [48.0, 64.0, 82.0, 46.0, 60.0, 55.0, 84.0, 96.0, 50.0, |
| 173 48.0, 64.0, 82.0, 46.0, 60.0, 55.0, 84.0, 96.0, 50.0]) | 173 48.0, 64.0, 82.0, 46.0, 60.0, 55.0, 84.0, 96.0, 50.0]) |
| 174 ); | 174 ); |
| 175 } | 175 } |
| 176 } | 176 } |
| 177 | 177 |
| 178 void main() { | 178 void main() { |
| 179 runApp(new CardCollectionApp()); | 179 runApp(new CardCollectionApp()); |
| 180 } | 180 } |
| OLD | NEW |