The “Swipe to dismiss” pattern is common in many mobile apps. For example, ifwe’re writing an email app, we might want to allow our users to swipe away emailmessages in a list. When they do, we’ll want to move the item from the Inbox tothe Trash.

Flutter makes this task easy by providing theDismissible Widget.

Directions

  • Create List of Items
  • Wrap each item in a Dismissible Widget
  • Provide “Leave Behind” indicators

1. Create List of Items

First, we’ll create a list of items we can swipe away. For more detailedinstructions on how to create a list, please follow the Working with longlists recipe.

Create a Data Source

In our example, we’ll want 20 sample items to work with. To keep it simple,we’ll generate a List of Strings.

  1. final items = List<String>.generate(20, (i) => "Item ${i + 1}");

Convert the data source into a List

At first, we’ll simply display each item in the List on screen. Users willnot be able to swipe away with these items just yet!

  1. ListView.builder(
  2. itemCount: items.length,
  3. itemBuilder: (context, index) {
  4. return ListTile(title: Text('${items[index]}'));
  5. },
  6. );

2. Wrap each item in a Dismissible Widget

Now that we’re displaying a list of items, we’ll want to give our users theability to swipe each item off the list!

After the user has swiped away the item, we’ll need to run some code to removethe item from the list and display a Snackbar. In a real app, you might need toperform more complex logic, such as removing the item from a web service ordatabase.

This is where theDismissibleWidget comes into play! In our example, we’ll update our itemBuilder functionto return a Dismissible Widget.

  1. Dismissible(
  2. // Each Dismissible must contain a Key. Keys allow Flutter to
  3. // uniquely identify Widgets.
  4. key: Key(item),
  5. // We also need to provide a function that will tell our app
  6. // what to do after an item has been swiped away.
  7. onDismissed: (direction) {
  8. // Remove the item from our data source.
  9. setState(() {
  10. items.removeAt(index);
  11. });
  12. // Show a snackbar! This snackbar could also contain "Undo" actions.
  13. Scaffold
  14. .of(context)
  15. .showSnackBar(SnackBar(content: Text("$item dismissed")));
  16. },
  17. child: ListTile(title: Text('$item')),
  18. );

3. Provide “Leave Behind” indicators

As it stands, our app allows users to swipe items off the List, but it mightnot give them a visual indication of what happens when they do. To provide a cuethat we’re removing items, we’ll display a “Leave Behind” indicator as theyswipe the item off the screen. In this case, a red background!

For this purpose, we’ll provide a background parameter to the Dismissible.

  1. Dismissible(
  2. // Show a red background as the item is swiped away
  3. background: Container(color: Colors.red),
  4. key: Key(item),
  5. onDismissed: (direction) {
  6. setState(() {
  7. items.removeAt(index);
  8. });
  9. Scaffold
  10. .of(context)
  11. .showSnackBar(SnackBar(content: Text("$item dismissed")));
  12. },
  13. child: ListTile(title: Text('$item')),
  14. );

Complete example

  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. void main() {
  4. runApp(MyApp());
  5. }
  6. // MyApp is a StatefulWidget. This allows us to update the state of the
  7. // Widget whenever an item is removed.
  8. class MyApp extends StatefulWidget {
  9. MyApp({Key key}) : super(key: key);
  10. @override
  11. MyAppState createState() {
  12. return MyAppState();
  13. }
  14. }
  15. class MyAppState extends State<MyApp> {
  16. final items = List<String>.generate(3, (i) => "Item ${i + 1}");
  17. @override
  18. Widget build(BuildContext context) {
  19. final title = 'Dismissing Items';
  20. return MaterialApp(
  21. title: title,
  22. theme: ThemeData(
  23. primarySwatch: Colors.blue,
  24. ),
  25. home: Scaffold(
  26. appBar: AppBar(
  27. title: Text(title),
  28. ),
  29. body: ListView.builder(
  30. itemCount: items.length,
  31. itemBuilder: (context, index) {
  32. final item = items[index];
  33. return Dismissible(
  34. // Each Dismissible must contain a Key. Keys allow Flutter to
  35. // uniquely identify Widgets.
  36. key: Key(item),
  37. // We also need to provide a function that tells our app
  38. // what to do after an item has been swiped away.
  39. onDismissed: (direction) {
  40. // Remove the item from our data source.
  41. setState(() {
  42. items.removeAt(index);
  43. });
  44. // Then show a snackbar!
  45. Scaffold.of(context)
  46. .showSnackBar(SnackBar(content: Text("$item dismissed")));
  47. },
  48. // Show a red background as the item is swiped away
  49. background: Container(color: Colors.red),
  50. child: ListTile(title: Text('$item')),
  51. );
  52. },
  53. ),
  54. ),
  55. );
  56. }
  57. }

Dismissible Demo