beamer 0.12.1 beamer: ^0.12.1 copied to clipboard
A routing package that lets you navigate through guarded page stacks and URLs using the Router and Navigator's Pages API, aka "Navigator 2.0".
import 'package:flutter/material.dart';
import 'package:beamer/beamer.dart';
// DATA
const List<Map<String, String>> books = [
{
'id': '1',
'title': 'Stranger in a Strange Land',
'author': 'Robert A. Heinlein',
},
{
'id': '2',
'title': 'Foundation',
'author': 'Isaac Asimov',
},
{
'id': '3',
'title': 'Fahrenheit 451',
'author': 'Ray Bradbury',
},
];
// SCREENS
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Screen'),
),
body: Center(
child: ElevatedButton(
//onPressed: () => context.beamToNamed('/books'),
onPressed: () => context.currentBeamLocation.update(
(state) => BeamState(
pathBlueprintSegments: ['books'],
),
),
child: Text('See books'),
),
),
);
}
}
class BooksScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final titleQuery =
context.currentBeamLocation.state.queryParameters['title'] ?? '';
return Scaffold(
appBar: AppBar(
title: Text('Books'),
),
body: ListView(
children: books
.where((book) =>
book['title'].toLowerCase().contains(titleQuery.toLowerCase()))
.map(
(book) => ListTile(
title: Text(book['title']),
subtitle: Text(book['author']),
//onTap: () => context.beamToNamed('/books/${book['id']}'),
onTap: () => context.currentBeamLocation.update(
(state) => state.copyWith(
pathBlueprintSegments: ['books', ':bookId'],
pathParameters: {'bookId': book['id']},
),
),
),
)
.toList(),
),
);
}
}
class BookDetailsScreen extends StatelessWidget {
BookDetailsScreen({
this.bookId,
}) : book = books.firstWhere((book) => book['id'] == bookId);
final String bookId;
final Map<String, String> book;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(book['title']),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Author: ${book['author']}'),
),
);
}
}
// LOCATIONS
class BooksLocation extends BeamLocation {
BooksLocation(BeamState state) : super(state);
@override
List<String> get pathBlueprints => ['/books/:bookId'];
@override
List<BeamPage> pagesBuilder(BuildContext context, BeamState state) => [
BeamPage(
key: ValueKey('home'),
child: HomeScreen(),
),
if (state.uri.pathSegments.contains('books'))
BeamPage(
key: ValueKey('books-${state.queryParameters['title'] ?? ''}'),
child: BooksScreen(),
),
if (state.pathParameters.containsKey('bookId'))
BeamPage(
key: ValueKey('book-${state.pathParameters['bookId']}'),
child: BookDetailsScreen(
bookId: state.pathParameters['bookId'],
),
),
];
}
// APP
class MyApp extends StatelessWidget {
final routerDelegate = BeamerRouterDelegate(
locationBuilder: (state) => BooksLocation(state),
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerDelegate: routerDelegate,
routeInformationParser: BeamerRouteInformationParser(),
backButtonDispatcher:
BeamerBackButtonDispatcher(delegate: routerDelegate),
);
}
}
void main() {
runApp(MyApp());
}