Created
March 17, 2022 06:00
-
-
Save bizz84/afb7e73b40caf56b81298116490bddde to your computer and use it in GitHub Desktop.
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'; | |
void main() { | |
WidgetsFlutterBinding.ensureInitialized(); | |
GoRouter.setUrlPathStrategy(UrlPathStrategy.path); | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
final goRouter = GoRouter( | |
debugLogDiagnostics: true, | |
initialLocation: '/', | |
routes: [ | |
GoRoute( | |
path: '/', | |
builder: (context, state) => const HomeScreen(), | |
routes: [ | |
GoRoute( | |
path: 'detail', | |
builder: (context, state) => const DetailScreen(), | |
), | |
GoRoute( | |
path: 'modal', | |
pageBuilder: (context, state) => const MaterialPage( | |
fullscreenDialog: true, | |
child: ModalScreen(), | |
), | |
) | |
], | |
), | |
], | |
); | |
return MaterialApp.router( | |
routerDelegate: goRouter.routerDelegate, | |
routeInformationParser: goRouter.routeInformationParser, | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.amber, | |
), | |
); | |
} | |
} | |
class HomeScreen extends StatelessWidget { | |
const HomeScreen({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Home Page'), | |
backgroundColor: Colors.red, | |
), | |
backgroundColor: Colors.red, | |
body: Padding( | |
padding: const EdgeInsets.all(32.0), | |
child: Center( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
ElevatedButton( | |
onPressed: () => context.go('/detail'), | |
child: const CenteredText('go /detail'), | |
), | |
const SizedBox( | |
height: 32, | |
), | |
ElevatedButton( | |
onPressed: () => context.push('/detail'), | |
child: const CenteredText('push /detail'), | |
), | |
const SizedBox( | |
height: 32, | |
), | |
ElevatedButton( | |
onPressed: () => context.go('/modal'), | |
child: const CenteredText('go /modal'), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class DetailScreen extends StatelessWidget { | |
const DetailScreen({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Detail Page'), | |
backgroundColor: Colors.green, | |
), | |
backgroundColor: Colors.green, | |
body: Padding( | |
padding: const EdgeInsets.all(32.0), | |
child: Center( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
ElevatedButton( | |
onPressed: () => context.go('/modal'), | |
child: const CenteredText('go /modal'), | |
), | |
const SizedBox( | |
height: 32, | |
), | |
ElevatedButton( | |
onPressed: () => context.push('/modal'), | |
child: const CenteredText('push /modal'), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class ModalScreen extends StatelessWidget { | |
const ModalScreen({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Modal Page'), | |
backgroundColor: Colors.blue, | |
), | |
backgroundColor: Colors.blue, | |
); | |
} | |
} | |
class CenteredText extends StatelessWidget { | |
const CenteredText(this.text, {Key? key}) : super(key: key); | |
final String text; | |
@override | |
Widget build(BuildContext context) { | |
return Text( | |
text, | |
style: const TextStyle(fontSize: 30), | |
textAlign: TextAlign.center, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment