Dismissible widget and Attributes

Dismissible widget and Attributes

ยท

2 min read

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:

  1. key (Key):

    • An optional key to distinguish this Dismissible widget from others.
  2. child (Widget):

    • The widget that can be dismissed by swiping.
  3. onDismissed (Function):

    • A callback function that is called when the widget has been dismissed.
  4. background (Widget):

    • The widget to show behind the child widget while it's being dismissed.
  5. secondaryBackground (Widget):

    • The widget to show behind the child widget when swiping in the opposite direction.
  6. 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.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย