Commit 30678259 authored by Taym Haddadi's avatar Taym Haddadi Committed by Hans Muller

Add confirmDismiss example to flutter_gallery (#30497)

parent e153883d
...@@ -13,7 +13,8 @@ enum LeaveBehindDemoAction { ...@@ -13,7 +13,8 @@ enum LeaveBehindDemoAction {
reset, reset,
horizontalSwipe, horizontalSwipe,
leftSwipe, leftSwipe,
rightSwipe rightSwipe,
confirmDismiss,
} }
class LeaveBehindItem implements Comparable<LeaveBehindItem> { class LeaveBehindItem implements Comparable<LeaveBehindItem> {
...@@ -43,6 +44,7 @@ class LeaveBehindDemo extends StatefulWidget { ...@@ -43,6 +44,7 @@ class LeaveBehindDemo extends StatefulWidget {
class LeaveBehindDemoState extends State<LeaveBehindDemo> { class LeaveBehindDemoState extends State<LeaveBehindDemo> {
static final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>(); static final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
DismissDirection _dismissDirection = DismissDirection.horizontal; DismissDirection _dismissDirection = DismissDirection.horizontal;
bool _confirmDismiss = true;
List<LeaveBehindItem> leaveBehindItems; List<LeaveBehindItem> leaveBehindItems;
void initListItems() { void initListItems() {
...@@ -77,6 +79,9 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> { ...@@ -77,6 +79,9 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
case LeaveBehindDemoAction.rightSwipe: case LeaveBehindDemoAction.rightSwipe:
_dismissDirection = DismissDirection.startToEnd; _dismissDirection = DismissDirection.startToEnd;
break; break;
case LeaveBehindDemoAction.confirmDismiss:
_confirmDismiss = !_confirmDismiss;
break;
} }
}); });
} }
...@@ -128,6 +133,7 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> { ...@@ -128,6 +133,7 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
body = ListView( body = ListView(
children: leaveBehindItems.map<Widget>((LeaveBehindItem item) { children: leaveBehindItems.map<Widget>((LeaveBehindItem item) {
return _LeaveBehindListItem( return _LeaveBehindListItem(
confirmDismiss: _confirmDismiss,
item: item, item: item,
onArchive: _handleArchive, onArchive: _handleArchive,
onDelete: _handleDelete, onDelete: _handleDelete,
...@@ -166,6 +172,11 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> { ...@@ -166,6 +172,11 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
checked: _dismissDirection == DismissDirection.startToEnd, checked: _dismissDirection == DismissDirection.startToEnd,
child: const Text('Only swipe right'), child: const Text('Only swipe right'),
), ),
CheckedPopupMenuItem<LeaveBehindDemoAction>(
value: LeaveBehindDemoAction.confirmDismiss,
checked: _confirmDismiss,
child: const Text('Confirm dismiss'),
),
], ],
), ),
], ],
...@@ -182,12 +193,14 @@ class _LeaveBehindListItem extends StatelessWidget { ...@@ -182,12 +193,14 @@ class _LeaveBehindListItem extends StatelessWidget {
@required this.onArchive, @required this.onArchive,
@required this.onDelete, @required this.onDelete,
@required this.dismissDirection, @required this.dismissDirection,
@required this.confirmDismiss,
}) : super(key: key); }) : super(key: key);
final LeaveBehindItem item; final LeaveBehindItem item;
final DismissDirection dismissDirection; final DismissDirection dismissDirection;
final void Function(LeaveBehindItem) onArchive; final void Function(LeaveBehindItem) onArchive;
final void Function(LeaveBehindItem) onDelete; final void Function(LeaveBehindItem) onDelete;
final bool confirmDismiss;
void _handleArchive() { void _handleArchive() {
onArchive(item); onArchive(item);
...@@ -214,6 +227,23 @@ class _LeaveBehindListItem extends StatelessWidget { ...@@ -214,6 +227,23 @@ class _LeaveBehindListItem extends StatelessWidget {
else else
_handleDelete(); _handleDelete();
}, },
confirmDismiss: !confirmDismiss ? null : (DismissDirection dismissDirection) async {
switch(dismissDirection) {
case DismissDirection.endToStart:
if (await _showConfirmationDialog(context, 'archive'))
_handleArchive();
break;
case DismissDirection.startToEnd:
if (await _showConfirmationDialog(context, 'delete'))
_handleDelete();
break;
case DismissDirection.horizontal:
case DismissDirection.vertical:
case DismissDirection.up:
case DismissDirection.down:
assert(false);
}
},
background: Container( background: Container(
color: theme.primaryColor, color: theme.primaryColor,
child: const ListTile( child: const ListTile(
...@@ -240,4 +270,30 @@ class _LeaveBehindListItem extends StatelessWidget { ...@@ -240,4 +270,30 @@ class _LeaveBehindListItem extends StatelessWidget {
), ),
); );
} }
Future<bool> _showConfirmationDialog(BuildContext context, String action) {
return showDialog<bool>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Do you want to $action this item?'),
actions: <Widget>[
FlatButton(
child: const Text('Yes'),
onPressed: () {
Navigator.pop(context, true); // showDialog() returns true
},
),
FlatButton(
child: const Text('No'),
onPressed: () {
Navigator.pop(context, false); // showDialog() returns false
},
),
],
);
},
);
}
} }
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