mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Merge pull request #2482 from HansMuller/dismiss_action
Support undo in the leave-behind demo
This commit is contained in:
commit
efa36de7f0
@ -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(
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
|
@ -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],
|
||||
|
@ -58,7 +58,7 @@ class Dismissable extends StatefulComponent {
|
||||
this.child,
|
||||
this.background,
|
||||
this.secondaryBackground,
|
||||
this.onResized,
|
||||
this.onResize,
|
||||
this.onDismissed,
|
||||
this.direction: DismissDirection.horizontal
|
||||
}) : super(key: key) {
|
||||
@ -79,7 +79,7 @@ class Dismissable extends StatefulComponent {
|
||||
final Widget secondaryBackground;
|
||||
|
||||
/// 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.
|
||||
final DismissDirectionCallback onDismissed;
|
||||
@ -263,12 +263,11 @@ class _DismissableState extends State<Dismissable> {
|
||||
|
||||
void _handleResizeProgressChanged() {
|
||||
if (_resizeController.isCompleted) {
|
||||
if (config.onDismissed != null) {
|
||||
if (config.onDismissed != null)
|
||||
config.onDismissed(_dismissDirection);
|
||||
}
|
||||
} else {
|
||||
if (config.onResized != null)
|
||||
config.onResized();
|
||||
if (config.onResize != null)
|
||||
config.onResize();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ DismissDirection dismissDirection = DismissDirection.horizontal;
|
||||
DismissDirection reportedDismissDirection;
|
||||
List<int> dismissedItems = <int>[];
|
||||
|
||||
void handleOnResized(int item) {
|
||||
void handleOnResize(int item) {
|
||||
expect(dismissedItems.contains(item), isFalse);
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ Widget buildDismissableItem(int item) {
|
||||
key: new ValueKey<int>(item),
|
||||
direction: dismissDirection,
|
||||
onDismissed: (DismissDirection direction) { handleOnDismissed(direction, item); },
|
||||
onResized: () { handleOnResized(item); },
|
||||
onResize: () { handleOnResize(item); },
|
||||
child: new Container(
|
||||
width: itemExtent,
|
||||
height: itemExtent,
|
||||
|
Loading…
Reference in New Issue
Block a user