|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | + |
| 5 | +class DelayedDisplay extends StatefulWidget { |
| 6 | + final Widget child; |
| 7 | + final Duration delay; |
| 8 | + final Duration fadingDuration; |
| 9 | + final Curve slidingCurve; |
| 10 | + final Offset slidingBeginOffset; |
| 11 | + |
| 12 | + final bool fadeIn; |
| 13 | + |
| 14 | + const DelayedDisplay({ |
| 15 | + Key? key, |
| 16 | + required this.child, |
| 17 | + this.delay = Duration.zero, |
| 18 | + this.fadingDuration = const Duration(milliseconds: 800), |
| 19 | + this.slidingCurve = Curves.decelerate, |
| 20 | + this.slidingBeginOffset = const Offset(0.0, 0.10), |
| 21 | + this.fadeIn = true, |
| 22 | + }) : super(key: key); |
| 23 | + |
| 24 | + @override |
| 25 | + _DelayedDisplayState createState() => _DelayedDisplayState(); |
| 26 | +} |
| 27 | + |
| 28 | +class _DelayedDisplayState extends State<DelayedDisplay> |
| 29 | + with TickerProviderStateMixin { |
| 30 | + late AnimationController _opacityController; |
| 31 | + |
| 32 | + late Animation<Offset> _slideAnimationOffset; |
| 33 | + |
| 34 | + Timer? _timer; |
| 35 | + |
| 36 | + Duration get delay => widget.delay; |
| 37 | + |
| 38 | + Duration get opacityTransitionDuration => widget.fadingDuration; |
| 39 | + |
| 40 | + Curve get slidingCurve => widget.slidingCurve; |
| 41 | + |
| 42 | + Offset get beginOffset => widget.slidingBeginOffset; |
| 43 | + |
| 44 | + bool get fadeIn => widget.fadeIn; |
| 45 | + |
| 46 | + @override |
| 47 | + void initState() { |
| 48 | + super.initState(); |
| 49 | + |
| 50 | + _opacityController = AnimationController( |
| 51 | + vsync: this, |
| 52 | + duration: opacityTransitionDuration, |
| 53 | + ); |
| 54 | + |
| 55 | + final CurvedAnimation curvedAnimation = CurvedAnimation( |
| 56 | + curve: slidingCurve, |
| 57 | + parent: _opacityController, |
| 58 | + ); |
| 59 | + |
| 60 | + _slideAnimationOffset = Tween<Offset>( |
| 61 | + begin: beginOffset, |
| 62 | + end: Offset.zero, |
| 63 | + ).animate(curvedAnimation); |
| 64 | + |
| 65 | + _runFadeAnimation(); |
| 66 | + } |
| 67 | + |
| 68 | + @override |
| 69 | + void dispose() { |
| 70 | + _opacityController.dispose(); |
| 71 | + _timer?.cancel(); |
| 72 | + super.dispose(); |
| 73 | + } |
| 74 | + |
| 75 | + @override |
| 76 | + void didUpdateWidget(DelayedDisplay oldWidget) { |
| 77 | + super.didUpdateWidget(oldWidget); |
| 78 | + if (oldWidget.fadeIn == fadeIn) { |
| 79 | + return; |
| 80 | + } |
| 81 | + _runFadeAnimation(); |
| 82 | + } |
| 83 | + |
| 84 | + void _runFadeAnimation() { |
| 85 | + _timer = Timer(delay, () { |
| 86 | + fadeIn ? _opacityController.forward() : _opacityController.reverse(); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + @override |
| 91 | + Widget build(BuildContext context) { |
| 92 | + return SlideTransition( |
| 93 | + position: _slideAnimationOffset, |
| 94 | + child: widget.child, |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +pushNewScreen(BuildContext context, Widget page) { |
| 100 | + Navigator.push( |
| 101 | + context, |
| 102 | + createRoute(page), |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +Route createRoute(Widget page) { |
| 107 | + return PageRouteBuilder( |
| 108 | + transitionDuration: const Duration(microseconds: 800), |
| 109 | + pageBuilder: (context, animation, secondaryAnimation) => page, |
| 110 | + transitionsBuilder: (context, animation, secondaryAnimation, child) { |
| 111 | + return SlideTransition( |
| 112 | + position: animation.drive(Tween<Offset>( |
| 113 | + begin: const Offset(1.0, 0.0), |
| 114 | + end: Offset.zero, |
| 115 | + )), |
| 116 | + child: child, |
| 117 | + ); |
| 118 | + }, |
| 119 | + ); |
| 120 | +} |
0 commit comments