Dismissible widget and Attributes
The Dismissible widget in Flutter is used to dismiss a widget from the screen in response to a swipe gesture. It's commonly used to implement features like swiping to delete items in lists or dismissing pop-up dialogs.
Attributes:
key (Key):
- An optional key to distinguish this Dismissible widget from others.
child (Widget):
- The widget that can be dismissed by swiping.
onDismissed (Function):
- A callback function that is called when the widget has been dismissed.
background (Widget):
- The widget to show behind the child widget while it's being dismissed.
secondaryBackground (Widget):
- The widget to show behind the child widget when swiping in the opposite direction.
confirmDismiss (Future<bool> Function(DismissDirection)):
- An optional callback function that is called to determine whether the widget should be dismissed or not.
Example:
import 'package:flutter/material.dart';
class DismissibleExample extends StatefulWidget {
@override
_DismissibleExampleState createState() => _DismissibleExampleState();
}
class _DismissibleExampleState extends State<DismissibleExample> {
final List<String> items = List.generate(10, (index) => 'Item $index');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dismissible Widget Example'),
),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return Dismissible(
key: Key(items[index]),
direction: DismissDirection.horizontal,
background: Container(
color: Colors.red,
alignment: Alignment.centerLeft,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Icon(Icons.delete, color: Colors.white),
),
),
secondaryBackground: Container(
color: Colors.green,
alignment: Alignment.centerRight,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Icon(Icons.archive, color: Colors.white),
),
),
confirmDismiss: (direction) async {
if (direction == DismissDirection.startToEnd) {
// Confirm deletion
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Delete Item?'),
content: Text('Are you sure you want to delete this item?'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text('Delete'),
),
],
),
);
}
return true; // Allow dismiss for other directions
},
onDismissed: (direction) {
setState(() {
items.removeAt(index);
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(direction == DismissDirection.startToEnd ? 'Item Deleted' : 'Item Archived'),
));
},
child: ListTile(
title: Text(items[index]),
),
);
},
),
);
}
}
void main() {
runApp(MaterialApp(
home: DismissibleExample(),
));
}
Explanation:
In this example, a ListView.builder is used to create a list of items.
Each item in the list is wrapped in a Dismissible widget, allowing it to be dismissed with a swipe gesture.
The background and secondaryBackground attributes are used to display widgets behind the item while it's being swiped in different directions.
The confirmDismiss attribute is used to show a confirmation dialog when an item is swiped from left to right (to delete it).
When an item is dismissed, the onDismissed callback is called, which removes the item from the list and shows a snackbar with a message based on the direction of dismissal.
Subscribe to my newsletter
Read articles from Vinit Mepani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vinit Mepani
Vinit Mepani
"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation. In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology. Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities. In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"