简体   繁体   中英

Flutter: non-named routing to named route while I have a PageRouteBuilder?

At the moment in my application I don't use named routes. I built a function like this:

class CustomPageRouteBuilder {
  static PageRouteBuilder getPageRouteBuilder(Widget widget) {
    return PageRouteBuilder(
        transitionDuration: Duration(
          milliseconds: 100,
        ),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secAnimation, Widget child) {
          animation = CurvedAnimation(
            parent: animation,
            curve: Curves.elasticIn,
          );
          return FadeTransition(
            opacity: animation,
            child: child,
          );
        },
        pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secAnimation) {
          return Scaffold(
            body: Stack(children: [
              BiometricAuthentication(widget),
              Align(
                alignment: Alignment.bottomLeft,
                child: Opacity(
                  opacity: 0.5,
                  child: Text("Version: 1.0.0",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 12,
                      )),
                ),
              )
            ]),
          );
        });
  }
}

And use it like this:

Navigator.push(
        context,
        CustomPageRouteBuilder.getPageRouteBuilder(SecondPage()),
      );

I would like to start using the named routes:

  Map<String, Widget Function(BuildContext)> map = {
    '/login': (context) => LoginPage(),
    '/second': (context) => ??? , // Here should be the mentioned function used somehow
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'My app',
        theme: ThemeData(
          primaryColor: Colors.yellow,
          scaffoldBackgroundColor: Colors.blue,
          cardColor: Colors.yellow,
        ),
        initialRoute: '/login',
        routes: map,
    );

It would be important to use that function because every Navigator.push is using that so the version is can be seen on every screen: Text("Version: 1.0.0"),

Thanks in advance.

If you use the onGenerateRoute instead of routes this will be possible:

MaterialApp(
  onGenerateRoute: (settings) {
    switch (settings.name) {
      case '/login':
        return MaterialPageRoute(
          builder: (context) => LoginPage(),
        );
      case '/second':
        return CustomPageRouteBuilder.getPageRouteBuilder(SecondPage());
    }
    return null;
  },
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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