Last active
August 12, 2022 06:02
-
-
Save iapicca/70f6cbe15819a32e4e274e8a89d4b13f to your computer and use it in GitHub Desktop.
issues_109020
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/material.dart'; | |
| import 'package:go_router/go_router.dart'; | |
| abstract class Routes { | |
| static const splash = '/'; | |
| static const home = '/home'; | |
| } | |
| void main() => runApp(const MyApp()); | |
| class MyApp extends StatefulWidget { | |
| const MyApp({super.key}); | |
| @override | |
| State<MyApp> createState() => _MyAppState(); | |
| } | |
| class _MyAppState extends State<MyApp> { | |
| late final GoRouter _router; | |
| void _splashScreenListener() async { | |
| if (_router.location == Routes.splash) { | |
| await Future.delayed(const Duration(seconds: 5)); | |
| _router.go(Routes.home); | |
| } | |
| } | |
| @override | |
| void initState() { | |
| _router = GoRouter( | |
| routes: [ | |
| GoRoute( | |
| path: Routes.splash, | |
| builder: (context, state) => const SplashScreen(), | |
| ), | |
| GoRoute( | |
| path: Routes.home, | |
| pageBuilder: (context, state) => CustomTransitionPage<void>( | |
| key: state.pageKey, | |
| child: const HomePage(), | |
| transitionsBuilder: (context, animation, _, child) { | |
| final tween = Tween( | |
| begin: const Offset(0.0, 1.0), | |
| end: Offset.zero, | |
| ).chain(CurveTween(curve: Curves.ease)); | |
| return SlideTransition( | |
| position: animation.drive(tween), | |
| child: child, | |
| ); | |
| }, | |
| ), | |
| ), | |
| ], | |
| )..addListener(_splashScreenListener); | |
| super.initState(); | |
| } | |
| @override | |
| void dispose() { | |
| _router | |
| ..removeListener(_splashScreenListener) | |
| ..dispose(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp.router( | |
| routeInformationParser: _router.routeInformationParser, | |
| routerDelegate: _router.routerDelegate, | |
| routeInformationProvider: _router.routeInformationProvider, | |
| ); | |
| } | |
| } | |
| class SplashScreen extends StatefulWidget { | |
| final Duration animationDuration; | |
| const SplashScreen({ | |
| super.key, | |
| this.animationDuration = const Duration(seconds: 1), | |
| }); | |
| @override | |
| State<SplashScreen> createState() => _SplashScreenState(); | |
| } | |
| class _SplashScreenState extends State<SplashScreen> | |
| with TickerProviderStateMixin { | |
| late final AnimationController _controller; | |
| late final Animation<double> _animation; | |
| @override | |
| void initState() { | |
| _controller = AnimationController( | |
| duration: widget.animationDuration, | |
| vsync: this, | |
| )..repeat(reverse: true); | |
| _animation = CurvedAnimation( | |
| parent: _controller, | |
| curve: Curves.easeIn, | |
| ); | |
| super.initState(); | |
| } | |
| @override | |
| void dispose() { | |
| _controller.dispose(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(context) => Scaffold( | |
| body: Center( | |
| child: FadeTransition( | |
| opacity: _animation, | |
| child: FlutterLogo( | |
| size: MediaQuery.of(context).size.shortestSide / 2, | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| class HomePage extends StatelessWidget { | |
| const HomePage({super.key}); | |
| @override | |
| Widget build(BuildContext context) => Scaffold( | |
| appBar: AppBar(title: const Text('home')), | |
| body: const Center(child: FlutterLogo()), | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment