How do i implement the swipe from the left to go back gesture in flutter? Not sure if it was already implemented automatically for iOS, but I wanted it for Android as well (as things are becoming gesture based).
-
2same as https://stackoverflow.com/questions/49894406/how-to-implement-swipe-to-previous-page-in-flutter – najeira Apr 20 '18 at 08:35
-
@najeira thanks, will close this! – Tom O'Sullivan Apr 20 '18 at 09:27
7 Answers
Use CupertinoPageRoute
to make it work on Android;
import 'package:flutter/cupertino.dart';
(as answered on How to implement swipe to previous page in Flutter?)

- 3,416
- 5
- 15
- 27
You could set your Theme.platform to TargetPlatform.ios. This will make use that the swipe back gesture is used on every device.

- 2,121
- 26
- 50
You can use CupertinoPageRoute()
as Tom O'Sullivan said above.
However, if you want to customize it (eg. using custom transition duration) using PageRouteBuilder
s and get the same swipe to go back gesture, then you can override buildTransitions()
.
For iOS, the default PageTransitionBuilder
is CupertinoPageTransitionsBuilder()
. So we can use that in buildTransitions()
. This automatically give us the swipe right to go back gesture.
Here's some sample code for the CustomPageRouteBuilder
:
class CustomPageRouteBuilder<T> extends PageRoute<T> {
final RoutePageBuilder pageBuilder;
final PageTransitionsBuilder matchingBuilder = const CupertinoPageTransitionsBuilder(); // Default iOS/macOS (to get the swipe right to go back gesture)
// final PageTransitionsBuilder matchingBuilder = const FadeUpwardsPageTransitionsBuilder(); // Default Android/Linux/Windows
CustomPageRouteBuilder({this.pageBuilder});
@override
Color get barrierColor => null;
@override
String get barrierLabel => null;
@override
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
return pageBuilder(context, animation, secondaryAnimation);
}
@override
bool get maintainState => true;
@override
Duration get transitionDuration => Duration(milliseconds: 900); // Can give custom Duration, unlike in MaterialPageRoute
@override
Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
return matchingBuilder.buildTransitions<T>(this, context, animation, secondaryAnimation, child);
}
}
Then to go to a new page:
GestureDetector(
onTap: () => Navigator.push(
context,
CustomPageRouteBuilder(pageBuilder: (context, animation, secondaryAnimation) => NewScreen()),
),
child: ...,
)

- 501
- 6
- 9
-
how about transitionDuration: transitionDuration and reverseTransitionDuration: transitionDuration, parameter ? can u edit your answer for these parameter ? – Yogi Arif Widodo Mar 12 '22 at 02:12
-
@override // TODO: implement reverseTransitionDuration Duration get reverseTransitionDuration => const Duration(milliseconds: 900); // – Yogi Arif Widodo Mar 12 '22 at 02:14
-
this worked for me like a charm, should be the correct answer! It helps when you are creating a custom pageroute – scugn1zz0 Jun 12 '22 at 12:02
You can set the platform
of your theme
(and darkTheme
) to TargetPlatform.iOS
, you can set the pageTransitionsTheme
of your themes to,
pageTransitionsTheme: PageTransitionsTheme(
builders: {
TargetPlatform.android: CupertinoPageTransitionsBuilder(),
TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
},
),
and you can load the new page using CupertinoPageRoute
... and none of that will work until you make sure to use Navigator.push
(instead of Navigator.pushReplacement
) to get to that new screen! I hope this helps anyone out there who was working with existing transitions and didn't notice this crucial detail. :)

- 882
- 1
- 12
- 17

- 355
- 4
- 9
Use this plugin:
https://pub.dev/packages/cupertino_back_gesture
A Flutter package to set custom width of iOS back swipe gesture area. For basic use:
import 'package:cupertino_back_gesture/cupertino_back_gesture.dart';
BackGestureWidthTheme(
backGestureWidth: BackGestureWidth.fraction(1 / 2),
child: MaterialApp(
theme: ThemeData(
pageTransitionsTheme: PageTransitionsTheme(
builders: {
//this is default transition
//TargetPlatform.android: FadeUpwardsPageTransitionsBuilder(),
//You can set iOS transition on Andoroid
TargetPlatform.android: CupertinoPageTransitionsBuilderCustomBackGestureWidth(),
TargetPlatform.iOS: CupertinoPageTransitionsBuilderCustomBackGestureWidth(),
},
),
),
home: MainPage(),
),
)
More details on plugin's page

- 1
- 1
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 01 '22 at 07:33
in my case, the solution turned out to be very simple. I just used context.push('screen')
instead of context.go('/screen')

- 51
- 5
This should not be implemented on Android since it makes interactions inconsistent across the OS.
Swiping from the screens edge to go back is not something that Android wants you to implement, so you should better don't do it.

- 5,308
- 5
- 30
- 52
-
2Although I agree with you from UX perspective, that's not really an answer to the actual question, which is a legit one. – Capricorn Jul 20 '18 at 12:28
-
@Capricorn yep, just wanted to see what it was like for implementation and for feel, but understand that it goes against the MD rules. – Tom O'Sullivan Jul 21 '18 at 12:40
-
Android Q the recommended way is a back gesture https://android-developers.googleblog.com/2019/08/gesture-navigation-backstory.html – Denis Murphy Oct 18 '19 at 10:05