Created
March 20, 2022 04:01
-
-
Save Kurogoma4D/1eb73ea2427544c080f70f4db2f960ac 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() { | |
GoRouter.setUrlPathStrategy(UrlPathStrategy.path); | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
MyApp({Key? key}) : super(key: key); | |
final _router = GoRouter(routes: [ | |
GoRoute( | |
path: '/', | |
builder: (_, state) => BranchRoutePage( | |
route: state.location, | |
children: const ['/todo'], | |
), | |
routes: [ | |
GoRoute( | |
path: 'todo', | |
builder: (_, state) => BranchRoutePage( | |
route: state.location, | |
children: const ['/todo/detail/321', '/todo/create'], | |
), | |
routes: [ | |
GoRoute( | |
path: 'create', | |
builder: (_, state) => LeafRoutePage(route: state.location), | |
), | |
GoRoute( | |
path: 'detail/:id', | |
builder: (_, state) => BranchRoutePage( | |
route: state.location, | |
children: const ['/todo/detail/321/edit', '/todo/create'], | |
), | |
routes: [ | |
GoRoute( | |
path: 'edit', | |
builder: (_, state) => LeafRoutePage( | |
route: state.location, | |
), | |
), | |
], | |
), | |
], | |
), | |
], | |
), | |
]); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp.router( | |
title: 'Flutter Demo', | |
theme: ThemeData(primarySwatch: Colors.blue), | |
routerDelegate: _router.routerDelegate, | |
routeInformationParser: _router.routeInformationParser, | |
); | |
} | |
} | |
class BranchRoutePage extends StatelessWidget { | |
const BranchRoutePage({ | |
Key? key, | |
this.route = '', | |
required this.children, | |
}) : super(key: key); | |
final String route; | |
final Iterable<String> children; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Branch page')), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text(route), | |
const SizedBox(height: 24), | |
for (final child in children) ...[ | |
ElevatedButton( | |
onPressed: () => context.go(child), | |
child: Text(child), | |
), | |
const SizedBox(height: 16), | |
] | |
], | |
), | |
), | |
); | |
} | |
} | |
class LeafRoutePage extends StatelessWidget { | |
const LeafRoutePage({Key? key, this.route = ''}) : super(key: key); | |
final String route; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Leaf page')), | |
body: Center(child: Text(route)), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment