How to create full screen dialog in flutter when I am using root names to Navigate?

天大地大妈咪最大 提交于 2021-01-05 04:41:08

问题


This is the sample code using rootName but here I am not able to use MaterialPageRoute to get the fullScreenDialog property.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (context) => MyHomePage(),
        '/under-development': (context) => UnderDevelopment(),
        '/profile1': (context) => Profile1()
      },
      title: appName,
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
          primaryColor: primaryColor,
          accentColor: secondaryColor,
          fontFamily: 'Poppins'),
    );
  }
}

Navigator

onTap: () {
    Navigator.pushNamed(context, '/profile1');
},

回答1:


You can use something like this inside Material App. I hope it helps.

onGenerateRoute: (RouteSettings settings) {
        List<String> pathElements = settings.name.split("/");
        if (pathElements[0] != "") return null;
        switch (pathElements[1]) {
          case "home":
          String userID = pathElements[2];  
            return MaterialPageRoute(
        builder: (context) => ReportsPage(
              userID: userID,
            ),fullscreenDialog: true);

        }
      },



回答2:


new MaterialApp(
    title: 'Named Routes Demo', 
    theme: ThemeData(
    primarySwatch: Colors.green), 
    initialRoute: '/', 
    onGenerateRoute: (RouteSettings settings) {
      List<String> pathElements = settings.name.split("/");
      if (pathElements[0] != "") return null;
      switch (pathElements[1]) {
        case "":
          return MaterialPageRoute(
              builder: (context) => FirstScreen());
        case "second":
          return MaterialPageRoute(
              builder: (context) => SecondScreen(), fullscreenDialog: true);
        case "third":
          return MaterialPageRoute(
              builder: (context) => ThirdScreen(), fullscreenDialog: true);
    }
  },
)

Navigator Button

onTap: () {
    Navigator.pushNamed(context, '/second');
},


来源:https://stackoverflow.com/questions/52922743/how-to-create-full-screen-dialog-in-flutter-when-i-am-using-root-names-to-naviga

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!