针对自定义路线的Flutter设置过渡

时间:2020-03-31 04:38:12

标签: flutter flutter-navigation

在我们的应用程序中,我们实现了自定义路线导航,我想为此设置过渡,例如,这是我们的自定义路线,可以正常工作而没有任何问题

Navigator(
  key: navigatorKey,
  initialRoute: Page.screenDashboard.route,
  onGenerateRoute: (settings) {
    final pageName = settings.name;

    final page = _fragments.keys.firstWhere(
            (element) => describeEnum(element) == pageName);

    return MaterialPageRoute(
        builder: (context) => FadeAndSlideRightTransition(page: _fragments[page]));
  },
),

在那我该如何为MaterialPageRoute设置过渡?

例如:

class SlideRightRoute extends PageRouteBuilder {
  final Widget page;
  SlideRightRoute({this.page})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) =>
              page,
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) =>
              SlideTransition(
                position: Tween<Offset>(
                  begin: const Offset(-1, 0),
                  end: Offset.zero,
                ).animate(animation),
                child: child,
              ),
        );
}    

此测试无法正常工作,并且出现错误:

return MaterialPageRoute(builder: (context) => SlideRightRoute (page: _fragments[page]));    

错误

error: The return type 'SlideRightRoute ' isn't a 'Widget', as required by the closure's context. (return_of_invalid_type_from_closure at [fluttersamples] lib\screens.dart:62)

1 个答案:

答案 0 :(得分:0)

使用

return SlideRightRoute(page: _fragments[page]);

代替

return MaterialPageRoute(builder: (context) => _fragments[page]);

解决了我的问题