[英]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:您还可以制作一个小部件:
@override
Widget build(BuildContext context) {
return Container(...); //Widget tree
}
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,通常从StatefulWidget
或StatelessWidget
开始。 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.