简体   繁体   English

滑动到 go 返回手势 flutter

[英]Swipe to go back gesture flutter

How do i implement the swipe from the left to go back gesture in flutter?如何在 flutter 中实现从左滑动到 go 后退手势? Not sure if it was already implemented automatically for iOS, but I wanted it for Android as well (as things are becoming gesture based).不确定它是否已经为 iOS 自动实现,但我也希望它用于 Android(因为事情正在变得基于手势)。

Use CupertinoPageRoute to make it work on Android;使用CupertinoPageRoute使其在 Android 上运行;

import 'package:flutter/cupertino.dart';

(as answered on How to implement swipe to previous page in Flutter? ) (如如何在 Flutter 中实现滑动到上一页?

You could set your Theme.platform to TargetPlatform.ios.您可以将 Theme.platform 设置为 TargetPlatform.ios。 This will make use that the swipe back gesture is used on every device.这将利用在每个设备上使用向后滑动手势。

You can use CupertinoPageRoute() as Tom O'Sullivan said above .您可以使用CupertinoPageRoute()正如上面 Tom O'Sullivan 所说的那样。

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() .但是,如果您想使用PageRouteBuilder自定义它(例如使用自定义过渡持续时间)并获得相同的滑动以返回手势,那么您可以覆盖buildTransitions()

For iOS, the default PageTransitionBuilder is CupertinoPageTransitionsBuilder() .对于 iOS,默认的PageTransitionBuilderCupertinoPageTransitionsBuilder() So we can use that in buildTransitions() .所以我们可以在buildTransitions()中使用它。 This automatically give us the swipe right to go back gesture.这会自动让我们向右滑动以返回手势。

Here's some sample code for the CustomPageRouteBuilder :这是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: ...,
)

You can set the platform of your theme (and darkTheme ) to TargetPlatform.iOS , you can set the pageTransitionsTheme of your themes to,您可以将theme (和darkTheme )的platform设置为TargetPlatform.iOS ,您可以将主题的pageTransitionsTheme设置为,

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!并且您可以使用CupertinoPageRoute加载新页面......在您确保使用Navigator.push (而不是Navigator.pushReplacement )进入该新屏幕之前,这些都不会起作用! I hope this helps anyone out there who was working with existing transitions and didn't notice this crucial detail.我希望这可以帮助那些正在处理现有转换并且没有注意到这个关键细节的人。 :) :)

Use this plugin:使用这个插件:

https://pub.dev/packages/cupertino_back_gesture https://pub.dev/packages/cupertino_back_gesture

A Flutter package to set custom width of iOS back swipe gesture area.一个 Flutter 包,用于设置 iOS 后滑动手势区域的自定义宽度。 For basic use:基本用途:

import 'package:cupertino_back_gesture/cupertino_back_gesture.dart';

BackGestureWidthTheme(
 backGestureWidth: BackGestureWidth.fraction(1 / 2),
  child: MaterialApp(
    theme: ThemeData(
      pageTransitionsTheme: PageTransitionsTheme(
        builders: {
           //this is default transition
          //TargetPlatform.android: FadeUpwardsPageTransitionsBuilder(),
          //You can set iOS transition on Andoroid
          TargetPlatform.android: CupertinoPageTransitionsBuilderCustomBackGestureWidth(),
          TargetPlatform.iOS: CupertinoPageTransitionsBuilderCustomBackGestureWidth(),
        },
      ),
    ),
    home: MainPage(),
  ),
)

More details on plugin's page插件页面上的更多详细信息

in my case, the solution turned out to be very simple.就我而言,解决方案非常简单。 I just used context.push('screen') instead of context.go('/screen')我只是使用context.push('screen')而不是context.go('/screen')

This should not be implemented on Android since it makes interactions inconsistent across the OS.这不应该在 Android 上实现,因为它会使整个操作系统的交互不一致。

Swiping from the screens edge to go back is not something that Android wants you to implement, so you should better don't do it.从屏幕边缘滑动返回不是 Android 想要你实现的,所以你最好不要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM