Flutter: Disable Swipe to Navigate Back in iOS

后端 未结 7 1074
一向
一向 2020-12-16 15:25

I\'m new to flutter development, and find it a bit frustrating in iOS when you have a navigation drawer and when you swipe to open it, it\'ll perform a Navigation.of(c

相关标签:
7条回答
  • 2020-12-16 15:44

    I had a similar problem where I wanted to block the swipe to return back in navigation (default pop function). This code helped fix the problem.

    @override
    Widget build(BuildContext context) {
        return WillPopScope(
          onWillPop: () async => false,
          child: Scaffold(
            body: Center(
              child: Text('Example'),
            ),
          ),
        );
      }
    

    This code now blocks the back navigation for iOS & Android (default back button) and that's exactly what I was looking for. Hope this helps.

    0 讨论(0)
  • 2020-12-16 15:50

    Alright, so as @Darky said WillPopScope is a perfectly acceptable answer, however, if you want to disable it across the board you can actually do the following.

    Open your project in xcode, find AppDelegate.swift and add the following:

    let controller: FlutterViewController
        = window?.rootViewController as! FlutterViewController;
    controller.navigationController?
        .interactivePopGestureRecognizer?.isEnabled = false;
    
    0 讨论(0)
  • 2020-12-16 15:57

    You can try this in your Widget build:

    @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>,
        );
      }
    
    0 讨论(0)
  • 2020-12-16 15:59

    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;
      },
    
    0 讨论(0)
  • 2020-12-16 16:04

    WillPopScope is the correct way to do this.

    (it seems too complicated... it should be easier... like a setting on the root app).

    It is not complicated. It's a one liner :

    WillPopScope(
      onWillPop: () async => false,
      child: <children here>
    )
    

    A configuration file would make things more complicated as it's harder to read and maintain.

    And remember that in flutter everything is a widget not just half of them. Authentification, configurations, everything.

    0 讨论(0)
  • 2020-12-16 16:04

    MaterialPageRoute has a parameter called fullscreenDialog which is set to false by default. When true your page animates a bit differently and swipe to go back on iOS will be disabled.

    Example usage:

     Navigator.of(context).push(
            MaterialPageRoute(builder: (_) => HomePage(), fullscreenDialog: true));
    

    See some discussion here: https://github.com/flutter/flutter/issues/14203

    0 讨论(0)
提交回复
热议问题