Andy
Andy

Reputation: 45

How to achieve swipe right or left in flutter

I am making a todo list app. I want achieve swipe right to delete and swipe left to mark just like some email app.

I know Dismissible Widget can achieve swipe to delete and secondaryBackground can make other way to swipe.But I don't how to call other function when I swipe to other way.

return Dismissible(
          // Each Dismissible must contain a Key. Keys allow Flutter to
          // uniquely identify widgets.
          key: Key(item),
          // Provide a function that tells the app
          // what to do after an item has been swiped away.
          onDismissed: (direction) {
            // Remove the item from the data source.
            setState(() {
              items.removeAt(index);
            });
            // Then show a snackbar.
            Scaffold.of(context)
                .showSnackBar(SnackBar(content: Text("$item dismissed")));
          },
          // Show a red background as the item is swiped away.
          background: Container(color: Colors.red,child: Icon(Icons.cancel),),
          secondaryBackground: Container(color: Colors.green,child: Icon(Icons.check),),
          child: ListTile(title: Text('$item')),
        );

Upvotes: 1

Views: 5745

Answers (1)

shb
shb

Reputation: 6277

To determine which direction you swiped

onDismissed: (direction) {

   if(direction == DismissDirection.startToEnd) { // Right Swipe

        setState(() {
          items.removeAt(index);
        });

        Scaffold.of(context).showSnackBar(SnackBar(content: Text("$item dismissed")));

   } else if(direction == DismissDirection.endToStart) {//Left Swipe
        //add event to Calendar 
   }
 },

Upvotes: 5

Related Questions