Dismissible lets users swipe to remove items from a list. It makes apps feel interactive and easy to use.
Dismissible for swipe actions in Flutter
Dismissible( key: Key('unique_key'), onDismissed: (direction) { // action when dismissed }, child: WidgetToDismiss, )
The key must be unique to identify the widget.
onDismissed runs when the user finishes the swipe.
Dismissible( key: Key('item1'), onDismissed: (direction) { print('Item dismissed'); }, child: ListTile(title: Text('Swipe me')), )
Dismissible( key: Key('item2'), background: Container(color: Colors.red), onDismissed: (direction) { // Remove item from list }, child: ListTile(title: Text('Swipe to delete')), )
This app shows a list of 5 items. Swipe left or right on any item to remove it. A red background with a trash icon appears while swiping. After dismissal, a message appears at the bottom.
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Dismissible Demo')), body: const DismissibleList(), ), ); } } class DismissibleList extends StatefulWidget { const DismissibleList({super.key}); @override State<DismissibleList> createState() => _DismissibleListState(); } class _DismissibleListState extends State<DismissibleList> { final List<String> items = List<String>.generate(5, (i) => 'Item ${i + 1}'); @override Widget build(BuildContext context) { return ListView.builder( itemCount: items.length, itemBuilder: (context, index) { final item = items[index]; return Dismissible( key: Key(item), background: Container(color: Colors.red, alignment: Alignment.centerLeft, padding: const EdgeInsets.only(left: 20), child: const Icon(Icons.delete, color: Colors.white)), secondaryBackground: Container(color: Colors.red, alignment: Alignment.centerRight, padding: const EdgeInsets.only(right: 20), child: const Icon(Icons.delete, color: Colors.white)), onDismissed: (direction) { setState(() { items.removeAt(index); }); ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('$item dismissed'))); }, child: ListTile(title: Text(item)), ); }, ); } }
Always provide a unique key for each Dismissible widget to avoid errors.
You can customize the swipe background using background and secondaryBackground for left and right swipes.
Use onDismissed to update your data source and UI after an item is swiped away.
Dismissible lets users swipe to remove items easily.
Provide a unique key and handle onDismissed to update your list.
Customize swipe backgrounds to improve user experience.
Practice
Dismissible widget in Flutter?Solution
Step 1: Understand the widget's function
TheDismissiblewidget is designed to let users swipe list items to remove them.Step 2: Compare options with the widget's purpose
Only To allow users to swipe and remove items from a list describes swiping to remove items, which matchesDismissible's role.Final Answer:
To allow users to swipe and remove items from a list -> Option CQuick Check:
Dismissible = swipe to remove [OK]
- Confusing Dismissible with buttons or animations
- Thinking it manages image layouts
- Assuming it changes text colors
Dismissible widget?Solution
Step 1: Review key types for Dismissible
Flutter requires a unique key for eachDismissible.Key,UniqueKey(), andValueKey()are all valid ways to create unique keys.Step 2: Confirm all options are valid
Each option shows a valid key assignment method, so all are correct.Final Answer:
All of the above -> Option DQuick Check:
Any unique key type works [OK]
- Using non-unique keys like constant strings for all items
- Omitting the key parameter
- Using keys that are not unique per item
Dismissible(
key: Key('item1'),
onDismissed: (direction) {
items.removeAt(0);
},
child: ListTile(title: Text('Item 1')),
)Solution
Step 1: Understand onDismissed callback
TheonDismissedcallback removes the first item from theitemslist when swiped.Step 2: Analyze the effect of removing item at index 0
Removing at index 0 deletes the first item, so the list updates accordingly.Final Answer:
The first item is removed from the list -> Option AQuick Check:
onDismissed removes item at index 0 [OK]
- Assuming key error prevents removal
- Forgetting to update the list state
- Thinking swipe duplicates items
Dismissible usage?Dismissible(
key: Key('item'),
onDismissed: (direction) {
items.removeAt(index);
},
child: ListTile(title: Text('Item')),
)Solution
Step 1: Check the key uniqueness
The key isKey('item')which is constant and not unique per item, causing issues in lists.Step 2: Verify other parts
TheonDismissedcallback exists, child is a ListTile, andindexis assumed defined, so no issues there.Final Answer:
The key is not unique for each item -> Option AQuick Check:
Keys must be unique per Dismissible [OK]
- Using same key for all items
- Ignoring key importance in lists
- Assuming onDismissed is optional
Dismissible item to the left. Which code snippet correctly implements this?Solution
Step 1: Understand background property
Thebackgroundexpects a widget behind the item during swipe, usually a colored container with an icon.Step 2: Check each option's correctness
The snippet withContainer(color: Colors.red, child: Icon(Icons.delete, color: Colors.white))provides a red background with a visible white trash icon. Others lack a container, the icon, or use colors that do not produce a red background with a visible trash icon.Final Answer:
background: Container(color: Colors.red, child: Icon(Icons.delete, color: Colors.white)) -> Option BQuick Check:
Red container + white icon = correct swipe background [OK]
- Using icon alone without container
- Wrong colors making icon invisible
- Missing icon in background
Ready to prove it?
Pass the test and this pattern moves unknown
