Swipe to Slide List Item in Flutter

You are currently viewing Swipe to Slide List Item in Flutter
Swipe to Slide

A Flutter implementation of slidable list item with directional slide actions that can be dismissed.

Features

  • Accepts start (left/top) and end (right/bottom) action panes.
  • Can be dismissed.
  • 4 built-in action panes.
  • 2 built-in slide action widgets.
  • 1 built-in dismiss animation.
  • You can easily create custom layouts and animations.
  • You can use a builder to create your slide actions if you want special effects during animation.
  • Closes when a slide action has been tapped (overridable).
  • Closes when the nearest Scrollable starts to scroll (overridable).
  • Option to disable the slide effect easily.

Install

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  flutter_slidable: <latest_version>

In your library add the following import:

import 'package:flutter_slidable/flutter_slidable.dart';

Getting started

Example:

Slidable(
  // Specify a key if the Slidable is dismissible.
  key: const ValueKey(0),

  // The start action pane is the one at the left or the top side.
  startActionPane: ActionPane(
    // A motion is a widget used to control how the pane animates.
    motion: const ScrollMotion(),

    // A pane can dismiss the Slidable.
    dismissible: DismissiblePane(onDismissed: () {}),

    // All actions are defined in the children parameter.
    children: const [
      // A SlidableAction can have an icon and/or a label.
      SlidableAction(
        onPressed: doNothing,
        backgroundColor: Color(0xFFFE4A49),
        foregroundColor: Colors.white,
        icon: Icons.delete,
        label: 'Delete',
      ),
      SlidableAction(
        onPressed: doNothing,
        backgroundColor: Color(0xFF21B7CA),
        foregroundColor: Colors.white,
        icon: Icons.share,
        label: 'Share',
      ),
    ],
  ),

  // The end action pane is the one at the right or the bottom side.
  endActionPane: const ActionPane(
    motion: ScrollMotion(),
    children: [
      SlidableAction(
        // An action can be bigger than the others.
        flex: 2,
        onPressed: doNothing,
        backgroundColor: Color(0xFF7BC043),
        foregroundColor: Colors.white,
        icon: Icons.archive,
        label: 'Archive',
      ),
      SlidableAction(
        onPressed: doNothing,
        backgroundColor: Color(0xFF0392CF),
        foregroundColor: Colors.white,
        icon: Icons.save,
        label: 'Save',
      ),
    ],
  ),

  // The child of the Slidable is what the user sees when the
  // component is not dragged.
  child: const ListTile(title: Text('Slide me')),
),

Motions

Any ActionPane has a motion parameter which allow you to define how the pane animates when the user drag the Slidable.

Behind Motion

The actions appear as if they where behind the Slidable:

Swipe to Slide
Swipe to Slide

Drawer Motion

Animate the actions as if they were drawers, when the Slidable is moving:

Swipe to Slide
Swipe to Slide

Scroll Motion

The actions follow the Slidable while it’s moving:

Swipe to Slide
Swipe to Slide

Stretch Motion

Animate the actions as if they were streched while the Slidable is moving:

Swipe to Slide
Swipe to Slide

FAQ

How to prevent my SlidableAction to be closed after it has been tapped?

The CustomSlidableAction and SlidableAction widgets have an autoClose constructor parameter with a default value to true. If you don’t want your action to be closed automatically, set the autoClose value to false.

How to prevent my Slidable to close after my list has scrolled?

By default, a Slidable closes when the nearest Scrollable widget starts to scroll. To prevent this, you can pass in false to its closeOnScroll constructor parameter.

See also  Material Range Slider for Flutter

How can I dismiss my Slidable?

By default, a Slidable is not dismissible. To be dismissed, you have to set a DismissiblePane to the dismissible constructor parameter of any ActionPane:

Slidable(
  // A key is mandatory when using a dismissible.
  key: ValueKey(item),

  // You can choose how which action panes respond to dismiss.
  startActionPane: ActionPane(

    // A pane can dismiss the Slidable.
    dismissible: DismissiblePane(onDismissed: () {}),
  )
),

How can I prevent to dismiss one side but not the other?

Just set the dismissible to the action pane you want to be dismissed and not to the other.

See also  Swipe to Confirm Sliding Button in Flutter

How to let the user cancel a dismissal?

You can set the confirmDismiss callback of the dismissal to show a dialog letting the user deciding if they want to confirm the action.

How to keep only one Slidable open?

You will have to set the same groupTag value to all the Slidables that share the same group and add a SlidableNotificationListener above your Slidables.

In the snippet below, the first two Slidables have the same group, so only one of them can be open, while the last Slidable is independent of the others.

SlidableNotificationListener(
  onNotification: (notification) {},
  child: ListView(
    children: [
      Slidable(
        groupTag: '0',
      ),
      Slidable(
        groupTag: '0',
      ),
      Slidable(
        groupTag: '1',
      ),
    ],
  ),
)

How can I open the Slidable programmatically?

You can get a SlidableController inside the child of the Slidable. This controller can be used to open the enclosing Slidable programmatically for example by calling openEndActionPane() or openStartActionPane().

Credit

This is developed by letsar
Download this project from the below link.
https://github.com/letsar/flutter_slidable/archive/refs/heads/master.zip
You can visit original source page from the below link:
https://github.com/letsar/flutter_slidable

See also  Hijri Date Picker in Flutter

Spread the love

Leave a Reply