Skip to content

Instantly share code, notes, and snippets.

@ChrisMarxDev
Last active November 25, 2024 12:50
Show Gist options
  • Save ChrisMarxDev/f6b07c20f57821cbaa54cafe96c82239 to your computer and use it in GitHub Desktop.
Save ChrisMarxDev/f6b07c20f57821cbaa54cafe96c82239 to your computer and use it in GitHub Desktop.
Very customisable transition to add some sauce
import 'package:flutter/material.dart';
class ScaleFadeTransition extends StatefulWidget {
const ScaleFadeTransition({
required this.child,
super.key,
this.show = false,
this.alignment = Alignment.center,
this.curve = Curves.easeInOut,
});
final Widget child;
final bool show;
final Alignment alignment;
final Curve curve;
@override
State<ScaleFadeTransition> createState() => _ScaleFadeTransitionState();
}
class _ScaleFadeTransitionState extends State<ScaleFadeTransition>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _opacity;
late final Animation<double> _scale;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 330),
value: widget.show ? 1 : 0,
);
_scale = Tween<double>(begin: 0, end: 1).animate(
CurvedAnimation(
parent: _controller,
curve: widget.curve,
),
);
_opacity = Tween<double>(begin: 0, end: 1).animate(
CurvedAnimation(
parent: _controller,
curve: Interval(0.3, 1, curve: widget.curve),
),
);
}
@override
void didUpdateWidget(covariant ScaleFadeTransition oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.show != widget.show) {
if (widget.show) {
_controller.forward(from: 0);
} else {
_controller.reverse(from: 1);
}
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SizeTransition(
axisAlignment: -1,
sizeFactor: _scale,
child: FadeTransition(opacity: _opacity, child: widget.child),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment