简体   繁体   English

为什么 flutter 将其 CLASSES 称为 WIDGETS? 为什么不称它为 class? 为什么使用“小部件”一词?

[英]Why does flutter call its CLASSES as WIDGETS? Why not call it a class? why the term "widget" is used?

Widget is a class?小部件是 class? But why not call it just class and not widget.但是为什么不把它叫做 class 而不是小部件。 I know that every thing in the flutter is a widget and every class is a widget?我知道 flutter 中的所有东西都是一个小部件,每个 class 都是一个小部件? Why though?为什么呢? Maybe call every class gadget or anything else I was wondering if my interviewer might ask me this question也许打电话给每个 class 小工具或其他任何我想知道我的面试官是否会问我这个问题的东西

A widget is a class that has the build method:小部件是具有构建方法的 class:

@override
Widget build(BuildContext context) {

  return Container(...);

}

A normal class doesn't have a build method.普通的 class 没有构建方法。


You also can make a widget:您还可以制作一个小部件:

  1. create a class that extends StatelessWidget or StatefulWidget创建一个扩展 StatelessWidget 或 StatefulWidget 的 class
  2. create a widget tree inside the build method:在 build 方法中创建一个小部件树:
@override
Widget build(BuildContext context) {

  return Container(...); //Widget tree

}
  1. (optional) create a constructor that gives you all the information you need: (可选)创建一个构造函数,为您提供所需的所有信息:
var name;
var age;
var location;

//constructor
ClassName({this.name, this.age, this.location});

in the constructor you also can make things required:在构造函数中,您还可以制作所需的东西:

var name;
var age;
var location;

//constructor
ClassName({@required this.name, this.age, this.location});

you put the constructor at the beginning of your class:您将构造函数放在 class 的开头:

class WidgetName extends StatelessWidget {
  
  //constructor
  WidgetName({
    @required this.name,
    this.age,
    this.location,
  });
 
  var name;
  var age;
  var location;


  @override
  Widget build(BuildContext context) {

    return Container(...); //Widget tree

  }
}

or extending StatefulWidget:或扩展 StatefulWidget:

class WidgetName extends StatefulWidget {
  
  var name;
  var age;
  var location;

  //constructor
  WidgetName({this.name, this.age, this.location});
  

  @override
  _WidgetNameState createState() => _WidgetNameState();
}

class _WidgetNameState extends State<WidgetName> {

  //inside of this class you can get the data from the constructor like this: widget.name


  @override
  Widget build(BuildContext context) {
    return Container(...);
  }
}

and you can use the widget like a normal widget:您可以像使用普通小部件一样使用小部件:

WidgetName(
  name: "David",
  age: 28,
  location: "Sofia, Bulgaria",
),

I hope this answer helps you to understand the difference between a normal class and a widget我希望这个答案可以帮助您了解普通 class 和小部件之间的区别

Simply, a widget is any class that inherits from the Widget class, typically starting from StatefulWidget or StatelessWidget .简单地说,一个小部件是从小Widget class 继承的任何 class,通常从StatefulWidgetStatelessWidget开始。 A widget is expected to have a certain protocol... in particular, be part of the context tree and also be able to be moved in that tree.一个小部件应该有一个特定的协议......特别是,它是上下文树的一部分,并且还能够在该树中移动。 Most widgets contribute some view on the UI, but some don't, and are merely for settings like TextTheme .大多数小部件在 UI 上提供一些视图,但有些不提供,并且仅用于TextTheme类的设置。

every flutter class is a Widgdet not right每个 flutter Widgdet都是一个不正确的小部件

every flutter class (Widget build) is a Widgdet is right.每个 flutter class(小部件构建)都是正确的小部件。

class will never be a widget class 永远不会是一个小部件

Widget it's more high level construction than class. Widget 它比 class 更高级。 Class it's foundation. Class 它是基础。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我在其小部件中调用 Flutter 有状态小部件时,它没有更改另一个有状态小部件的 state - Why Flutter statefull widget didn't change the state of another statefull widget when I call it inside its widget 为什么有状态的小部件在 flutter 中定义为两个类? - Why are stateful widgets defined as two classes in flutter? 为什么扑动的小部件是不可变的? - Why are flutter widgets immutable? 为什么在颤动的 Wrap 小部件中添加小部件时会留下空间? - why space is leaving while adding widgets inside Wrap widget in flutter? 来自另一个列出的小部件的颤动调用小部件 - flutter call widget from another listed widgets 为什么 Flutter State 对象需要一个 Widget? - Why does Flutter State object require a Widget? 为什么 flutter 中的小部件在 Tab 上不起作用? - why does widget in flutter not working onTab? 为什么 flutter 在类内部为 Firestore 方法返回错误,但在小部件内部很好? - Why does flutter return an error for Firestore methods inside of classes but is fine inside of widget? 为什么小部件的 state 在 flutter 上没有改变? - Why the state of widgets is not changing on flutter? 为什么有状态和无状态类是flutter中的抽象类? - Why are statefull and stateless class are abstract classes in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM