|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:go_router/go_router.dart'; |
| 7 | + |
| 8 | +/// This sample app demonstrates how to use go relatively with GoRouter.go('./$path'). |
| 9 | +void main() => runApp(const MyApp()); |
| 10 | + |
| 11 | +/// The main app. |
| 12 | +class MyApp extends StatelessWidget { |
| 13 | + /// Constructs a [MyApp] |
| 14 | + const MyApp({super.key}); |
| 15 | + |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return MaterialApp.router( |
| 19 | + routerConfig: _router, |
| 20 | + ); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// The route configuration. |
| 25 | +final GoRouter _router = GoRouter( |
| 26 | + routes: <RouteBase>[ |
| 27 | + GoRoute( |
| 28 | + path: '/', |
| 29 | + builder: (BuildContext context, GoRouterState state) { |
| 30 | + return const HomeScreen(); |
| 31 | + }, |
| 32 | + routes: <RouteBase>[ |
| 33 | + GoRoute( |
| 34 | + path: 'details', |
| 35 | + builder: (BuildContext context, GoRouterState state) { |
| 36 | + return const DetailsScreen(); |
| 37 | + }, |
| 38 | + routes: <RouteBase>[ |
| 39 | + GoRoute( |
| 40 | + path: 'settings', |
| 41 | + builder: (BuildContext context, GoRouterState state) { |
| 42 | + return const SettingsScreen(); |
| 43 | + }, |
| 44 | + ), |
| 45 | + ], |
| 46 | + ), |
| 47 | + ], |
| 48 | + ), |
| 49 | + ], |
| 50 | +); |
| 51 | + |
| 52 | +/// The home screen |
| 53 | +class HomeScreen extends StatelessWidget { |
| 54 | + /// Constructs a [HomeScreen] |
| 55 | + const HomeScreen({super.key}); |
| 56 | + |
| 57 | + @override |
| 58 | + Widget build(BuildContext context) { |
| 59 | + return Scaffold( |
| 60 | + appBar: AppBar(title: const Text('Home Screen')), |
| 61 | + body: Center( |
| 62 | + child: Column( |
| 63 | + mainAxisAlignment: MainAxisAlignment.center, |
| 64 | + children: <Widget>[ |
| 65 | + ElevatedButton( |
| 66 | + onPressed: () => context.go('./details'), |
| 67 | + child: const Text('Go to the Details screen'), |
| 68 | + ), |
| 69 | + ], |
| 70 | + ), |
| 71 | + ), |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// The details screen |
| 77 | +class DetailsScreen extends StatelessWidget { |
| 78 | + /// Constructs a [DetailsScreen] |
| 79 | + const DetailsScreen({super.key}); |
| 80 | + |
| 81 | + @override |
| 82 | + Widget build(BuildContext context) { |
| 83 | + return Scaffold( |
| 84 | + appBar: AppBar(title: const Text('Details Screen')), |
| 85 | + body: Center( |
| 86 | + child: Column( |
| 87 | + children: <Widget>[ |
| 88 | + TextButton( |
| 89 | + onPressed: () { |
| 90 | + context.pop(); |
| 91 | + }, |
| 92 | + child: const Text('Go back'), |
| 93 | + ), |
| 94 | + TextButton( |
| 95 | + onPressed: () { |
| 96 | + context.go('./settings'); |
| 97 | + }, |
| 98 | + child: const Text('Go to the Settings screen'), |
| 99 | + ), |
| 100 | + ], |
| 101 | + ), |
| 102 | + ), |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// The settings screen |
| 108 | +class SettingsScreen extends StatelessWidget { |
| 109 | + /// Constructs a [SettingsScreen] |
| 110 | + const SettingsScreen({super.key}); |
| 111 | + |
| 112 | + @override |
| 113 | + Widget build(BuildContext context) { |
| 114 | + return Scaffold( |
| 115 | + appBar: AppBar(title: const Text('Settings Screen')), |
| 116 | + body: Column( |
| 117 | + children: <Widget>[ |
| 118 | + TextButton( |
| 119 | + onPressed: () { |
| 120 | + context.pop(); |
| 121 | + }, |
| 122 | + child: const Text('Go back'), |
| 123 | + ), |
| 124 | + ], |
| 125 | + ), |
| 126 | + ); |
| 127 | + } |
| 128 | +} |
0 commit comments