gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:01:30 26 4
gpt4 key购买 nike

我下面的代码是 flutter 文档中给出的页面路由

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

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

For Android, the entrance transition for the page slides the page upwards and fades it in. The exit transition is the same, but in reverse.

The transition is adaptive to the platform and on iOS, the page slides in from the right and exits in reverse. The page also shifts to the left in parallax when another page enters to cover it. (These directions are flipped in environments with a right-to-left reading direction.)

有没有办法在没有任何动画的情况下路由到下一页?

编辑:请检查整个代码:

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!'),
),
),
);
}
}

最佳答案

  • 对于Navigator.push(...)

    Navigator.push(
    context,
    PageRouteBuilder(pageBuilder: (_, __, ___) => SecondRoute()),
    )
  • 对于Navigator.pushNamed(...)

    首先,将其添加到您的MaterialApp

    MaterialApp(
    onGenerateRoute: (settings) {
    if (settings.name == '/second')
    return PageRouteBuilder(pageBuilder: (_, __, ___) => SecondRoute());

    return null;
    },
    )

    现在,您可以使用:

    Navigator.pushNamed(context, '/second');

关于flutter - 如何删除默认导航路线动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57773077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com