簡體   English   中英

Flutter:當我有一個 PageRouteBuilder 時,非命名路由到命名路由?

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

目前在我的應用程序中我不使用命名路由。 我像這樣構建了一個 function:

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

並像這樣使用它:

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

我想開始使用命名路線:

  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,
    );

使用 function 很重要,因為每個Navigator.push都在使用它,因此可以在每個屏幕上看到版本: Text("Version: 1.0.0"),

提前致謝。

如果您使用onGenerateRoute而不是路線,這將是可能的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM