class MyHomePage与runApp函数之间的关系是什么?

huangapple go评论63阅读模式
英文:

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 &#39;package:flutter/material.dart&#39;; 

void main() =&gt; runApp(MyApp()); 


class MyApp extends StatelessWidget { 

 @override Widget build(BuildContext context) { return MaterialApp( title: &#39;Flutter Demo&#39;, 
theme: ThemeData(
  primarySwatch: Colors.blue, ), 

home: MyHomePage(title: &#39;Flutter Demo Home Page&#39;), ); }} 

class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); 

final String title; 

@override 

_MyHomePageState createState() =&gt; _MyHomePageState();} 

class _MyHomePageState extends State&lt;MyHomePage&gt; { 

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: &lt;Widget&gt;[ 

Text( &#39;You have pushed the button this many times:&#39;, ), Text( &#39;$_counter&#39;, style: Theme.of(context).textTheme.display1, ), ], ), ),

 floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: &#39;Increment&#39;, child: Icon(Icons.add);}}

答案1

得分: 2

Flutter基于一个小部件树。只有树的根节点被传递给runApp方法。在这个示例中,MyApp是这个树的根节点,所有其他小部件都只通过位于MyApp或其子级中来引用。例如,在这里,MyHomePageMyApp的子级。远离页面,"较小"的小部件,如TextContainer,也是这个树的一部分,并在单个页面中引用。

关于你不明白的部分,你有更具体的问题吗?或者我已经回答了你的问题?

英文:

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

huangapple
  • 本文由 发表于 2023年1月8日 23:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定