Swipe to go back gesture flutter
Solution 1
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?)
Solution 2
You could set your Theme.platform to TargetPlatform.ios. This will make use that the swipe back gesture is used on every device.
Solution 3
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. :)
Solution 4
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: ...,
)
Related videos on Youtube
Tom O'Sullivan
Flutter Flutter Flutter. Dart. I am interested in learning Flutter (currently Beta), and will stick with it whiles it's still in production, and Dart 2.
Updated on September 14, 2022Comments
-
Tom O'Sullivan over 1 year
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).
-
najeira about 6 years
-