Skip to content

Instantly share code, notes, and snippets.

@Ansh-Rathod
Created March 8, 2023 11:56
Show Gist options
  • Save Ansh-Rathod/2888656d1b6d99129830766e5e79a62a to your computer and use it in GitHub Desktop.
Save Ansh-Rathod/2888656d1b6d99129830766e5e79a62a to your computer and use it in GitHub Desktop.
slide up animation with fade animation
import 'dart:async';
import 'package:flutter/material.dart';
class DelayedDisplay extends StatefulWidget {
final Widget child;
final Duration delay;
final Duration fadingDuration;
final Curve slidingCurve;
final Offset slidingBeginOffset;
final bool fadeIn;
const DelayedDisplay({
Key? key,
required this.child,
this.delay = Duration.zero,
this.fadingDuration = const Duration(milliseconds: 800),
this.slidingCurve = Curves.decelerate,
this.slidingBeginOffset = const Offset(0.0, 0.10),
this.fadeIn = true,
}) : super(key: key);
@override
_DelayedDisplayState createState() => _DelayedDisplayState();
}
class _DelayedDisplayState extends State<DelayedDisplay>
with TickerProviderStateMixin {
late AnimationController _opacityController;
late Animation<Offset> _slideAnimationOffset;
Timer? _timer;
Duration get delay => widget.delay;
Duration get opacityTransitionDuration => widget.fadingDuration;
Curve get slidingCurve => widget.slidingCurve;
Offset get beginOffset => widget.slidingBeginOffset;
bool get fadeIn => widget.fadeIn;
@override
void initState() {
super.initState();
_opacityController = AnimationController(
vsync: this,
duration: opacityTransitionDuration,
);
final CurvedAnimation curvedAnimation = CurvedAnimation(
curve: slidingCurve,
parent: _opacityController,
);
_slideAnimationOffset = Tween<Offset>(
begin: beginOffset,
end: Offset.zero,
).animate(curvedAnimation);
_runFadeAnimation();
}
@override
void dispose() {
_opacityController.dispose();
_timer?.cancel();
super.dispose();
}
@override
void didUpdateWidget(DelayedDisplay oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.fadeIn == fadeIn) {
return;
}
_runFadeAnimation();
}
void _runFadeAnimation() {
_timer = Timer(delay, () {
fadeIn ? _opacityController.forward() : _opacityController.reverse();
});
}
@override
Widget build(BuildContext context) {
return SlideTransition(
position: _slideAnimationOffset,
child: widget.child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment