英文:
what is the relation between class MyHomePage and runApp Function
问题
在默认的Flutter项目中,runApp
函数接受MyApp
类作为参数,所以当我们运行应用程序时,MyApp
类的内容和StatelessWidget
类的内容正常显示。但是,runApp
函数如何获取其他类如MyHomePage
类和StatefulWidget
类的内容呢?
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('You have pushed the button this many times:'),
Text('$_counter', style: Theme.of(context).textTheme.display1),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
请注意,runApp
函数只接受一个Widget
作为参数,通常是整个应用程序的根Widget
,而在上面的代码中,MyApp
类是根Widget
,它包含了整个应用程序的UI结构,包括MyHomePage
类和StatefulWidget
类的内容。这是因为在Flutter中,您可以嵌套不同的Widget
来构建复杂的UI层次结构。
英文:
in default flutter project runApp function it countain the MyApp class as aparameter so when we run app It's normal for the content of the class MyApp and content of StatelessWidget class to appear , but how the run app function get content of others classes like MyHomepage class and StatefullWidget class.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue, ),
home: MyHomePage(title: 'Flutter Demo Home Page'), ); }}
class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++; }); }
@override
Widget build(BuildContext context) {
return Scaffold( appBar: AppBar(
title: Text(widget.title), ), body: Center(
mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.display1, ), ], ), ),
floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: Icon(Icons.add);}}
答案1
得分: 2
Flutter基于一个小部件树。只有树的根节点被传递给runApp
方法。在这个示例中,MyApp
是这个树的根节点,所有其他小部件都只通过位于MyApp
或其子级中来引用。例如,在这里,MyHomePage
是MyApp
的子级。远离页面,"较小"的小部件,如Text
或Container
,也是这个树的一部分,并在单个页面中引用。
关于你不明白的部分,你有更具体的问题吗?或者我已经回答了你的问题?
英文:
Flutter is based on a widget tree. To the runApp
method only the root of the tree is passed. In this example, MyApp
is the root of this tree and all other widgets are referenced only by being in MyApp
or its children. For example, here MyHomePage
is a child of MyApp
. Away from the pages, the "smaller" widgets like Text
or Container
are also part of this tree and referenced in the single pages.
Do you have a more specific question about what you don't understand or have I already answered your question?
答案2
得分: 0
无论您在 runApp() 内传递什么有效的小部件,都将显示出来,并且将被视为应用程序树的根小部件。
因此,最好使用一个返回 MaterialApp() 小部件的小部件,因为这将构成您的应用程序的窗口框架,其他小部件将显示为页面。
这些将路由到 MaterialApp 小部件的页面小部件应该依次返回一个 Scaffold() 小部件。
英文:
Whatever valid widget you pass inside runApp() will be displayed and it will be considered as the root widget of the app tree.
So, it is best to use a widget which returns a MaterialApp() widget as this will constitute the window frame of your app on which other widgets will be displayed as pages.
These page widgets that will be routed to the MaterialApp widget should in turn return a Scaffold() widget.
答案3
得分: 0
我昨天找到了答案,答案是 MyApp 类中的参数 home 采用 MyHomePage 类作为其值与 runApp 函数之间的关系。
英文:
I found out the answer yesterday and it is that the relationship between the runApp function and MyHomePage class is that parameter named home in MyApp class takes MyHomePage class as its value
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论