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 {
reset,
horizontalSwipe,
leftSwipe,
rightSwipe
rightSwipe,
confirmDismiss,
}
class LeaveBehindItem implements Comparable<LeaveBehindItem> {
......@@ -43,6 +44,7 @@ class LeaveBehindDemo extends StatefulWidget {
class LeaveBehindDemoState extends State<LeaveBehindDemo> {
static final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
DismissDirection _dismissDirection = DismissDirection.horizontal;
bool _confirmDismiss = true;
List<LeaveBehindItem> leaveBehindItems;
void initListItems() {
......@@ -77,6 +79,9 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
case LeaveBehindDemoAction.rightSwipe:
_dismissDirection = DismissDirection.startToEnd;
break;
case LeaveBehindDemoAction.confirmDismiss:
_confirmDismiss = !_confirmDismiss;
break;
}
});
}
......@@ -128,6 +133,7 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
body = ListView(
children: leaveBehindItems.map<Widget>((LeaveBehindItem item) {
return _LeaveBehindListItem(
confirmDismiss: _confirmDismiss,
item: item,
onArchive: _handleArchive,
onDelete: _handleDelete,
......@@ -166,6 +172,11 @@ class LeaveBehindDemoState extends State<LeaveBehindDemo> {
checked: _dismissDirection == DismissDirection.startToEnd,
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 {
@required this.onArchive,
@required this.onDelete,
@required this.dismissDirection,
@required this.confirmDismiss,
}) : super(key: key);
final LeaveBehindItem item;
final DismissDirection dismissDirection;
final void Function(LeaveBehindItem) onArchive;
final void Function(LeaveBehindItem) onDelete;
final bool confirmDismiss;
void _handleArchive() {
onArchive(item);
......@@ -214,6 +227,23 @@ class _LeaveBehindListItem extends StatelessWidget {
else
_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(
color: theme.primaryColor,
child: const ListTile(
......@@ -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