From 677060d528643b93f79de0aa899d742e44c143a7 Mon Sep 17 00:00:00 2001 From: Gabriel Terwesten Date: Tue, 23 May 2023 23:01:34 +0200 Subject: [PATCH] feat: make `AnimatableRenderObjectWidget` work with `InheritedWidget`s (#45) --- .../lib/src/animatable_flutter_widgets.dart | 30 ++++++++++++-- .../src/animatable_render_object_widget.dart | 39 ++++++++++++++----- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/packages/fleet/lib/src/animatable_flutter_widgets.dart b/packages/fleet/lib/src/animatable_flutter_widgets.dart index 2deb70b..3ab2241 100644 --- a/packages/fleet/lib/src/animatable_flutter_widgets.dart +++ b/packages/fleet/lib/src/animatable_flutter_widgets.dart @@ -33,6 +33,7 @@ class AAlign extends Align @override _AlignAnimatableParameters createAnimatableParameters( + covariant RenderPositionedBox renderObject, AnimatableParameterHost host, ) { return ( @@ -43,7 +44,10 @@ class AAlign extends Align } @override - void updateAnimatableParameters(_AlignAnimatableParameters parameters) { + void updateAnimatableParameters( + BuildContext context, + _AlignAnimatableParameters parameters, + ) { parameters ..alignment.value = alignment ..widthFactor.value = widthFactor @@ -242,13 +246,17 @@ class AOpacity extends Opacity @override _OpacityAnimatableParameters createAnimatableParameters( + covariant RenderOpacity renderObject, AnimatableParameterHost host, ) { return (opacity: AnimatableDouble(opacity, host: host)); } @override - void updateAnimatableParameters(_OpacityAnimatableParameters parameters) { + void updateAnimatableParameters( + BuildContext context, + _OpacityAnimatableParameters parameters, + ) { parameters.opacity.value = opacity; } @@ -277,13 +285,17 @@ class APadding extends Padding @override _PaddingAnimatableParameters createAnimatableParameters( + covariant RenderPadding renderObject, AnimatableParameterHost host, ) { return (padding: AnimatableEdgeInsetsGeometry(padding, host: host),); } @override - void updateAnimatableParameters(_PaddingAnimatableParameters parameters) { + void updateAnimatableParameters( + BuildContext context, + _PaddingAnimatableParameters parameters, + ) { parameters.padding.value = padding; } @@ -543,6 +555,7 @@ class ASizedBox extends SizedBox @override _SizedBoxAnimatableParameters createAnimatableParameters( + covariant RenderConstrainedBox renderObject, AnimatableParameterHost host, ) { return ( @@ -554,7 +567,10 @@ class ASizedBox extends SizedBox } @override - void updateAnimatableParameters(_SizedBoxAnimatableParameters parameters) { + void updateAnimatableParameters( + BuildContext context, + _SizedBoxAnimatableParameters parameters, + ) { parameters.additionalConstraints.value = _additionalConstraints; } @@ -588,6 +604,7 @@ class ASliverOpacity extends SliverOpacity @override _SliverOpacityAnimatableParameters createAnimatableParameters( + covariant RenderSliverOpacity renderObject, AnimatableParameterHost host, ) { return (opacity: AnimatableDouble(opacity, host: host),); @@ -595,6 +612,7 @@ class ASliverOpacity extends SliverOpacity @override void updateAnimatableParameters( + BuildContext context, _SliverOpacityAnimatableParameters parameters, ) { parameters.opacity.value = opacity; @@ -627,6 +645,7 @@ class ASliverPadding extends SliverPadding @override _SliverPaddingAnimatableParameters createAnimatableParameters( + covariant RenderSliverPadding renderObject, AnimatableParameterHost host, ) { return (padding: AnimatableEdgeInsetsGeometry(padding, host: host),); @@ -634,6 +653,7 @@ class ASliverPadding extends SliverPadding @override void updateAnimatableParameters( + BuildContext context, _SliverPaddingAnimatableParameters parameters, ) { parameters.padding.value = padding; @@ -742,6 +762,7 @@ abstract class _TransformBase extends ATransform @override _TransformAnimatableParameters createAnimatableParameters( + covariant RenderTransform renderObject, AnimatableParameterHost host, ) { return ( @@ -753,6 +774,7 @@ abstract class _TransformBase extends ATransform @override void updateAnimatableParameters( + BuildContext context, _TransformAnimatableParameters parameters, ) { updateTransformInputParameters(parameters.transformInput); diff --git a/packages/fleet/lib/src/animatable_render_object_widget.dart b/packages/fleet/lib/src/animatable_render_object_widget.dart index d9616ef..16ffd2b 100644 --- a/packages/fleet/lib/src/animatable_render_object_widget.dart +++ b/packages/fleet/lib/src/animatable_render_object_widget.dart @@ -14,12 +14,15 @@ abstract class AnimatableRenderObjectWidget const AnimatableRenderObjectWidget({super.key}); /// Creates the [AnimatableParameter]s for this widget. - T createAnimatableParameters(AnimatableParameterHost host); + T createAnimatableParameters( + covariant RenderObject renderObject, + AnimatableParameterHost host, + ); /// Updates the [AnimatableParameter]s with the new values. /// /// This is called when the widget is rebuilt. - void updateAnimatableParameters(T parameters); + void updateAnimatableParameters(BuildContext context, T parameters); /// Updates the [RenderObject] with the current /// [AnimatableParameter.animatedValue]s. @@ -45,22 +48,32 @@ mixin _AnimatableRenderObjectElementMixin void update(covariant RenderObjectWidget newWidget) { if (_animatableParameters == null) { if (Transaction.of(this) != null) { - _animatableParameters = widget.createAnimatableParameters(this); + _animatableParameters = + widget.createAnimatableParameters(renderObject, this); } } super.update(newWidget); - final animatableParameters = _animatableParameters; - if (animatableParameters != null) { - applyUpdateToAnimatableParameters(); - _updateRenderObjectWithAnimatableParameters(); - } + // TODO: It's inefficient to do this now, after the render object has + // already been updated by updateRenderObject. But there currently is no + // other way to do this. + _applyUpdateToAnimatableParameters(); + } + + @override + void performRebuild() { + super.performRebuild(); + + // TODO: It's inefficient to do this now, after the render object has + // already been updated by updateRenderObject. But there currently is no + // other way to do this. + _applyUpdateToAnimatableParameters(); } @override void updateAnimatableParameters() { - widget.updateAnimatableParameters(_animatableParameters!); + widget.updateAnimatableParameters(this, _animatableParameters!); } @override @@ -87,6 +100,14 @@ mixin _AnimatableRenderObjectElementMixin _animatableParameters!, ); } + + void _applyUpdateToAnimatableParameters() { + final animatableParameters = _animatableParameters; + if (animatableParameters != null) { + applyUpdateToAnimatableParameters(); + _updateRenderObjectWithAnimatableParameters(); + } + } } /// A mixin that allows a [SingleChildRenderObjectWidget] to transparently