Skip to content

Instantly share code, notes, and snippets.

@braj065
Created September 11, 2021 12:18
Show Gist options
  • Save braj065/e0a74129180414ddb9c26eb3f08ea0ee to your computer and use it in GitHub Desktop.
Save braj065/e0a74129180414ddb9c26eb3f08ea0ee to your computer and use it in GitHub Desktop.
Beamer loses uri path on going back from one beamlocation to previous
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
const List<Map<String, dynamic>> books = [
{
'id': '1',
'title': 'Stranger in a Strange Land',
'author': 'Robert A. Heinlein',
'genres': ['Science fiction'],
},
{
'id': '2',
'title': 'Foundation',
'author': 'Isaac Asimov',
'genres': ['Science fiction', 'Political drama'],
},
{
'id': '3',
'title': 'Fahrenheit 451',
'author': 'Ray Bradbury',
'genres': ['Dystopian'],
},
];
///MATERIAL APP - Call 'BeamerIssueRecreateMyApp' inside runApp
class BeamerIssueRecreateMyApp extends StatelessWidget {
BeamerIssueRecreateMyApp({Key? key}) : super(key: key);
final routerDelegate = BeamerDelegate(
setBrowserTabTitle: false,
initialPath: '/home',
listener: (BeamState state, BeamLocation<BeamState> loc) {
print('STATE DETAILS MATeriAL-- ${loc.state.uri}');
},
locationBuilder: BeamerLocationBuilder(
beamLocations: [
HomeLocation(),
BooksLocation(),
],
),
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routeInformationParser: BeamerParser(),
routerDelegate: routerDelegate,
backButtonDispatcher:
BeamerBackButtonDispatcher(delegate: routerDelegate),
);
}
}
//LOCATIONS
class HomeLocation extends BeamLocation<BeamState> {
@override
List<BeamPage> buildPages(BuildContext context, BeamState state) {
return [
BeamPage(
key: ValueKey('home'),
title: 'Home',
name: 'Home',
child: HomeScreen(),
),
];
}
@override
List get pathBlueprints => ['/home'];
}
class BooksLocation extends BeamLocation<BeamState> {
@override
List<BeamPage> buildPages(BuildContext context, BeamState state) {
List<BeamPage> beamPages = [...HomeLocation().buildPages(context, state)];
if (state.uri.pathSegments.contains('books')) {
beamPages.add(
BeamPage(
key: ValueKey('books'),
title: 'Books',
name: 'books',
child: BooksScreen(
books: books,
),
),
);
}
return beamPages;
}
@override
List get pathBlueprints => ['/books'];
}
///SCREENS
class BooksScreen extends StatelessWidget {
final List<Map<String, dynamic>> books;
const BooksScreen({
Key? key,
required this.books,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Books'),
),
body: ListView(
children: books
.map(
(book) => ListTile(
title: Text(
book['title']!,
),
subtitle: Text(book['author']!),
),
)
.toList(),
),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Screen'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
context.beamToNamed('/books');
},
child: Text('Beam to books location'),
),
SizedBox(height: 10),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment