-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.dart
150 lines (133 loc) · 4.81 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ignore_for_file: public_member_api_docs
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:google_navigation_flutter/google_navigation_flutter.dart';
import 'package:permission_handler/permission_handler.dart';
import 'pages/circles.dart';
import 'pages/pages.dart';
import 'widgets/widgets.dart';
/// The list of pages to show in the Google Maps Navigation demo.
final List<ExamplePage> _allPages = <ExamplePage>[
const NavigationPage(),
const BasicMapPage(),
const CameraPage(),
const MarkersPage(),
const PolygonsPage(),
const PolylinesPage(),
const CirclesPage(),
const TurnByTurnPage(),
const WidgetInitializationPage(),
const NavigationWithoutMapPage(),
const MultipleMapViewsPage(),
];
/// The main Google Maps Navigation demo screen.
class NavigationDemo extends StatelessWidget {
const NavigationDemo({super.key});
@override
Widget build(BuildContext context) {
return const NavigationBody();
}
}
class NavigationBody extends StatefulWidget {
const NavigationBody({super.key});
@override
State<StatefulWidget> createState() => _NavigationDemoState();
}
class _NavigationDemoState extends State<NavigationBody> {
_NavigationDemoState();
bool _locationPermitted = false;
bool _notificationsPermitted = false;
String _navSDKVersion = '';
@override
void initState() {
_requestPermissions();
super.initState();
unawaited(_checkSDKVersion());
}
Future<void> _checkSDKVersion() async {
// Get the Navigation SDK version.
_navSDKVersion = await GoogleMapsNavigator.getNavSDKVersion();
}
Future<void> _pushPage(BuildContext context, ExamplePage page) async {
await Navigator.of(context)
.push(MaterialPageRoute<void>(builder: (_) => page));
}
/// Request permission for accessing the device's location and notifications.
///
/// Android: Fine and Coarse Location
/// iOS: CoreLocation (Always and WhenInUse), Notification
Future<void> _requestPermissions() async {
final PermissionStatus locationPermission =
await Permission.location.request();
PermissionStatus notificationPermission = PermissionStatus.denied;
if (Platform.isIOS) {
notificationPermission = await Permission.notification.request();
}
setState(() {
_locationPermitted = locationPermission == PermissionStatus.granted;
_notificationsPermitted =
notificationPermission == PermissionStatus.granted;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Google Navigation Flutter examples')),
body: SafeArea(
top: false,
minimum: const EdgeInsets.all(8.0),
child: ListView.builder(
itemCount: _allPages.length + 1,
itemBuilder: (_, int index) {
if (index == 0) {
return Card(
child: Container(
padding: const EdgeInsets.symmetric(vertical: 4.0),
alignment: Alignment.center,
child: Column(
children: <Widget>[
Text(Platform.isIOS
? 'Location ${_locationPermitted ? 'granted' : 'denied'} • Notifications ${_notificationsPermitted ? 'granted' : 'denied'}'
: 'Location ${_locationPermitted ? 'granted' : 'denied'} '),
Text('Navigation SDK version: $_navSDKVersion'),
],
),
),
);
}
return ListTile(
leading: _allPages[index - 1].leading,
title: Text(_allPages[index - 1].title),
onTap: () => _pushPage(context, _allPages[index - 1]),
);
},
)),
);
}
}
void main() {
final ElevatedButtonThemeData exampleButtonDefaultTheme =
ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(minimumSize: const Size(160, 36)));
runApp(MaterialApp(
home: const NavigationDemo(),
theme: ThemeData.light()
.copyWith(elevatedButtonTheme: exampleButtonDefaultTheme),
darkTheme: ThemeData.dark()
.copyWith(elevatedButtonTheme: exampleButtonDefaultTheme),
));
}