-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.dart
148 lines (131 loc) · 4.89 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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:poly_geofence_service/poly_geofence_service.dart';
void main() => runApp(const ExampleApp());
class ExampleApp extends StatefulWidget {
const ExampleApp({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
final _streamController = StreamController<PolyGeofence>();
// Create a [PolyGeofenceService] instance and set options.
final _polyGeofenceService = PolyGeofenceService.instance.setup(
interval: 5000,
accuracy: 100,
loiteringDelayMs: 60000,
statusChangeDelayMs: 10000,
allowMockLocations: false,
printDevLog: false);
// Create a [PolyGeofence] list.
final _polyGeofenceList = <PolyGeofence>[
PolyGeofence(
id: 'Yongdusan_Park',
data: {
'address': '37-55 Yongdusan-gil, Gwangbokdong 2(i)-ga, Jung-gu, Busan',
'about': 'Mountain park known for its 129m-high observation tower, statues & stone monuments.',
},
polygon: <LatLng>[
const LatLng(35.101727, 129.031665),
const LatLng(35.101815, 129.033458),
const LatLng(35.100032, 129.034055),
const LatLng(35.099324, 129.033811),
const LatLng(35.099906, 129.031927),
const LatLng(35.101080, 129.031534),
],
),
];
// This function is to be called when the geofence status is changed.
Future<void> _onPolyGeofenceStatusChanged(
PolyGeofence polyGeofence,
PolyGeofenceStatus polyGeofenceStatus,
Location location) async {
print('polyGeofence: ${polyGeofence.toJson()}');
print('polyGeofenceStatus: ${polyGeofenceStatus.toString()}');
_streamController.sink.add(polyGeofence);
}
// This function is to be called when the location has changed.
void _onLocationChanged(Location location) {
print('location: ${location.toJson()}');
}
// This function is to be called when a location services status change occurs
// since the service was started.
void _onLocationServicesStatusChanged(bool status) {
print('isLocationServicesEnabled: $status');
}
// This function is used to handle errors that occur in the service.
void _onError(error) {
final errorCode = getErrorCodesFromError(error);
if (errorCode == null) {
print('Undefined error: $error');
return;
}
print('ErrorCode: $errorCode');
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_polyGeofenceService.addPolyGeofenceStatusChangeListener(_onPolyGeofenceStatusChanged);
_polyGeofenceService.addLocationChangeListener(_onLocationChanged);
_polyGeofenceService.addLocationServicesStatusChangeListener(_onLocationServicesStatusChanged);
_polyGeofenceService.addStreamErrorListener(_onError);
_polyGeofenceService.start(_polyGeofenceList).catchError(_onError);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
// A widget used when you want to start a foreground task when trying to minimize or close the app.
// Declare on top of the [Scaffold] widget.
home: WillStartForegroundTask(
onWillStart: () async {
// You can add a foreground task start condition.
return _polyGeofenceService.isRunningService;
},
androidNotificationOptions: AndroidNotificationOptions(
channelId: 'geofence_service_notification_channel',
channelName: 'Geofence Service Notification',
channelDescription: 'This notification appears when the geofence service is running in the background.',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
isSticky: false,
),
iosNotificationOptions: const IOSNotificationOptions(),
foregroundTaskOptions: const ForegroundTaskOptions(),
notificationTitle: 'Geofence Service is running',
notificationText: 'Tap to return to the app',
child: Scaffold(
appBar: AppBar(
title: const Text('Poly Geofence Service'),
centerTitle: true,
),
body: _buildContentView(),
),
),
);
}
@override
void dispose() {
_streamController.close();
super.dispose();
}
Widget _buildContentView() {
return StreamBuilder<PolyGeofence>(
stream: _streamController.stream,
builder: (context, snapshot) {
final updatedDateTime = DateTime.now();
final content = snapshot.data?.toJson().toString() ?? '';
return ListView(
physics: const BouncingScrollPhysics(),
padding: const EdgeInsets.all(8.0),
children: [
Text('•\t\tPolyGeofence (updated: $updatedDateTime)'),
const SizedBox(height: 10.0),
Text(content),
],
);
},
);
}
}