forked from Baseflow/flutter-geolocator
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.dart
95 lines (82 loc) · 2.38 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
import 'package:flutter/material.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, locationStream, distance }
class GeolocatorExampleApp extends StatefulWidget {
@override
State<GeolocatorExampleApp> createState() => BottomNavigationState();
}
class BottomNavigationState extends State<GeolocatorExampleApp> {
TabItem _currentItem = TabItem.singleLocation;
@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.singleLocation:
default:
return CurrentLocationWidget();
}
}
Widget _buildBottomNavigationBar() {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
_buildBottomNavigationBarItem(
Icons.location_on, TabItem.singleLocation),
_buildBottomNavigationBarItem(Icons.clear_all, TabItem.locationStream),
_buildBottomNavigationBarItem(Icons.redo, TabItem.distance),
],
onTap: _onSelectTab,
);
}
BottomNavigationBarItem _buildBottomNavigationBarItem(
IconData icon, TabItem tabItem) {
final String text = tabItem.toString().split('.').last;
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;
switch (index) {
case 1:
selectedTabItem = TabItem.locationStream;
break;
case 2:
selectedTabItem = TabItem.distance;
break;
default:
selectedTabItem = TabItem.singleLocation;
}
setState(() {
_currentItem = selectedTabItem;
});
}
}