什么是 statelesswidget?
什么是 widget?
在 Flutter 中几乎所有对象都是一个 widget。与原生开发中的控件不同的是,Flutter 中 widget 的概念更广泛,它不仅表示 UI 元素,也可以表示一些功能性组件,如用于手势检测的 GestureDetector Widget…
有哪些方法?
//Widget 类继承自 DiagnosticableTree,DiagnosticableTree 即 诊断树,主要作用是提供调试信息。
abstract class Widget extends DiagnosticableTree {
//key 主要作用是决定下一次创建时是否复用旧的的 Widget。
const Widget({ this.key });
final Key? key;
//创建 Element
Element createElement();
String toStringShort() {
final String type = objectRuntimeType(this, 'Widget');
return key == null ? type : '$type-$key';
}
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.defaultDiagnosticsTreeStyle = DiagnosticsTreeStyle.dense;
}
bool operator ==(Object other) => super == other;
int get hashCode => super.hashCode;
static bool canUpdate(Widget oldWidget, Widget newWidget) {
return oldWidget.runtimeType == newWidget.runtimeType
&& oldWidget.key == newWidget.key;
}
static int _debugConcreteSubtype(Widget widget) {
return widget is StatefulWidget ? 1 :
widget is StatelessWidget ? 2 :
0;
}
}
statelesswidget?
statelesswidget 是 widget 的一个子类,用于无状态的 UI 构建。 它通常在 build 方法中通过嵌套其他 Widget 来构建 UI,在构建过程中会递归构建其嵌套的 Widget。