Skip to content

Commit

Permalink
Routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
xclud committed Feb 8, 2024
1 parent defd8a3 commit d643785
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
46 changes: 15 additions & 31 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import 'package:example/pages/custom_tile_page.dart';
import 'package:example/pages/shapes_page.dart';
import 'package:example/pages/twilight_page.dart';
import 'package:example/pages/interactive_page.dart';
import 'package:example/pages/markers_page.dart';
import 'package:example/pages/metro_lines_page.dart';
import 'package:example/pages/raster_map_page.dart';
import 'package:example/pages/vector_map_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:go_router/go_router.dart';
import 'routes.dart';

void main() => runApp(const MapApp());

Expand All @@ -16,19 +9,14 @@ class MapApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return MaterialApp(
return MaterialApp.router(
routerConfig: router,
title: 'Map Examples',
supportedLocales: const [Locale('en')],
localizationsDelegates: const [
GlobalWidgetsLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
],
theme: ThemeData(
colorSchemeSeed: Colors.purple,
useMaterial3: true,
brightness: Brightness.dark,
),
home: const HomePage(),
);
}
}
Expand All @@ -53,48 +41,48 @@ class _HomePageState extends State<HomePage> {
body: ListView(
children: [
ListTile(
title: const Text('Raster Map'),
title: const Text('Basic Map'),
subtitle: const Text('Raster tiles from Google, OSM and etc.'),
trailing: const Icon(Icons.chevron_right_sharp),
onTap: () => _push(const RasterMapPage()),
onTap: () => _go('basic'),
),
ListTile(
title: const Text('Vector Map'),
subtitle: const Text('OSM light-themed vector maps.'),
trailing: const Icon(Icons.chevron_right_sharp),
onTap: () => _push(const VectorMapPage()),
onTap: () => _showNotImplemented,
enabled: false,
),
ListTile(
title: const Text('Markers'),
subtitle: const Text(
'Drop multiple fixed and centered markers on the map.'),
trailing: const Icon(Icons.chevron_right_sharp),
onTap: () => _push(const MarkersPage()),
onTap: () => _go('markers'),
),
ListTile(
title: const Text('Interactive'),
subtitle: const Text('Say where on the earth user has clicked.'),
trailing: const Icon(Icons.chevron_right_sharp),
onTap: () => _push(const InteractiveMapPage()),
onTap: () => _go('interactive'),
),
ListTile(
title: const Text('Shapes'),
subtitle: const Text('Display Polylines on the map.'),
trailing: const Icon(Icons.chevron_right_sharp),
onTap: () => _push(const ShapesPage()),
onTap: () => _go('shapes'),
),
ListTile(
title: const Text('Custom Tiles'),
subtitle: const Text('Use any Widget as map tiles.'),
trailing: const Icon(Icons.chevron_right_sharp),
onTap: () => _push(const CustomTilePage()),
onTap: () => _go('custom-tiles'),
),
ListTile(
title: const Text('Metro Lines (Work in Progress)'),
subtitle: const Text('Draw polyline overlays (Tehran Metro).'),
trailing: const Icon(Icons.chevron_right_sharp),
onTap: () => _push(const MetroLinesPage()),
onTap: () => _go('tehran-metro'),
),
ListTile(
title: const Text('Custom Projection'),
Expand All @@ -108,7 +96,7 @@ class _HomePageState extends State<HomePage> {
title: const Text('Twilight'),
subtitle: const Text('Day and night map, sun and moon position.'),
trailing: const Icon(Icons.chevron_right_sharp),
onTap: () => _push(const TwilightPage()),
onTap: () => _go('twilight'),
),
],
),
Expand All @@ -121,11 +109,7 @@ class _HomePageState extends State<HomePage> {
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}

void _push(Widget page) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => page,
),
);
void _go(String name) {
GoRouter.of(context).goNamed(name);
}
}
2 changes: 1 addition & 1 deletion example/lib/pages/raster_map_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class RasterMapPageState extends State<RasterMapPage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Raster Map'),
title: const Text('Basic Map'),
actions: [
IconButton(
tooltip: 'Toggle Dark Mode',
Expand Down
56 changes: 56 additions & 0 deletions example/lib/routes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:example/main.dart';
import 'package:example/pages/custom_tile_page.dart';
import 'package:example/pages/interactive_page.dart';
import 'package:example/pages/metro_lines_page.dart';
import 'package:example/pages/raster_map_page.dart';
import 'package:example/pages/shapes_page.dart';
import 'package:example/pages/twilight_page.dart';
import 'package:go_router/go_router.dart';

/// This handles '/' and '/details'.
final router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
path: '/',
builder: (_, rs) => const HomePage(),
routes: [
GoRoute(
path: 'custom-tiles',
name: 'custom-tiles',
builder: (_, __) => const CustomTilePage(),
),
GoRoute(
path: 'interactive',
name: 'interactive',
builder: (_, __) => const InteractiveMapPage(),
),
GoRoute(
path: 'markers',
name: 'markers',
builder: (_, __) => const InteractiveMapPage(),
),
GoRoute(
path: 'tehran-metro',
name: 'tehran-metro',
builder: (_, __) => const MetroLinesPage(),
),
GoRoute(
path: 'basic',
name: 'basic',
builder: (_, __) => const RasterMapPage(),
),
GoRoute(
path: 'shapes',
name: 'shapes',
builder: (_, __) => const ShapesPage(),
),
GoRoute(
path: 'twilight',
name: 'twilight',
builder: (_, __) => const TwilightPage(),
),
],
),
],
);
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:
flutter_localizations:
sdk: flutter
platform: ^3.1.0
go_router: any
map:
path: ../

Expand Down

0 comments on commit d643785

Please sign in to comment.