Flutter: Disable Swipe to Navigate Back in iOS and Android

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



Leave a reply



Submit