Skip to content

Instantly share code, notes, and snippets.

@hawkkiller
Created June 3, 2024 09:53
Show Gist options
  • Save hawkkiller/032ccc81c01bc0754340b7ef138e8ce8 to your computer and use it in GitHub Desktop.
Save hawkkiller/032ccc81c01bc0754340b7ef138e8ce8 to your computer and use it in GitHub Desktop.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class AnimatedEdgeSlide extends SingleChildRenderObjectWidget {
const AnimatedEdgeSlide({
required Widget super.child,
required this.positionAnimation,
super.key,
});
final Animation<double> positionAnimation;
@override
RenderObject createRenderObject(BuildContext context) {
return RenderAnimatedEdgeSlide(
positionAnimation: positionAnimation,
);
}
@override
void updateRenderObject(BuildContext context, covariant RenderObject renderObject) {
(renderObject as RenderAnimatedEdgeSlide).positionAnimation = positionAnimation;
}
}
class RenderAnimatedEdgeSlide extends RenderProxyBox {
RenderAnimatedEdgeSlide({
required Animation<double> positionAnimation,
}) : _positionAnimation = positionAnimation;
Animation<double> get positionAnimation => _positionAnimation;
Animation<double> _positionAnimation;
set positionAnimation(Animation<double> value) {
if (value == _positionAnimation) {
return;
}
if (attached) {
_positionAnimation.removeListener(_animationListener);
}
_positionAnimation = value;
if (attached) {
_positionAnimation.addListener(_animationListener);
}
}
@override
void attach(covariant PipelineOwner owner) {
super.attach(owner);
_positionAnimation.addListener(_animationListener);
}
@override
void detach() {
_positionAnimation.removeListener(_animationListener);
super.detach();
}
void _animationListener() => markNeedsLayout();
@override
void performLayout() {
final child = this.child;
if (child == null || _positionAnimation.value == 0) {
size = Size.zero;
return;
}
child.layout(constraints, parentUsesSize: true);
size = Size(
child.size.width * _positionAnimation.value,
constraints.maxHeight,
);
}
@override
void paint(PaintingContext context, Offset offset) {
if (child != null && _positionAnimation.value > 0) {
context.paintChild(child!, offset);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment