Skip to content

Instantly share code, notes, and snippets.

@callmephil
Created May 8, 2025 10:41
Show Gist options
  • Save callmephil/033c2e2af00e4bba73fe07ff50ef7878 to your computer and use it in GitHub Desktop.
Save callmephil/033c2e2af00e4bba73fe07ff50ef7878 to your computer and use it in GitHub Desktop.
Animated Constraint (Instead of AnimatedContainer)
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