微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

flutter setState 不会重新渲染我的孩子有状态小部件

如何解决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 只是一个普通的有状态小部件

我不明白为什么。 任何人都知道原因。谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。