Commit 73d7378c authored by Hans Muller's avatar Hans Muller

Support undo in the leave-behind demo

parent 33ab0ee6
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'package:collection/collection.dart' show lowerBound;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
enum LeaveBehindDemoAction { enum LeaveBehindDemoAction {
...@@ -67,6 +69,15 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> { ...@@ -67,6 +69,15 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
} }
} }
void handleUndo(LeaveBehindItem item) {
int insertionIndex = lowerBound(leaveBehindItems, item,
compare: (LeaveBehindItem a, LeaveBehindItem b) => a.index.compareTo(b.index)
);
setState(() {
leaveBehindItems.insert(insertionIndex, item);
});
}
Widget buildItem(LeaveBehindItem item) { Widget buildItem(LeaveBehindItem item) {
final ThemeData theme = Theme.of(context); final ThemeData theme = Theme.of(context);
return new Dismissable( return new Dismissable(
...@@ -78,7 +89,11 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> { ...@@ -78,7 +89,11 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
}); });
final String action = (direction == DismissDirection.left) ? 'archived' : 'deleted'; final String action = (direction == DismissDirection.left) ? 'archived' : 'deleted';
_scaffoldKey.currentState.showSnackBar(new SnackBar( _scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text('You $action item ${item.index}') content: new Text('You $action item ${item.index}'),
action: new SnackBarAction(
label: 'UNDO',
onPressed: () { handleUndo(item); }
)
)); ));
}, },
background: new Container( background: new Container(
......
...@@ -33,7 +33,7 @@ class SnackBarDemo extends StatelessComponent { ...@@ -33,7 +33,7 @@ class SnackBarDemo extends StatelessComponent {
Scaffold.of(context).showSnackBar(new SnackBar( Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text('This is a SnackBar'), content: new Text('This is a SnackBar'),
action: new SnackBarAction( action: new SnackBarAction(
label: 'Action', label: 'ACTION',
onPressed: () { onPressed: () {
Scaffold.of(context).showSnackBar(new SnackBar( Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("You pressed the SnackBar's Action") content: new Text("You pressed the SnackBar's Action")
......
name: material_gallery name: material_gallery
dependencies: dependencies:
intl: '>=0.12.4+2 <0.13.0' intl: '>=0.12.4+2 <0.13.0'
collection: '>=1.4.0 <2.0.0'
flutter: flutter:
path: ../../packages/flutter path: ../../packages/flutter
......
...@@ -298,7 +298,7 @@ class CardCollectionState extends State<CardCollection> { ...@@ -298,7 +298,7 @@ class CardCollectionState extends State<CardCollection> {
Widget card = new Dismissable( Widget card = new Dismissable(
key: new ObjectKey(cardModel), key: new ObjectKey(cardModel),
direction: _dismissDirection, direction: _dismissDirection,
onResized: () { _invalidator(<int>[index]); }, onResize: () { _invalidator(<int>[index]); },
onDismissed: (DismissDirection direction) { dismissCard(cardModel); }, onDismissed: (DismissDirection direction) { dismissCard(cardModel); },
child: new Card( child: new Card(
color: _primaryColor[cardModel.color], color: _primaryColor[cardModel.color],
......
...@@ -58,7 +58,7 @@ class Dismissable extends StatefulComponent { ...@@ -58,7 +58,7 @@ class Dismissable extends StatefulComponent {
this.child, this.child,
this.background, this.background,
this.secondaryBackground, this.secondaryBackground,
this.onResized, this.onResize,
this.onDismissed, this.onDismissed,
this.direction: DismissDirection.horizontal this.direction: DismissDirection.horizontal
}) : super(key: key) { }) : super(key: key) {
...@@ -79,7 +79,7 @@ class Dismissable extends StatefulComponent { ...@@ -79,7 +79,7 @@ class Dismissable extends StatefulComponent {
final Widget secondaryBackground; final Widget secondaryBackground;
/// Called when the widget changes size (i.e., when contracting before being dismissed). /// Called when the widget changes size (i.e., when contracting before being dismissed).
final VoidCallback onResized; final VoidCallback onResize;
/// Called when the widget has been dismissed, after finishing resizing. /// Called when the widget has been dismissed, after finishing resizing.
final DismissDirectionCallback onDismissed; final DismissDirectionCallback onDismissed;
...@@ -263,12 +263,11 @@ class _DismissableState extends State<Dismissable> { ...@@ -263,12 +263,11 @@ class _DismissableState extends State<Dismissable> {
void _handleResizeProgressChanged() { void _handleResizeProgressChanged() {
if (_resizeController.isCompleted) { if (_resizeController.isCompleted) {
if (config.onDismissed != null) { if (config.onDismissed != null)
config.onDismissed(_dismissDirection); config.onDismissed(_dismissDirection);
}
} else { } else {
if (config.onResized != null) if (config.onResize != null)
config.onResized(); config.onResize();
} }
} }
......
...@@ -13,7 +13,7 @@ DismissDirection dismissDirection = DismissDirection.horizontal; ...@@ -13,7 +13,7 @@ DismissDirection dismissDirection = DismissDirection.horizontal;
DismissDirection reportedDismissDirection; DismissDirection reportedDismissDirection;
List<int> dismissedItems = <int>[]; List<int> dismissedItems = <int>[];
void handleOnResized(int item) { void handleOnResize(int item) {
expect(dismissedItems.contains(item), isFalse); expect(dismissedItems.contains(item), isFalse);
} }
...@@ -28,7 +28,7 @@ Widget buildDismissableItem(int item) { ...@@ -28,7 +28,7 @@ Widget buildDismissableItem(int item) {
key: new ValueKey<int>(item), key: new ValueKey<int>(item),
direction: dismissDirection, direction: dismissDirection,
onDismissed: (DismissDirection direction) { handleOnDismissed(direction, item); }, onDismissed: (DismissDirection direction) { handleOnDismissed(direction, item); },
onResized: () { handleOnResized(item); }, onResize: () { handleOnResize(item); },
child: new Container( child: new Container(
width: itemExtent, width: itemExtent,
height: itemExtent, height: itemExtent,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment