Created
May 8, 2025 10:41
-
-
Save callmephil/033c2e2af00e4bba73fe07ff50ef7878 to your computer and use it in GitHub Desktop.
Animated Constraint (Instead of AnimatedContainer)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
class AnimatedConstraints extends ImplicitlyAnimatedWidget { | |
const AnimatedConstraints({ | |
super.key, | |
required this.constraints, | |
required super.duration, | |
super.curve, | |
this.child, | |
super.onEnd, | |
}); | |
final BoxConstraints constraints; | |
final Widget? child; | |
@override | |
AnimatedWidgetBaseState<AnimatedConstraints> createState() => | |
_AnimatedConstraintsState(); | |
@override | |
void debugFillProperties(DiagnosticPropertiesBuilder properties) { | |
super.debugFillProperties(properties); | |
properties.add( | |
DiagnosticsProperty<BoxConstraints>( | |
'constraints', | |
constraints, | |
defaultValue: null, | |
showName: false, | |
), | |
); | |
} | |
} | |
class _AnimatedConstraintsState | |
extends AnimatedWidgetBaseState<AnimatedConstraints> { | |
BoxConstraintsTween? _constraints; | |
@override | |
void forEachTween(TweenVisitor<Object?> visitor) { | |
_constraints = | |
visitor(_constraints, widget.constraints, (value) { | |
return BoxConstraintsTween(begin: value! as BoxConstraints); | |
}) | |
as BoxConstraintsTween?; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return ConstrainedBox( | |
constraints: _constraints?.evaluate(animation) ?? const BoxConstraints(), | |
child: widget.child, | |
); | |
} | |
@override | |
void debugFillProperties(DiagnosticPropertiesBuilder description) { | |
super.debugFillProperties(description); | |
description.add( | |
DiagnosticsProperty<BoxConstraintsTween>( | |
'constraints', | |
_constraints, | |
showName: false, | |
defaultValue: null, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment