Created
April 10, 2023 23:47
-
-
Save justinmc/96e3e7a6d69c190c902f625d755cc3e0 to your computer and use it in GitHub Desktop.
A simple GoRouter example to use to test predictive back. It doesn't work out of the box. Always exits the app.
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
// Copyright 2014 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:go_router/go_router.dart'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(_MyApp()); | |
class _MyApp extends StatelessWidget { | |
final GoRouter router = GoRouter( | |
routes: <RouteBase>[ | |
GoRoute( | |
path: '/', | |
builder: (BuildContext context, GoRouterState state) => _LinksPage( | |
title: 'Home page', | |
backgroundColor: Colors.indigo, | |
buttons: <Widget>[ | |
TextButton( | |
onPressed: () { | |
context.push('/one'); | |
}, | |
child: const Text('Go to one'), | |
), | |
], | |
), | |
), | |
GoRoute( | |
path: '/one', | |
builder: (BuildContext context, GoRouterState state) => _LinksPage( | |
title: 'Page one', | |
backgroundColor: Colors.indigo.withRed(255), | |
buttons: <Widget>[ | |
TextButton( | |
onPressed: () { | |
context.push('/one/two'); | |
}, | |
child: const Text('Go to one/two'), | |
), | |
], | |
), | |
), | |
GoRoute( | |
path: '/one/two', | |
builder: (BuildContext context, GoRouterState state) => _LinksPage( | |
title: 'Page one/two', | |
backgroundColor: Colors.indigo.withBlue(255), | |
), | |
), | |
], | |
); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp.router( | |
routerConfig: router, | |
); | |
} | |
} | |
class _HomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Nested Navigators Example'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text('Home Page'), | |
const Text('A system back gesture here will exit the app.'), | |
const SizedBox(height: 20.0), | |
ListTile( | |
title: const Text('Nested Navigator route'), | |
subtitle: const Text('This route has another Navigator widget in addition to the one inside MaterialApp above.'), | |
onTap: () { | |
context.push('/nested_navigators'); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class _LinksPage extends StatelessWidget { | |
const _LinksPage ({ | |
required this.backgroundColor, | |
this.buttons = const <Widget>[], | |
this.onBack, | |
required this.title, | |
}); | |
final Color backgroundColor; | |
final List<Widget> buttons; | |
final VoidCallback? onBack; | |
final String title; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: backgroundColor, | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text(title), | |
...buttons, | |
if (GoRouter.of(context).canPop()) | |
TextButton( | |
onPressed: onBack ?? () { | |
context.pop(); | |
}, | |
child: const Text('Go back'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment