Search code examples
flutterswipeprogram-entry-pointback

Prevent swipe to go back on first page in flutter


I have an issue that even when I'm on the main page, the user can still swipe back (from the edge) and hence end up on a black page, how do I prevent this?

Best regards


Solution

  • 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>,
            );
        }