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

Unified Diff: sky/sdk/example/widgets/card_collection.dart

Issue 1237713002: Dismissable component (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Updated per review feedback 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sky/sdk/BUILD.gn ('k') | sky/sdk/lib/widgets/dismissable.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/sdk/example/widgets/card_collection.dart
diff --git a/sky/sdk/example/widgets/card_collection.dart b/sky/sdk/example/widgets/card_collection.dart
index 44852f0bff0eb6cc3cea59b80af39c587b90ed51..6cd48e906d99daaa05a5c07fbbe31ce300735e7e 100644
--- a/sky/sdk/example/widgets/card_collection.dart
+++ b/sky/sdk/example/widgets/card_collection.dart
@@ -2,16 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-import 'dart:sky' as sky;
-
-import 'package:vector_math/vector_math.dart';
-import 'package:sky/animation/animation_performance.dart';
import 'package:sky/base/lerp.dart';
import 'package:sky/painting/text_style.dart';
import 'package:sky/theme/colors.dart';
-import 'package:sky/widgets/animation_builder.dart';
import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/card.dart';
+import 'package:sky/widgets/dismissable.dart';
import 'package:sky/widgets/scaffold.dart';
import 'package:sky/widgets/variable_height_scrollable.dart';
import 'package:sky/widgets/theme.dart';
@@ -19,11 +15,6 @@ import 'package:sky/widgets/tool_bar.dart';
import 'package:sky/widgets/widget.dart';
-const int _kCardDismissFadeoutMS = 300;
-const double _kMinFlingVelocity = 700.0;
-const double _kMinFlingVelocityDelta = 400.0;
-const double _kDismissCardThreshold = 0.6;
-
class CardCollectionApp extends App {
final TextStyle cardLabelStyle =
@@ -38,135 +29,36 @@ class CardCollectionApp extends App {
List<int> visibleCardIndices;
- CardCollectionApp() {
- _activeCardTransform = new AnimationBuilder()
- ..position = new AnimatedType<Point>(Point.origin)
- ..opacity = new AnimatedType<double>(1.0, end: 0.0);
-
- _activeCardAnimation = _activeCardTransform.createPerformance(
- [_activeCardTransform.position, _activeCardTransform.opacity],
- duration: new Duration(milliseconds: _kCardDismissFadeoutMS));
- _activeCardAnimation.addListener(_handleAnimationProgressChanged);
-
+ void initState() {
visibleCardIndices = new List.generate(cardHeights.length, (i) => i);
+ super.initState();
}
- int _activeCardIndex = -1;
- AnimationBuilder _activeCardTransform;
- AnimationPerformance _activeCardAnimation;
- double _activeCardWidth;
- double _activeCardDragX = 0.0;
- bool _activeCardDragUnderway = false;
-
- Point get _activeCardDragEndPoint {
- return new Point(_activeCardDragX.sign * _activeCardWidth * _kDismissCardThreshold, 0.0);
- }
-
- void _handleAnimationProgressChanged() {
- setState(() {
- if (_activeCardAnimation.isCompleted && !_activeCardDragUnderway)
- visibleCardIndices.remove(_activeCardIndex);
- });
- }
-
- void _handleSizeChanged(Size newSize) {
- _activeCardWidth = newSize.width;
- _activeCardTransform.position.end = _activeCardDragEndPoint;
- }
-
- void _handlePointerDown(sky.PointerEvent event, int cardIndex) {
- setState(() {
- _activeCardIndex = cardIndex;
- _activeCardDragUnderway = true;
- _activeCardDragX = 0.0;
- _activeCardAnimation.progress = 0.0;
- });
- }
-
- void _handlePointerMove(sky.PointerEvent event) {
- if (_activeCardWidth == null || _activeCardIndex < 0)
- return;
-
- double oldDragX = _activeCardDragX;
- _activeCardDragX += event.dx;
+ void dismissCard(int cardIndex) {
setState(() {
- if (!_activeCardAnimation.isAnimating) {
- if (oldDragX.sign != _activeCardDragX.sign)
- _activeCardTransform.position.end = _activeCardDragEndPoint;
- _activeCardAnimation.progress = _activeCardDragX.abs() / (_activeCardWidth * _kDismissCardThreshold);
- }
+ visibleCardIndices.remove(cardIndex);
});
}
- void _handlePointerUpOrCancel(_) {
- if (_activeCardWidth == null || _activeCardIndex < 0)
- return;
-
- setState(() {
- _activeCardDragUnderway = false;
- if (_activeCardAnimation.isCompleted)
- visibleCardIndices.remove(_activeCardIndex);
- else if (!_activeCardAnimation.isAnimating)
- _activeCardAnimation.progress = 0.0;
- });
- }
-
- bool _isHorizontalFlingGesture(sky.GestureEvent event) {
- double vx = event.velocityX.abs();
- double vy = event.velocityY.abs();
- return vx - vy > _kMinFlingVelocityDelta && vx > _kMinFlingVelocity;
- }
-
- void _handleFlingStart(sky.GestureEvent event) {
- if (_activeCardWidth == null || _activeCardIndex < 0)
- return;
-
- _activeCardDragUnderway = false;
-
- if (_isHorizontalFlingGesture(event)) {
- double distance = 1.0 - _activeCardAnimation.progress;
- if (distance > 0.0) {
- double duration = 250.0 * 1000.0 * distance / event.velocityX.abs();
- _activeCardDragX = event.velocityX.sign;
- _activeCardAnimation.timeline.animateTo(1.0, duration: duration);
- }
- }
- }
-
- Widget _buildCard(int cardIndex) {
- Widget label = new Center(child: new Text("Item ${cardIndex}", style: cardLabelStyle));
- Color color = lerpColor(Red[500], Blue[500], cardIndex / cardHeights.length);
- Widget card = new Card(
- child: new Padding(child: label, padding: const EdgeDims.all(8.0)),
- color: color
- );
-
- // TODO(hansmuller) The code below changes the card's widget tree when
- // the user starts dragging it. Currently this causes Sky to drop the
- // rest of the pointer gesture, see https://github.com/domokit/mojo/issues/312.
- // As a workaround, always create the Transform and Opacity nodes.
- if (cardIndex == _activeCardIndex) {
- card = _activeCardTransform.build(card);
- } else {
- card = new Transform(child: card, transform: new Matrix4.identity());
- card = new Opacity(child: card, opacity: 1.0);
- }
-
- return new Listener(
- key: "$cardIndex",
- child: new Container(child: card, height: cardHeights[cardIndex]),
- onPointerDown: (event) { _handlePointerDown(event, cardIndex); },
- onPointerMove: _handlePointerMove,
- onPointerUp: _handlePointerUpOrCancel,
- onPointerCancel: _handlePointerUpOrCancel,
- onGestureFlingStart: _handleFlingStart
- );
- }
-
Widget _builder(int index) {
if (index >= visibleCardIndices.length)
return null;
- return _buildCard(visibleCardIndices[index]);
+
+ int cardIndex = visibleCardIndices[index];
+ Color color = lerpColor(Red[500], Blue[500], cardIndex / cardHeights.length);
+ Widget label = new Text("Item ${cardIndex}", style: cardLabelStyle);
+ return new Dismissable(
+ key: cardIndex.toString(),
+ onDismissed: () { dismissCard(cardIndex); },
+ child: new Card(
+ color: color,
+ child: new Container(
+ height: cardHeights[cardIndex],
+ padding: const EdgeDims.all(8.0),
+ child: new Center(child: label)
+ )
+ )
+ );
}
Widget build() {
@@ -181,7 +73,7 @@ class CardCollectionApp extends App {
return new Scaffold(
toolbar: new ToolBar(center: new Text('Swipe Away')),
- body: new SizeObserver(child: cardCollection, callback: _handleSizeChanged)
+ body: cardCollection
);
}
}
« no previous file with comments | « sky/sdk/BUILD.gn ('k') | sky/sdk/lib/widgets/dismissable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698