答案 0 :(得分:1)
用 child
将您的 builder
包裹在 Container()
中。
//...
builder: (BuildContext context, Widget? child) {
return Container(child: child);
},
//...
这是如何使用 AnimatedBuilder()
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> with
TickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
duration: const Duration(seconds: 10),
vsync: this,
)..repeat();
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
// This widget wont rebuild.
child: Container(
child: Text("I can not rebuild weeeee.....!!!"), color: Colors.green),
animation: _controller,
builder: (BuildContext context, Widget? child) {
return Transform.rotate(
angle: _controller.value * 2.0 * math.pi,
// here you will pass your widget
child: child,
);
},
);
}
}