如何传递参数runApp(MyApp());

迈克赞

我有下面的代码,这没有问题。

void main() async {
  Widget _defaultHome = new LoginPage();

  runApp(new MaterialApp(
    title: 'App',
    debugShowCheckedModeBanner: false,
    home: _defaultHome,
    routes: <String, WidgetBuilder>{
      // Set routes for using the Navigator.
      '/tabs': (BuildContext context) => new TabsPage(),
      '/login': (BuildContext context) => new LoginPage()
    },
  ));
}

我想将其更改为以下代码,如何将_defaultHome传递给MyApp类?

void main() async {
  Widget _defaultHome = new LoginPage();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    // final textTheme = Theme.of(context).textTheme;
    return MaterialApp(
      title: 'Gaia',
      debugShowCheckedModeBanner: false,
      home: _defaultHome,
      routes: <String, WidgetBuilder>{
        // Set routes for using the Navigator.
        '/tabs': (BuildContext context) => new TabsPage(),
        '/login': (BuildContext context) => new LoginPage()
      },
    );
  }
}
拉扎·伊姆蒂亚兹

使用以下代码,任何参数都可以通过构造函数传递。

  void main() async {
      Widget _defaultHome = new LoginPage();
      runApp(MyApp(defaultHome: _defaultHome,));
    }
    
    class MyApp extends StatelessWidget {
     final Widget defaultHome;
      
        const MyApp({
        @required this.defaultHome,
        Key key,
      }) : super(key: key);
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        // final textTheme = Theme.of(context).textTheme;
        return MaterialApp(
          title: 'Gaia',
          debugShowCheckedModeBanner: false,
          home: defaultHome,
          routes: <String, WidgetBuilder>{
            // Set routes for using the Navigator.
            '/tabs': (BuildContext context) => new TabsPage(),
            '/login': (BuildContext context) => new LoginPage()
          },
        );
      }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章