【发布时间】:2021-09-16 18:49:32
【问题描述】:
颤动
我正在尝试将一个小部件显示到 Stack IF an condition (true or false ) 中,它可以正常工作
但是当我需要将条件 bool 更改为 SetState 以再次隐藏小部件时,它也可以工作,但会出现烦人的错误消息,即 setState() or markNeedsBuild() called when widget tree was locked。
我的代码很复杂,但我将展示一个简单的类似示例
bool displayWidget = false;
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Stack(
children: [
Container(
child: TextButton(
onPressed: () {
final result = await FilePicker.platform.pickFiles(allowMultiple: false );
if (result == null) return;
final path = result.files.single.path;
setState(() => displayWidget = true);
},
child: Text ("studio")
),
),
displayWidget?
GestureDetector(
onTap: ()=> setState(() => displayWidget = false), // the error happen when i click here
child: Container(
child: Image.asset("here is the picture in full secreen"),
),
):Container()
],
),
);
}
}
我知道有比这种方式更好的照片查看器 :D 但我只为其他真实案例举一个简单的例子
【问题讨论】:
标签: flutter dart widget setstate