Support undo in the leave-behind demo

This commit is contained in:
Hans Muller
2016-03-07 17:04:58 -08:00
parent 33ab0ee673
commit 73d7378cee
6 changed files with 26 additions and 11 deletions

View File

@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:collection/collection.dart' show lowerBound;
import 'package:flutter/material.dart';
enum LeaveBehindDemoAction {
@@ -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) {
final ThemeData theme = Theme.of(context);
return new Dismissable(
@@ -78,7 +89,11 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
});
final String action = (direction == DismissDirection.left) ? 'archived' : 'deleted';
_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(

View File

@@ -33,7 +33,7 @@ class SnackBarDemo extends StatelessComponent {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text('This is a SnackBar'),
action: new SnackBarAction(
label: 'Action',
label: 'ACTION',
onPressed: () {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("You pressed the SnackBar's Action")

View File

@@ -1,6 +1,7 @@
name: material_gallery
dependencies:
intl: '>=0.12.4+2 <0.13.0'
collection: '>=1.4.0 <2.0.0'
flutter:
path: ../../packages/flutter

View File

@@ -298,7 +298,7 @@ class CardCollectionState extends State<CardCollection> {
Widget card = new Dismissable(
key: new ObjectKey(cardModel),
direction: _dismissDirection,
onResized: () { _invalidator(<int>[index]); },
onResize: () { _invalidator(<int>[index]); },
onDismissed: (DismissDirection direction) { dismissCard(cardModel); },
child: new Card(
color: _primaryColor[cardModel.color],