Home > Net >  Is it necessary to add pages to routes in MaterialApp
Is it necessary to add pages to routes in MaterialApp

Time:10-25

Is it necessary to have multiple pages in flutter app without adding the pages to routes parameter in MaterialApp()

example

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

CodePudding user response:

Your question is a bit unclear if your want to ask that it is necessary to add the routes dynamically or defined, you can have a single page as Material App have a property called as home, which is the default route of the app, and can be an only route unless you define multiple routes.

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

For further detail you can check the Material Class here, https://api.flutter.dev/flutter/material/Material-class.html

CodePudding user response:

if you are asking..is there a alternate way to navigate to multiple pages without using routes in your flutter app you can use 'MaterialPageRoute' whenever you need to navigate (MaterialPageRoute can be used without adding page in your routes)

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

Also you can always use routes in your flutter app and navigate using 'pushNamed'

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

now use pushNamed:

Navigator.pushNamed(context, LobbyScreen.route);

define route in every page using:

static const route = '/lobby';
  • Related