![](/img/trans.png)
[英]Why Flutter statefull widget didn't change the state of another statefull widget when I call it inside its widget
[英]Why does flutter call its CLASSES as WIDGETS? Why not call it a class? why the term "widget" is used?
小部件是 class? 但是為什么不把它叫做 class 而不是小部件。 我知道 flutter 中的所有東西都是一個小部件,每個 class 都是一個小部件? 為什么呢? 也許打電話給每個 class 小工具或其他任何我想知道我的面試官是否會問我這個問題的東西
小部件是具有構建方法的 class:
@override
Widget build(BuildContext context) {
return Container(...);
}
普通的 class 沒有構建方法。
您還可以制作一個小部件:
@override
Widget build(BuildContext context) {
return Container(...); //Widget tree
}
var name;
var age;
var location;
//constructor
ClassName({this.name, this.age, this.location});
在構造函數中,您還可以制作所需的東西:
var name;
var age;
var location;
//constructor
ClassName({@required this.name, this.age, this.location});
您將構造函數放在 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
}
}
或擴展 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(...);
}
}
您可以像使用普通小部件一樣使用小部件:
WidgetName(
name: "David",
age: 28,
location: "Sofia, Bulgaria",
),
我希望這個答案可以幫助您了解普通 class 和小部件之間的區別
簡單地說,一個小部件是從小Widget
class 繼承的任何 class,通常從StatefulWidget
或StatelessWidget
開始。 一個小部件應該有一個特定的協議......特別是,它是上下文樹的一部分,並且還能夠在該樹中移動。 大多數小部件在 UI 上提供一些視圖,但有些不提供,並且僅用於TextTheme
類的設置。
每個 flutter Widgdet
都是一個不正確的小部件
每個 flutter class(小部件構建)都是正確的小部件。
class 永遠不會是一個小部件
Widget 它比 class 更高級。 Class 它是基礎。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.