【问题标题】:Can i start my app with a class except MyApp class?我可以使用除 MyApp 类之外的类来启动我的应用程序吗?
【发布时间】:2019-08-08 12:43:06
【问题描述】:

我正在开发一个需要登录页面的移动应用。我已经编写了主页。我希望应用程序从登录页面开始,而不是我编码的页面。有没有办法用除 myapp 类之外的另一个类来启动应用程序?

我尝试启动runApp(ClassThatIWant());,但似乎不起作用。

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: VoiceHome(),
    );
  }
}

【问题讨论】:

  • 您能分享一下您遇到了什么异常吗?只要它是一个小部件,您就可以在 runApp 方法中传递它。

标签: mobile flutter dart


【解决方案1】:

你总是运行 MyApp()。您可以通过以下方式访问您的登录信息:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: YourLoginPage(),
    );
  }
}

使用路由也很常见,像这样:


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
       title: "My app name",
       routes: {
        '/': (BuildContext context) => YourLoginPage(),
        '/home': (BuildContext context) => VoiceHome(),
        '/otherscreen': (BuildContext context) => OtherScreen(),
        '/etc': (BuildContext context) => EtcScreen(),

      },
    );
  }
}

并通过这样做来引用它:

Navigator.pushNamed(context, '/home');

【讨论】:

  • 只要它是一个小部件,您就可以在 runApp 方法中传递它。它不一定是 MyApp。
猜你喜欢
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 2013-02-21
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
相关资源
最近更新 更多