flutter_slidable

repository·master·Indexed 25 days ago

https://github.com/letsar/flutter_slidable

A Flutter Favorite package that provides slidable list items with directional slide actions and dismissal capabilities. It includes the Slidable widget for creating action panes with various motion effects (BehindMotion, DrawerMotion, ScrollMotion, StretchMotion), SlidableAction and CustomSlidableAction for defining pane content, and a SlidableController for programmatic control of pane states and ratios.

Tokens
4.5K
Snippets
7
Records
27
Agent score
83%

What's inside flutter_slidable

  1. Implement a basic Slidable widget

    master

    Use the Slidable widget to create list items with directional slide actions. You can define a startActionPane (left/top) and an endActionPane (right/bottom). Each pane uses an ActionPane with a specified motion and a list of SlidableAction children.

    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')),
    ),
  2. Control Slidable programmatically with SlidableController

    master

    Use a SlidableController to open or close action panes via code. Pass the controller instance to the Slidable widget's controller property.

    final controller = SlidableController();
    
    // ...
    
    Slidable(
      controller: controller,
      // ...
    );
    
    // ...
    
    // Open the actions
    void _handleOpen() {
      controller.openEndActionPane();
      // OR
      //controller.openStartActionPane();
    }
    
    void _handleClose() {
      controller.close();
    }
  3. Configure ActionPane motions

    master

    The motion parameter in an ActionPane defines how the actions animate when the user drags the Slidable. Available motions include:

    • BehindMotion: Actions appear as if they are behind the Slidable.
    • DrawerMotion: Actions animate as if they were drawers.
    • ScrollMotion: Actions follow the Slidable while it is moving.
    • StretchMotion: Actions animate as if they were being stretched.
  4. Handle dismissal errors in Slidable widgets

    master

    If you encounter a FlutterError stating "A dismissed Slidable widget is still part of the tree.", it means the Slidable widget was not removed from the widget tree immediately after the dismissal animation completed.

    To fix this:

    1. Ensure you have implemented the onDismissed handler within your ActionPane.
    2. Immediately remove the Slidable widget from your application's widget tree once the onDismissed handler has fired.
  5. Access ActionPaneData via context

    master

    You can access the ambient data of the current ActionPane from its children using the static ActionPane.of(context) method. This returns an ActionPaneData object containing:

    • extentRatio: The total extent of the pane relative to the enclosing Slidable.
    • alignment: The Alignment used by the pane.
    • direction: The Axis in which the slidable moves.
    • fromStart: A boolean indicating if this is the start action pane.
    • children: The list of action widgets in this pane.
  6. Monitor Slidable state via SlidableController ValueNotifiers

    master

    The SlidableController exposes several ValueNotifier properties that you can listen to for reacting to state changes:

    • actionPaneType: A ValueNotifier<ActionPaneType> indicating if the current pane is start, end, or none.
    • direction: A ValueNotifier<int> representing movement direction (-1 for left, 0 for stationary, 1 for right).
    • endGesture: A ValueNotifier<EndGesture?> that tracks the end of a gesture (e.g., OpeningGesture, ClosingGesture, or StillGesture).
    • dismissGesture: A ValueNotifier<DismissGesture?> tracking dismissal intentions.
    • resizeRequest: A ValueNotifier<ResizeRequest?> that emits when a dismiss() call completes, providing a duration and a callback.
  7. Programmatically control Slidable state with SlidableController

    master

    Use SlidableController to control a Slidable widget from outside its scope. You must provide a TickerProvider (e.g., via vsync) during initialization. The controller allows you to open specific action panes, close the slidable, or move it to a specific ratio via animations.

    Key methods:

    • openStartActionPane({Duration duration, Curve curve}): Opens the start action pane.
    • openEndActionPane({Duration duration, Curve curve}): Opens the end action pane.
    • openCurrentActionPane({Duration duration, Curve curve}): Opens whichever action pane is currently active.
    • openTo(double ratio, {Duration duration, Curve curve}): Opens the slidable to a specific ratio between -1 and 1. A negative ratio opens the end pane, and a positive ratio opens the start pane.
    • close({Duration duration, Curve curve}): Closes the slidable.
    • dismiss(ResizeRequest request, {Duration duration, Curve curve}): Dismisses the slidable and triggers a ResizeRequest.

    Note: Always call dispose() on the controller when it is no longer needed to prevent memory leaks.

  8. Dispatch notifications using SlidableGroupNotification

    master

    To manually trigger a notification within a SlidableGroupBehavior scope, use the SlidableGroupNotification static methods. This is useful for custom interactions or sending a final notification during a widget's disposal.

    • dispatch<T>(BuildContext context, T notification, {bool assertParentExists = true}): Sends a notification of type T to the nearest SlidableGroupBehavior<T>.
    • createDispatcher<T>(BuildContext context, {bool assertParentExists = true}): Returns a SlidableGroupNotificationDispatcher<T> which can be used to call .dispatch(notification) later (e.g., after a BuildContext is no longer valid for direct dispatch).
  9. Use DismissiblePane to manage Slidable dismissal

    master

    The DismissiblePane widget controls how a Slidable widget behaves when a user performs a dismiss gesture. It allows you to define thresholds, durations, and confirmation logic before an item is removed.

    Important Requirement: You must set a Key on the enclosing Slidable widget for DismissiblePane to work correctly. Without a key, list items may sync incorrectly after a dismissal.

    Key Properties

    • onDismissed: A VoidCallback called after the dismissal and resizing animations complete.
    • dismissThreshold: A value between 0 and 1 (exclusive) that determines how far the user must drag to trigger a dismissal. Defaults to 0.75.
    • confirmDismiss: An optional ConfirmDismissCallback (a Future<bool> Function()) that allows you to show a dialog or perform logic to confirm or veto the dismissal. If it returns false, the item returns to its original position.
    • closeOnCancel: If true, the Slidable will close (return to its original position) if the confirmDismiss callback returns false.
    • dismissalDuration: The duration of the dismissal animation. Defaults to 300ms.
    • resizeDuration: The duration the widget spends contracting before onDismissed is called. If null, onDismissed is called immediately after dismissal.
    • motion: The widget used to animate the dismissal process (e.g., InversedDrawerMotion).
  10. Use the Slidable widget

    master

    The Slidable widget allows users to drag a widget to reveal contextual actions (panes). You can define a startActionPane and/or an endActionPane to show different actions depending on the drag direction.

    Key properties:

    • enabled: Set to false to disable interaction.
    • closeOnScroll: If true, the slidable closes when the nearest Scrollable moves.
    • direction: The axis of movement (Axis.horizontal or Axis.vertical).
    • groupTag: Use a shared object for multiple Slidable widgets to ensure only one in the group is open at a time.
    • useTextDirection: If true (and direction is horizontal), the panes are positioned based on the ambient TextDirection (LTR/RTL).
  11. Manage groups of Slidables with SlidableGroupBehavior

    master

    Use SlidableGroupBehavior<T> to create a scope for a group of Slidable widgets. This allows you to coordinate state or listen to notifications (like a specific slide action) across multiple Slidables within the same tree branch.

    • onNotification: A callback T? Function(T notification)? that can intercept and modify a notification before it is dispatched. If it returns null, the notification is suppressed.
    • child: The widget tree containing the Slidables that belong to this group.