我有一个BlocBuilder函数,该函数会生成一列要添加到屏幕上的动画的小部件列表:
Center(
child: BlocBuilder<DetectionBloc, DetectionState>(builder: (context, state) {
if (state is ImageDetectedState) {
state.detections.forEach((detection) {
stackItems.add(Positioned(
left: detection['rect']['x'] * (_keyImageStack.currentContext.findRenderObject() as RenderBox).size.width,
top: detection['rect']['y'] * (_keyImageStack.currentContext.findRenderObject() as RenderBox).size.height,
child: AnimatedOpacity(
opacity: _visible ? 1.0 : 0.0,
duration: Duration(milliseconds: 2000),
child: Container(
color: Colors.pink,
height: detection['rect']['h'] * (_keyImageStack.currentContext.findRenderObject() as RenderBox).size.height,
width: detection['rect']['w'] * (_keyImageStack.currentContext.findRenderObject() as RenderBox).size.width,
),
),
));
});
}
return Stack(
key: _keyImageStack,
alignment: Alignment.topLeft,
children: stackItems,
);
}),
)
问题是,当我触发setState
来更改_visible
变量时,出现错误:
The method 'findRenderObject' was called on null.
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_visible = !_visible;
});
},
tooltip: 'Toggle Opacity',
child: Icon(Icons.flip),
),
我认为这与使用BlocBuilder创建AnimatedOpacity小部件有关。
还有其他方法可以实现此目的吗,或者有任何变通办法可以使这些动画正常工作?