如何删除默认的导航路线动画

时间:2019-09-03 13:46:22

标签: flutter flutter-animation flutter-navigation

我在flutter文档中给出的用于页面路由的代码下面

// Within the `FirstRoute` widget
onPressed: () {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SecondRoute()),
  );
}

但是它在推动和弹出路线时提供了一些动画。

  

对于Android,页面的入口过渡会滑动页面   向上并逐渐淡入。退出过渡相同,但在   反向。

     

该过渡适应于平台,在iOS上,页面会滑动   从右边进入,然后反向退出。页面也转移到   当另一页进入覆盖时,视差保留在视差中。 (这些   在具有从右到左读数的环境中,方向会发生翻转   方向。

有没有任何动画可以路由到下一页吗?

编辑: 请检查整个代码:

class MyApp extends StatelessWidget {
  final routes = <String, WidgetBuilder>{
    SecondRoute.tag: (context) => SecondRoute(),
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Routes",
      home: new FirstRoute(),
      routes: routes,
      onGenerateRoute: (routeSettings) {
        if (routeSettings.name == SecondRoute.tag)
          return PageRouteBuilder(pageBuilder: (_, a1, a2) => SecondRoute());

        return null;
      },
    );
  }
}

class FirstRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Open route'),
          onPressed: () {
            Navigator.of(context).pushNamed(SecondRoute.tag);

          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  static String tag = 'second-route';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}

4 个答案:

答案 0 :(得分:1)

动画由MaterialPageRoute执行。如果您不想要,只需使用其他方法即可:

Navigator.push(
  context,
  PageRouteBuilder(pageBuilder: (_, __, ___) => MyRoute()),
)

答案 1 :(得分:1)

使用Navigator.push(...)

Navigator.push(
  context,
  PageRouteBuilder(pageBuilder: (_, a1, a2) => SecondRoute()),
)

使用Navigator.pushNamed(...)

首先,将其添加到您的MaterialApp

MaterialApp(
  onGenerateRoute: (routeSettings) {
    if (routeSettings.name == "/abc") 
      return PageRouteBuilder(pageBuilder: (_, a1, a2) => SecondRoute());

    return null;
  },
  ...
)

并将其用作

Navigator.pushNamed(context, "/abc");

答案 2 :(得分:0)

以此替换您的MyApp

class MyApp extends StatelessWidget {
  final routes = <String, WidgetBuilder>{SecondRoute.tag: (context) => SecondRoute()};

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Routes",
      home: new FirstRoute(),
      onGenerateRoute: (routeSettings) {
        if (routeSettings.name == SecondRoute.tag)
          return PageRouteBuilder(
            pageBuilder: (_, a1, a2) => FadeTransition(opacity: a1 ,child: SecondRoute()),
            transitionDuration: Duration(seconds: 5),
          );

        return null;
      },
    );
  }
}

答案 3 :(得分:0)

由于 Flutter 现在迁移到 Navigator 2.0 以获得更多支持,我建议查看他们的 migration guide 以将 TransitionDelegate 添加到 Navigator。将此类的一个实例添加到您的导航器以实现预期结果:

import 'package:flutter/widgets.dart';

class NoAnimationTransitionDelegate extends TransitionDelegate<void> {
  @override
  Iterable<RouteTransitionRecord> resolve({
    List<RouteTransitionRecord> newPageRouteHistory,
    Map<RouteTransitionRecord, RouteTransitionRecord> locationToExitingPageRoute,
    Map<RouteTransitionRecord, List<RouteTransitionRecord>> pageRouteToPagelessRoutes,
  }) {
    final List<RouteTransitionRecord> results = <RouteTransitionRecord>[];

    for (final RouteTransitionRecord pageRoute in newPageRouteHistory) {
      // Renames isEntering to isWaitingForEnteringDecision.
      if (pageRoute.isWaitingForEnteringDecision) {
        pageRoute.markForAdd();
      }
      results.add(pageRoute);

    }
    for (final RouteTransitionRecord exitingPageRoute in locationToExitingPageRoute.values) {
      // Checks the isWaitingForExitingDecision before calling the markFor methods.
      if (exitingPageRoute.isWaitingForExitingDecision) {
        exitingPageRoute.markForRemove();
        final List<RouteTransitionRecord> pagelessRoutes = pageRouteToPagelessRoutes[exitingPageRoute];
        if (pagelessRoutes != null) {
          for (final RouteTransitionRecord pagelessRoute in pagelessRoutes) {
            pagelessRoute.markForRemove();
          }
        }
      }
      results.add(exitingPageRoute);

    }
    return results;
   }
 }