| // Copyright 2013 The Flutter Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import 'package:flutter/material.dart'; |
| import 'package:go_router/go_router.dart'; |
| import 'package:logging/logging.dart'; |
| |
| void main() => runApp(App()); |
| |
| class App extends StatelessWidget { |
| App({Key? key}) : super(key: key); |
| |
| static const title = 'GoRouter Example: Navigator Observer'; |
| |
| @override |
| Widget build(BuildContext context) => MaterialApp.router( |
| routeInformationParser: _router.routeInformationParser, |
| routerDelegate: _router.routerDelegate, |
| title: title, |
| ); |
| |
| final _router = GoRouter( |
| observers: [MyNavObserver()], |
| routes: [ |
| GoRoute( |
| // if there's no name, path will be used as name for observers |
| path: '/', |
| builder: (context, state) => const Page1Screen(), |
| routes: [ |
| GoRoute( |
| name: 'page2', |
| path: 'page2/:p1', |
| builder: (context, state) => const Page2Screen(), |
| routes: [ |
| GoRoute( |
| name: 'page3', |
| path: 'page3', |
| builder: (context, state) => const Page3Screen(), |
| ), |
| ], |
| ), |
| ], |
| ), |
| ], |
| ); |
| } |
| |
| class MyNavObserver extends NavigatorObserver { |
| MyNavObserver() { |
| log.onRecord.listen((e) => debugPrint('$e')); |
| } |
| |
| final log = Logger('MyNavObserver'); |
| |
| @override |
| void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) => |
| log.info('didPush: ${route.str}, previousRoute= ${previousRoute?.str}'); |
| |
| @override |
| void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) => |
| log.info('didPop: ${route.str}, previousRoute= ${previousRoute?.str}'); |
| |
| @override |
| void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) => |
| log.info('didRemove: ${route.str}, previousRoute= ${previousRoute?.str}'); |
| |
| @override |
| void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) => |
| log.info('didReplace: new= ${newRoute?.str}, old= ${oldRoute?.str}'); |
| |
| @override |
| void didStartUserGesture( |
| Route<dynamic> route, |
| Route<dynamic>? previousRoute, |
| ) => |
| log.info('didStartUserGesture: ${route.str}, ' |
| 'previousRoute= ${previousRoute?.str}'); |
| |
| @override |
| void didStopUserGesture() => log.info('didStopUserGesture'); |
| } |
| |
| extension on Route<dynamic> { |
| String get str => 'route(${settings.name}: ${settings.arguments})'; |
| } |
| |
| class Page1Screen extends StatelessWidget { |
| const Page1Screen({Key? key}) : super(key: key); |
| |
| @override |
| Widget build(BuildContext context) => Scaffold( |
| appBar: AppBar(title: const Text(App.title)), |
| body: Center( |
| child: Column( |
| mainAxisAlignment: MainAxisAlignment.center, |
| children: [ |
| ElevatedButton( |
| onPressed: () => context.goNamed( |
| 'page2', |
| params: {'p1': 'pv1'}, |
| queryParams: {'q1': 'qv1'}, |
| ), |
| child: const Text('Go to page 2'), |
| ), |
| ], |
| ), |
| ), |
| ); |
| } |
| |
| class Page2Screen extends StatelessWidget { |
| const Page2Screen({Key? key}) : super(key: key); |
| |
| @override |
| Widget build(BuildContext context) => Scaffold( |
| appBar: AppBar(title: const Text(App.title)), |
| body: Center( |
| child: Column( |
| mainAxisAlignment: MainAxisAlignment.center, |
| children: [ |
| ElevatedButton( |
| onPressed: () => context.goNamed( |
| 'page3', |
| params: {'p1': 'pv2'}, |
| ), |
| child: const Text('Go to page 3'), |
| ), |
| ], |
| ), |
| ), |
| ); |
| } |
| |
| class Page3Screen extends StatelessWidget { |
| const Page3Screen({Key? key}) : super(key: key); |
| |
| @override |
| Widget build(BuildContext context) => Scaffold( |
| appBar: AppBar(title: const Text(App.title)), |
| body: Center( |
| child: Column( |
| mainAxisAlignment: MainAxisAlignment.center, |
| children: [ |
| ElevatedButton( |
| onPressed: () => context.go('/'), |
| child: const Text('Go to home page'), |
| ), |
| ], |
| ), |
| ), |
| ); |
| } |