flutter中的runApp(new MyApp())和runApp(new MaterialApp())有什么区别? [英] What is the difference between runApp(new MyApp()) and runApp(new MaterialApp()) in flutter?

查看:454
本文介绍了flutter中的runApp(new MyApp())和runApp(new MaterialApp())有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在波动中,我们可以传递一个无状态小部件,该小部件将MaterialApp实例返回给runApp()函数,如下所示:

In flutter we can pass a stateless widget that returns a MaterialApp instance to the runApp() function like this:

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

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            ...
        );
    }
}

或者我们可以将MaterialApp的实例直接传递给runApp()函数,如下所示:

or we can pass the instance of MaterialApp directly to the runApp() function like so:

void main()=>runApp(
    new MaterialApp(
       ...
    );
);

这些方式之间有什么区别?谢谢.

What is the difference between these to ways? Thanks.

推荐答案

视觉行为没有区别. 变化的是热重装的行为方式.

There's no difference in visual behavior. What changes is how hot reload behaves.

例如,如果您使用的是runApp(MaterialApp()),则从

For example if you used runApp(MaterialApp()), changing from

runApp(MaterialApp(title: 'Foo'))

runApp(MaterialApp(title: 'Bar'))

然后热重载将不会考虑更改.

then the hot reload wouldn't take changes into consideration.

如果您有以下课程:

class MyApp {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Foo',
    );
  )
}

并像这样使用它:

runApp(MyApp())

然后将title中的title更改为正确的热加载.

then changing title of MyApp would be correctly hot reloaded.

这篇关于flutter中的runApp(new MyApp())和runApp(new MaterialApp())有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆