flutter_slidable icon indicating copy to clipboard operation
flutter_slidable copied to clipboard

How to make the delete effect smooth when you click on the delete button.

Open Artem738 opened this issue 3 years ago • 5 comments

When a block is removed with a swipe, it is removed smoothly. But if you remove it with a simple click, then the removal effect is instant. What do I need to do with this sample code to use the smooth effect of this package when I delete an element with a button? Thanks for the great package. Unfortunately, I couldn't figure it out myself.

import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({
    Key? key,
  }) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List dataMap = [
    {"id": 0, "title": "Zero"},
    {"id": 1, "title": "First"},
    {"id": 2, "title": "Second"},
    {"id": 3, "title": "Third"},
    {"id": 4, "title": "Fourth"},
    {"id": 5, "title": "Fifth"}
  ];

  void removeFromMap(id) {
    setState(() {
      dataMap.removeAt(id);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Slidable Example',
      home: Scaffold(
        body: ListView(
          children: [
            for (var id = 0; id < dataMap.length; id++) ...[
              Slidable(
                key: UniqueKey(),
                endActionPane: ActionPane(
                  motion: const ScrollMotion(),
                  dismissible: DismissiblePane(onDismissed: () {
                    removeFromMap(id);
                  }),
                  children: [
                    SlidableAction(
                      onPressed: (context) => removeFromMap(id),
                      backgroundColor: const Color(0xFFFE4A49),
                      foregroundColor: Colors.white,
                      icon: Icons.delete,
                      label: 'Delete',
                    ),
                  ],
                ),
                child: ListTile(
                  title: Text(
                    '${dataMap[id]['title']} | realId=${dataMap[id]['id']} | listId=${id.toString()}',
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }
}

Artem738 avatar Jun 30 '22 17:06 Artem738

onPressed: (context) {
  final controller = Slidable.of(context);
  controller?.dismiss(
    ResizeRequest(const Duration(milliseconds: 300),
    () => _deleteComment(_comments![index])),
    duration: const Duration(milliseconds: 300),
  );
}

Try this.

cliftonscott avatar Jul 06 '22 00:07 cliftonscott

What do you mean by smooth? The default code should Resize the item on removal, so I don't understand what is not smooth.

letsar avatar Jul 10 '22 15:07 letsar

onPressed: (context) {
  final controller = Slidable.of(context);
  controller?.dismiss(
    ResizeRequest(const Duration(milliseconds: 300),
    () => _deleteComment(_comments![index])),
    duration: const Duration(milliseconds: 300),
  );
}

Try this.

unfortunately it didn't work at all. It doesn't show any errors, just nothing happens.

                      onPressed: (context) {
                        final controller = Slidable.of(context);
                        controller?.dismiss(
                          ResizeRequest(
                            const Duration(milliseconds: 300),
                            () => removeFromMap(id),
                          ),
                          duration: const Duration(milliseconds: 300),
                        );
                      },

What do you mean by smooth? The default code should Resize the item on removal, so I don't understand what is not smooth.

DismissiblePane removes the block nicely. But I can't figure out how to use it when I remove it in a way other than shift to the side. In the code above, the removeFromMap method is run in two places. And because of this, the removal effect works differently.

Artem738 avatar Jul 17 '22 12:07 Artem738

I am trying to achieve click to delete instead of sliding too, but no luck yet whatsoever

Zechst avatar Jul 28 '22 15:07 Zechst

@Artem738 I think https://github.com/letsar/flutter_slidable/issues/245#issuecomment-910219066 would help you.

kyle-seongwoo-jun avatar Aug 22 '23 09:08 kyle-seongwoo-jun