flutter - 颤振 setState 不会重新渲染我的孩子有状态小部件
问题描述
这是我的代码
var _showGoogleMaps = false;
@override
void initState() {
super.initState();
Future.delayed(Duration(milliseconds: 2000), () => setState(() {
_showGoogleMaps = true;
} ));
}
@override
Widget build(BuildContext context) {
return
// example 1
/
/ _showGoogleMaps ? Container(height: 500, color: Colors.blue)
// : Container(height: 500, color: Colors.red);
// example 2
FullImageView(
imgCodeVal: imgCodeVal,
child: Container(height: 500, color: _showGoogleMaps ? Colors.blue : Colors.red));
}
class FullImageView extends StatefulWidget {
final ValueListenable<String> imgCodeVal;
final Widget child;
FullImageView({Key key, this.imgCodeVal, this.child}) : super(key: key);
@override
_FullImageViewState createState() => _FullImageViewState();
}
class _FullImageViewState extends State<FullImageView> {
String _imgCode;
Widget _child;
double _height = 0;
bool _showImage = false;
@override
void initState() {
super.initState();
_child = widget.child;
}
@override
Widget build(BuildContext context) {
return Stack( children: [
_child
)))))
]);
}
}
在示例 1 中,容器颜色会在 2 秒后从红色变为蓝色。
但在示例 2 中,颜色为红色且未更改。FullImageView 只是一个普通的有状态小部件
我不明白为什么。任何人都知道原因。谢谢
解决方案
推荐阅读
- windows - PowerShell 脚本太慢
- javascript - 覆盖 JS 中特定单元测试用例的 sinon 存根
- django-forms - 字段 'id' 需要一个数字,但得到了 'create'。请求方法:
- javascript - 谷歌地图搜索框在模态中具有自动完成功能
- c# - 如何通过谓词计算枚举列表?
- jwt - Spring Webflux:帮助将代码转换为非阻塞
- javascript - Django 在一个 html 搜索字段中进行多项搜索 + selectpicker = 如何更改操作值?
- java - 制作此动画的最佳解决方案是什么
- mysql - 如果客户的销售额超过 2 次,则仅汇总每位客户的最后 5 次销售额
- php - 将数组数据推送到会话时出错