Flutter: Disable Swipe to Navigate Back in iOS and Android
I have one additional point here. I was just solving this problem, but I also needed my user to be able to go back by pressing the "native" back button on the AppBar (did not want to reimplement AppBar just because of this), and I found this niche little flag: userGestureInProgress
on the Navigator object, so what I use (and presume is the preferred way) is:
onWillPop: () async {
if (Navigator.of(context).userGestureInProgress)
return false;
else
return true;
},
Prevent swipe to go back on first page in flutter
There's some more info here:
https://api.flutter.dev/flutter/widgets/ModalRoute/addScopedWillPopCallback.html
@override
Widget build(BuildContext context) {
return WillPopScope(//forbidden swipe in iOS(my ThemeData(platform: TargetPlatform.iOS,)
onWillPop: ()async {
if (Navigator.of(context).userGestureInProgress)
return false;
else
return true;
},
child: <your child>,
);
}
Why does WillPopScope always disable swipe backwards?
Please refer to the below code
Without wrapping the widget with WillPopScope usually its performs Navigator.pop(context);
// disables swiping back
WillPopScope(
// disables swiping back or navigating back
onWillPop: () {},
child: Scaffold(
body: Container(),
),
);
WillPopScope(
onWillPop: () {
// whenever you want to navigate back to specific route
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
child: Scaffold(
body: Container(),
),
);
WillPopScope(
onWillPop: () {
// pop back
Navigator.pop(context);
},
child: Scaffold(
body: Container(),
),
);
Swipe to go back gesture flutter
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?)
Increasing swipe back threshold in flutter
There is a package called swipeable_page_route.
Navigator.of(context).push(
SwipeablePageRoute(
builder: (context) => const FollowInfoScreen(),
),
);
Related Topics
Restore Button for iOS Non-Consumable In-App Purchase
Does iPhone Support Hardware-Accelerated Aes Encryption
Finding Distance Between Cllocationcoordinate2D Points
How to Open a Link to a PDF with Wkwebview
Filter Array by First Letter of String Property
Avaudiosession .Defaulttospeaker Changes Mic Input
Xcode 6.3: Could Not Load Nib in Bundle
Swrevealviewcontroller - Rightviewcontroller
How to Expose Existing Property on Obj-C Class Using an Extension Protocol in Swift
Instagram API Prevents Redirection to iOS App Upon Successful Login with 400 Bad Request Error
Core Data Predicate:Unimplemented SQL Generation for Predicate
iOS Simulator Is Not Launching
Uipinchgesturerecognizer. Make Zoom in Location of Fingers, Not Only Center
How to Show "Would Like to Send You Push Notifications" Alert View Again
Uiscrollview with Dynamically Sized Content
Remove Next/Previous Buttons (Inputaccessoryview) for Custom Keyboard in iOS8 Webview