Swipe List Item for more options (Flutter)
Solution 1
I created a package for doing this kind of layout: flutter_slidable (Thanks Rémi Rousselet for the based idea)
With this package it's easier to create contextual actions for a list item. For example if you want to create the kind of animation you described:
You will use this code:
new Slidable(
delegate: new SlidableDrawerDelegate(),
actionExtentRatio: 0.25,
child: new Container(
color: Colors.white,
child: new ListTile(
leading: new CircleAvatar(
backgroundColor: Colors.indigoAccent,
child: new Text('$3'),
foregroundColor: Colors.white,
),
title: new Text('Tile n°$3'),
subtitle: new Text('SlidableDrawerDelegate'),
),
),
actions: <Widget>[
new IconSlideAction(
caption: 'Archive',
color: Colors.blue,
icon: Icons.archive,
onTap: () => _showSnackBar('Archive'),
),
new IconSlideAction(
caption: 'Share',
color: Colors.indigo,
icon: Icons.share,
onTap: () => _showSnackBar('Share'),
),
],
secondaryActions: <Widget>[
new IconSlideAction(
caption: 'More',
color: Colors.black45,
icon: Icons.more_horiz,
onTap: () => _showSnackBar('More'),
),
new IconSlideAction(
caption: 'Delete',
color: Colors.red,
icon: Icons.delete,
onTap: () => _showSnackBar('Delete'),
),
],
);
Solution 2
There's already a widget for this kind of gesture. It's called Dismissible
.
You can find it here. https://docs.flutter.io/flutter/widgets/Dismissible-class.html
EDIT
If you need the exact same transtion, you'd probably have to implement if yourself. I made a basic example. You'd probably want to tweak the animation a bit, but it's working at least.
class Test extends StatefulWidget {
@override
_TestState createState() => new _TestState();
}
class _TestState extends State<Test> {
double rating = 3.5;
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new ListView(
children: ListTile
.divideTiles(
context: context,
tiles: new List.generate(42, (index) {
return new SlideMenu(
child: new ListTile(
title: new Container(child: new Text("Drag me")),
),
menuItems: <Widget>[
new Container(
child: new IconButton(
icon: new Icon(Icons.delete),
),
),
new Container(
child: new IconButton(
icon: new Icon(Icons.info),
),
),
],
);
}),
)
.toList(),
),
);
}
}
class SlideMenu extends StatefulWidget {
final Widget child;
final List<Widget> menuItems;
SlideMenu({this.child, this.menuItems});
@override
_SlideMenuState createState() => new _SlideMenuState();
}
class _SlideMenuState extends State<SlideMenu> with SingleTickerProviderStateMixin {
AnimationController _controller;
@override
initState() {
super.initState();
_controller = new AnimationController(vsync: this, duration: const Duration(milliseconds: 200));
}
@override
dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final animation = new Tween(
begin: const Offset(0.0, 0.0),
end: const Offset(-0.2, 0.0)
).animate(new CurveTween(curve: Curves.decelerate).animate(_controller));
return new GestureDetector(
onHorizontalDragUpdate: (data) {
// we can access context.size here
setState(() {
_controller.value -= data.primaryDelta / context.size.width;
});
},
onHorizontalDragEnd: (data) {
if (data.primaryVelocity > 2500)
_controller.animateTo(.0); //close menu on fast swipe in the right direction
else if (_controller.value >= .5 || data.primaryVelocity < -2500) // fully open if dragged a lot to left or on fast swipe to left
_controller.animateTo(1.0);
else // close if none of above
_controller.animateTo(.0);
},
child: new Stack(
children: <Widget>[
new SlideTransition(position: animation, child: widget.child),
new Positioned.fill(
child: new LayoutBuilder(
builder: (context, constraint) {
return new AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return new Stack(
children: <Widget>[
new Positioned(
right: .0,
top: .0,
bottom: .0,
width: constraint.maxWidth * animation.value.dx * -1,
child: new Container(
color: Colors.black26,
child: new Row(
children: widget.menuItems.map((child) {
return new Expanded(
child: child,
);
}).toList(),
),
),
),
],
);
},
);
},
),
)
],
),
);
}
}
EDIT
Flutter no longer allows type Animation<FractionalOffset>
in SlideTransition
animation
property. According to this post https://groups.google.com/forum/#!topic/flutter-dev/fmr-C9xK5t4 it should be replaced with AlignmentTween
but this also doesn't work. Instead, according to this issue: https://github.com/flutter/flutter/issues/13812 replacing it instead with a raw Tween
and directly creating Offset
object works instead. Unfortunately, the code is much less clear.
Solution 3
Updated Code with Null Safety: Flutter: 2.x Firstly you need to add the flutter_slidable package in your project and add below code then Let's enjoy...
Slidable(
actionPane: SlidableDrawerActionPane(),
actionExtentRatio: 0.25,
child: Container(
color: Colors.white,
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.indigoAccent,
child: Text('$3'),
foregroundColor: Colors.white,
),
title: Text('Tile n°$3'),
subtitle: Text('SlidableDrawerDelegate'),
),
),
actions: <Widget>[
IconSlideAction(
caption: 'Archive',
color: Colors.blue,
icon: Icons.archive,
onTap: () => _showSnackBar('Archive'),
),
IconSlideAction(
caption: 'Share',
color: Colors.indigo,
icon: Icons.share,
onTap: () => _showSnackBar('Share'),
),
],
secondaryActions: <Widget>[
IconSlideAction(
caption: 'More',
color: Colors.black45,
icon: Icons.more_horiz,
onTap: () => _showSnackBar('More'),
),
IconSlideAction(
caption: 'Delete',
color: Colors.red,
icon: Icons.delete,
onTap: () => _showSnackBar('Delete'),
),
],
);
Solution 4
I have a task that needs the same swipeable menu actions I tried answeres of Romain Rastel and Rémi Rousselet. but I have complex widget tree. the issue with that slideable solutions is they go on other widgets(to left widgets of listview). I found a batter solution here someone wrote a nice article medium and GitHub sample is here.
Lukas Kirner
BY DAY: Student at University of Applied Sciences Munich. BY NIGHT: Fan of the Flutter framework since 1,5 years and always interested in new frameworks and tools.
Updated on July 08, 2022Comments
-
Lukas Kirner almost 2 years
Somedays ago I decided to choose an Ui for an app from Pinterest to practice building apps with Flutter but I'm stuck with the Slider which shows an "more" and "delete" button on horizontal drag. Picture on the right.
I don't have enough knowledge to use Gestures combined with Animations to create something like this in flutter. Thats why I hope that someone of you can make an example for everyone like me that we can understand how to implement something like this in a ListView.builder.
An gif example from the macOS mail App:
-
Shady Aziza over 6 yearsThe original post is asking about how to place a widget to appear at the right corner when you swipe, in the gif provided, that would be the red rectangle with the trash icon in it, the question is not about the swipe effect itself rather making the red rectangle appear when you swipe.
-
Rémi Rousselet over 6 yearsDismissible contains a
background
parameter which is made for that purpose. -
Shady Aziza over 6 yearsBut you need to stop the swipe effect at a certain position in order for the user to be able to interact with the items in the background, otherwise the widget is going to resize to full size or zero before you will be able to interact with the background, is there a way to freeze the swipe at a certain position so it shows enough of the background and be able to interact with it ?
-
Rémi Rousselet over 6 yearsBut that's kinda against Material rules. Which flutter will somehow prevents you from doing. This is clearly a "Dismiss swipe", which is used to send the item off screen. Something like a "More info" should be done using an "Edge swipe" (like android notifications bar) or an "Expand" gesture (android notification card here)
-
Lukas Kirner over 6 yearsSo and how can I implement what I want even if its against the Material Rules?
-
Shady Aziza over 6 years@Darky this is already fine in iOS design language, why would flutter prevent me from doing this just because it does not follow material design, maybe I am making two views, one for android and one for iOS, check this: i.imgur.com/jvsfElV.gif?1
-
Lukas Kirner over 6 years@Darky then how can i move an widget off screen by an swipe without the widget Dismissible?
-
Rémi Rousselet over 6 years@azizia It's not that flutter prevents you from doing anything else but material. Just that Cupertino theme is not fully finished yet I guess ? Anyway if you want to have this exact transition, you'd have to implement it yourself. I edited my post with an example.
-
Bilal Şimşek over 5 yearslooks great. I implement to my project. I wonder if its possible to implement an action that runs a function when slide full screen direction
-
Dani over 4 yearsawesome widget. I don't know why are not these great widgets already included in Flutter. One question, is possible to close the option when we click again on the element? (to avoid another swipe to close them again)
-
Pavan over 4 yearsThis is just the flutter example code for Dismissible. no delete button unlike in the answers provided above.
-
Panduranga Rao Sadhu about 4 yearscool widget to have. One question I have is how do I tell the user that the row is slidable?
-
Ali Rn over 3 years@PandurangaRaoSadhu meybe you can use tooltip or something like that
-
Andy Torres over 3 yearsThank! this answer works! i this case how could i slide only one item at time @RémiRousselet
-
Andy Torres over 3 yearsi wish prevent this: please check img ibb.co/MRGRBf2
-
Snivio over 2 yearsHi Roman the flutter_slidable package is pretty good i wanted to know how to show a demo swipe animation only to the first item when the customer first time opens the page
-
Smith July about 2 yearsas of today in 2022, if you use the latest flutter slideable package, it completely changed, so better if you put flutter_slidable: ^0.5.7 in the pubspec than the latest
-
Ali ijaz about 2 yearsGreat work, Awesome widget. I don't know why are not these great widgets already included in Flutter. But your work is appreciated!