有必要在MaterialApp中添加页面到路由吗?



有必要在flutter应用程序中有多个页面而不将页面添加到MaterialApp()中的routes参数

的例子:

void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
))}

如果你问…是否有一种替代方法来导航到多个页面而不使用路由在你的flutter应用程序中,你可以使用'MaterialPageRoute'每当你需要导航(MaterialPageRoute可以使用而不添加页面在你的路由)

Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => LobbyScreen(),
),
);

你也可以在你的flutter应用中使用路由并使用'pushNamed'进行导航

MaterialApp(
title: 'Flutter Demo',
theme: ThemeData.light(),
initialRoute: LoginScreen.route,
routes: {
LobbyScreen.route: (context) => LobbyScreen(),
LoginScreen.route: (context) => LoginScreen(),
GameScreen.route: (context) => GameScreen(),
},
);

现在使用pushNamed:

Navigator.pushNamed(context, LobbyScreen.route);

定义每个页面的路由:

static const route = '/lobby';

你的问题有点不清楚,如果你想问是否有必要动态添加或定义路由,你可以有一个单独的页面作为Material App,有一个名为home的属性,这是应用的默认路由,可以是唯一的路由,除非你定义多个路由。

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Your App',
theme: ThemeData(
primarySwatch: Colors.lue,
),
home: YourSinglePage(),
debugShowCheckedModeBanner: false,
);
}
}

要了解更多细节,您可以查看这里的材质类,https://api.flutter.dev/flutter/material/Material-class.html