forked from Baseflow/flutter-geolocator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.dart
More file actions
136 lines (121 loc) · 3.32 KB
/
main.dart
File metadata and controls
136 lines (121 loc) · 3.32 KB
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
import 'dart:io';
import 'package:flutter/material.dart';
import 'pages/lookup_address_widget.dart';
import 'pages/calculate_distance_widget.dart';
import 'pages/current_location_widget.dart';
import 'pages/location_stream_widget.dart';
void main() => runApp(GeolocatorExampleApp());
enum TabItem {
singleLocation,
singleFusedLocation,
locationStream,
distance,
geocode
}
class GeolocatorExampleApp extends StatefulWidget {
@override
State<GeolocatorExampleApp> createState() => BottomNavigationState();
}
class BottomNavigationState extends State<GeolocatorExampleApp> {
TabItem _currentItem = TabItem.singleLocation;
final List<TabItem> _bottomTabs = [
TabItem.singleLocation,
if (Platform.isAndroid) TabItem.singleFusedLocation,
TabItem.locationStream,
TabItem.distance,
TabItem.geocode,
];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Geolocator Example App'),
),
body: _buildBody(),
bottomNavigationBar: _buildBottomNavigationBar(),
),
);
}
Widget _buildBody() {
switch (_currentItem) {
case TabItem.locationStream:
return LocationStreamWidget();
case TabItem.distance:
return CalculateDistanceWidget();
case TabItem.singleFusedLocation:
return CurrentLocationWidget(androidFusedLocation: true);
case TabItem.geocode:
return LookupAddressWidget();
case TabItem.singleLocation:
default:
return CurrentLocationWidget(androidFusedLocation: false);
}
}
Widget _buildBottomNavigationBar() {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: _bottomTabs
.map((tabItem) =>
_buildBottomNavigationBarItem(_icon(tabItem), tabItem))
.toList(),
onTap: _onSelectTab,
);
}
BottomNavigationBarItem _buildBottomNavigationBarItem(
IconData icon, TabItem tabItem) {
final String text = _title(tabItem);
final Color color =
_currentItem == tabItem ? Theme.of(context).primaryColor : Colors.grey;
return BottomNavigationBarItem(
icon: Icon(
icon,
color: color,
),
title: Text(
text,
style: TextStyle(
color: color,
),
),
);
}
void _onSelectTab(int index) {
TabItem selectedTabItem = _bottomTabs[index];
setState(() {
_currentItem = selectedTabItem;
});
}
String _title(TabItem item) {
switch (item) {
case TabItem.singleLocation:
return 'Single';
case TabItem.singleFusedLocation:
return 'Single (Fused)';
case TabItem.locationStream:
return 'Stream';
case TabItem.distance:
return 'Distance';
case TabItem.geocode:
return 'Geocode';
default:
throw 'Unknown: $item';
}
}
IconData _icon(TabItem item) {
switch (item) {
case TabItem.singleLocation:
return Icons.location_on;
case TabItem.singleFusedLocation:
return Icons.location_on;
case TabItem.locationStream:
return Icons.clear_all;
case TabItem.distance:
return Icons.redo;
case TabItem.geocode:
return Icons.compare_arrows;
default:
throw 'Unknown: $item';
}
}
}