首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在颤振中设置go_router的默认转换

在颤振中设置go_router的默认转换
EN

Stack Overflow用户
提问于 2022-03-27 12:40:07
回答 2查看 3.5K关注 0票数 3

正如路由器描述的那样,很容易为单个页面设置pageBuilder-Transitions。但是,我希望设置all pages的默认.

如何在颤振中设置使用/用于go_router的默认页面转换?

单页:

代码语言:javascript
运行
复制
  // this is the proposed method to do it for single pages
  // how can i apply it to all pages without writing the same code?
  GoRoute(
      path: '/login',
      builder: (context, state) => const LoginScreen(),
      pageBuilder: (context, state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const LoginScreen(),
        transitionsBuilder: (context, animation, secondaryAnimation, child) =>
            FadeTransition(opacity: animation, child: child),
      ),
    ),

诚挚的问候

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-27 21:16:43

go_router包目前不支持这一点,但是为了减少代码重复,您可以创建一个帮助函数,用于为您的路由应用自定义转换,如下所示:

代码语言:javascript
运行
复制
CustomTransitionPage buildPageWithDefaultTransition<T>({
  required BuildContext context, 
  required GoRouterState state, 
  required Widget child,
}) {
  return CustomTransitionPage<T>(
    key: state.pageKey,
    child: child,
    transitionsBuilder: (context, animation, secondaryAnimation, child) => 
      FadeTransition(opacity: animation, child: child),
  );
}

<...>

GoRoute(
  path: '/login',
  builder: (context, state) => const LoginScreen(),
  pageBuilder: (context, state) => buildPageWithDefaultTransition<void>(
    context: context, 
    state: state, 
    child: LoginScreen(),
  ),
),
票数 10
EN

Stack Overflow用户

发布于 2022-05-12 16:16:03

更正确的方法是从CustomTransitionPage继承。

代码语言:javascript
运行
复制
class WebPage extends CustomTransitionPage {
  WebPage({
    LocalKey key,
    ... // other properties taken from `MaterialPage`
    required Widget child
  }) : super(
         key: key,
         transitionBuilder: (...) {
           return FadeTransition(...);
         }
         child: child, // Here you may also wrap this child with some common designed widget
       );
}

然后

代码语言:javascript
运行
复制
GoRoute(
  path: '/login',
  builder: (context, state) => const LoginScreen(),
  pageBuilder: (context, state) => WebPage(
    key: state.pageKey,
    child: const LoginScreen(),
  ),
),
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71636397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档