Created
November 20, 2024 14:32
-
-
Save rocboronat/72e8664b6408534f2448cc8a34199ee6 to your computer and use it in GitHub Desktop.
GoRouter issue with deep links
This file contains 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/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:go_router/go_router.dart'; | |
// 1. The main file | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp.router(routerConfig: goRouter); | |
} | |
} | |
// 2. The GoRouter configuration | |
final goRouter = GoRouter( | |
debugLogDiagnostics: kDebugMode, | |
routes: [ | |
GoRoute( | |
path: '/', | |
builder: (context, state) => const SplashScreen(), | |
), | |
GoRoute( | |
path: '/home', | |
builder: (context, state) => const HomeScreen(), | |
), | |
GoRoute( | |
path: '/deeplink', | |
builder: (context, state) => const DeepLinkScreen(), | |
), | |
GoRoute( | |
path: '/child', | |
builder: (context, state) => const ChildScreen(), | |
redirect: (context, GoRouterState state) { | |
final valueAddedDuringDeepLink = state.uri.queryParameters['value']; | |
if (valueAddedDuringDeepLink == '42') { | |
return null; | |
} else { | |
return '/deeplink'; | |
} | |
}, | |
), | |
], | |
); | |
// 3. The 4 screens | |
// 3.1 Splash screen | |
class SplashScreen extends StatefulWidget { | |
const SplashScreen({super.key}); | |
@override | |
State<SplashScreen> createState() => _SplashScreenState(); | |
} | |
class _SplashScreenState extends State<SplashScreen> { | |
@override | |
void initState() { | |
super.initState(); | |
Future.delayed(const Duration(seconds: 1), () { | |
context.go('/home'); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Splash screen')), | |
body: const Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Text('Loading...'), | |
CircularProgressIndicator(), | |
], | |
)), | |
); | |
} | |
} | |
// 3.2 Home screen | |
class HomeScreen extends StatelessWidget { | |
const HomeScreen({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Home screen')), | |
body: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
const Text('Home'), | |
TextButton( | |
onPressed: () => context.push('/child'), | |
child: const Text('Go to child screen'), | |
), | |
], | |
)), | |
); | |
} | |
} | |
// 3.3 Deep link screen | |
class DeepLinkScreen extends StatefulWidget { | |
const DeepLinkScreen({super.key}); | |
@override | |
State<DeepLinkScreen> createState() => _DeepLinkScreenState(); | |
} | |
class _DeepLinkScreenState extends State<DeepLinkScreen> { | |
@override | |
void initState() { | |
super.initState(); | |
Future.delayed(const Duration(seconds: 1), () { | |
context.pushReplacement('/child?value=42'); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Deep link screen')), | |
body: const Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Text('Redirecting to the child screen...'), | |
CircularProgressIndicator(), | |
], | |
))); | |
} | |
} | |
// 3.4 Child screen | |
class ChildScreen extends StatelessWidget { | |
const ChildScreen({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Child screen')), | |
body: const Center(child: Text('Child')), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment