哪种类型的小部件不会有不能改变的状态/属性?我想不出任何可能的事情。按钮有文本,图像有大小,文本有颜色,等等。什么小部件会没有某种属性呢?
在Flutter演示代码中,"MyApp“是无状态的,但它具有属性。为什么这是无状态的,而不是有状态的?
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}发布于 2019-04-26 19:57:59
当您描述的用户界面部分不依赖于对象本身中的配置信息以及在其中膨胀小部件的BuildContext之外,
无状态小部件非常有用。
那么这到底是什么意思呢?StatelessWidget不包含它需要随时间跟踪更改的属性-例如,文本框就包含这些属性。
布局就是一个典型的StatelessWidget示例。它确实包含可能需要或可能不需要维护状态的子项,但是您的布局本身并不需要维护状态。
这样做的一个巨大优势是,StatelessWidget不会触发重新构建。并不是所有的小部件树都会响应文本框的文本更改,只有在文本框发生更改时被跟踪的部分才会响应。不用说,这会带来巨大的性能影响。
发布于 2019-04-26 21:14:59
有两种状态:
etc...
StatelessWidget是一个没有内部状态的小部件,但它可以使用外部状态。
当参数更改(包括InheritedWidget更新)时,StatelessWidget将正确更新。
https://stackoverflow.com/questions/55866663
复制相似问题