我有一个设置,其中
StatefulWidget
创建自定义(叶)
RenderBox
. 是否有可能
渲染器
在的生存期内释放并重新创建
State
?
背景。我想将大量计算/动画控制器的某些结果保存在
RenderObject
这就是为什么对我来说
渲染对象
只要
状态
封闭小部件的。
import 'package:flutter/widgets.dart';
class SquaredCircle extends StatefulWidget {
const SquaredCircle({Key key, this.radius}) : super(key: key);
final double radius;
@override
State<StatefulWidget> createState() => _SquaredCircleState();
}
class _SquaredCircleState extends State<SquaredCircle> {
int _squareness = 42;
@override
Widget build(BuildContext context) {
final foo = widget.radius * _squareness;
return _SquaredCircleRenderObjectWidget(foo);
}
}
class _SquaredCircleRenderObjectWidget extends LeafRenderObjectWidget {
_SquaredCircleRenderObjectWidget(this.foo);
final double foo;
@override
_RenderSquaredCircle createRenderObject(BuildContext context) {
return _RenderSquaredCircle(foo: foo);
}
@override
void updateRenderObject(BuildContext context, _RenderSquaredCircle renderObject) {
renderObject..foo = foo;
}
}
class _RenderSquaredCircle extends RenderBox {
_RenderSquaredCircle({double foo})
: assert(foo != null),
_foo = foo;
double get foo => _foo;
double _foo;
set foo(double value) {
assert(value != null);
if (_foo == value) return;
_foo = value;
markNeedsPaint();
}
// ...
}