-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathgeofence_view.dart
More file actions
246 lines (235 loc) · 9.81 KB
/
geofence_view.dart
File metadata and controls
246 lines (235 loc) · 9.81 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import 'package:flutter/material.dart';
import 'package:latlong2/latlong.dart';
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart'
as bg;
import 'package:flutter_background_geolocation_example/advanced/util/dialog.dart'
as util;
class GeofenceView extends StatefulWidget {
final LatLng? center;
final List<LatLng>? vertices;
GeofenceView({this.center, this.vertices});
@override
State createState() =>
_GeofenceViewState(center: this.center, vertices: this.vertices);
}
class _GeofenceViewState extends State<GeofenceView> {
LatLng? center;
List<LatLng>? vertices;
String _identifier = "";
double _radius = 200.0;
bool _notifyOnEntry = true;
bool _notifyOnExit = true;
bool _notifyOnDwell = false;
int _loiteringDelay = 10000;
_GeofenceViewState({this.center, this.vertices});
void _onClickClose() {
bg.BackgroundGeolocation.playSound(util.Dialog.getSoundId("CLOSE"));
//bg.BackgroundGeolocation.playSound(util.Dialog.getSoundId("CLOSE"));
Navigator.of(context).pop();
}
void _onClickAdd() {
if (center != null) {
// Circular Geofence.
bg.BackgroundGeolocation.addGeofence(bg.Geofence(
identifier: _identifier,
radius: _radius,
latitude: center?.latitude,
longitude: center?.longitude,
notifyOnEntry: _notifyOnEntry,
notifyOnExit: _notifyOnExit,
notifyOnDwell: _notifyOnDwell,
loiteringDelay: _loiteringDelay,
extras: {
'radius': _radius,
'vertices': [],
'center': {
'latitude': center?.latitude,
'longitude': center?.longitude
}
} // meta-data for tracker.transistorsoft.com
)).then((bool success) {
bg.BackgroundGeolocation.playSound(
util.Dialog.getSoundId('ADD_GEOFENCE'));
}).catchError((error) {
print('[addGeofence] ERROR: $error');
});
} else if (this.vertices != null) {
// Polygon Geofence
var vertices = this.vertices?.map((LatLng ll) {
return [ll.latitude, ll.longitude];
}).toList();
// A Polygon geofence has no center point (latitude/longitude) or radius.
// Those weill be calculated based upon the polygon using native code.
bg.BackgroundGeolocation.addGeofence(bg.Geofence(
identifier: _identifier,
vertices: vertices,
notifyOnEntry: _notifyOnEntry,
notifyOnExit: _notifyOnExit,
notifyOnDwell: _notifyOnDwell,
loiteringDelay: _loiteringDelay,
extras: {
'radius': _radius,
'center': {
'latitude': center?.latitude,
'longitude': center?.longitude
},
'vertices': vertices
} // meta-data for tracker.transistorsoft.com
)).then((bool success) {
bg.BackgroundGeolocation.playSound(
util.Dialog.getSoundId('ADD_GEOFENCE'));
}).catchError((error) {
print('[addGeofence] ERROR: $error');
});
}
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
const labelStyle = TextStyle(color: Colors.blue, fontSize: 16.0);
return new Scaffold(
appBar: new AppBar(
leading: IconButton(
onPressed: _onClickClose,
icon: Icon(Icons.close),
color: Colors.black),
title: const Text('Add Geofence'),
foregroundColor: Colors.black,
backgroundColor: Theme.of(context).bottomAppBarTheme.color,
iconTheme: IconThemeData(color: Colors.black),
actions: [
MaterialButton(child: Text('Add'), onPressed: _onClickAdd)
]),
body: Container(
padding: EdgeInsets.only(left: 10.0),
child: ListView(children: <Widget>[
TextField(
onChanged: (String value) {
_identifier = value;
},
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Unique geofence identifier',
labelText: "identifier",
labelStyle: labelStyle)),
Visibility(
visible: (center != null),
child: FormField(
enabled: (center != null),
builder: (FormFieldState state) {
return InputDecorator(
decoration: InputDecoration(
labelText: 'Radius', labelStyle: labelStyle),
child: new DropdownButtonHideUnderline(
child: new DropdownButton(
value: _radius.toInt().toString(),
isDense: true,
onChanged: (String? value) {
setState(() {
_radius = double.parse(value!);
});
},
items: [
DropdownMenuItem(
value: '150', child: new Text('150')),
DropdownMenuItem(
value: '200', child: new Text('200')),
DropdownMenuItem(
value: '500', child: new Text('500')),
DropdownMenuItem(
value: '1000', child: new Text('1000')),
DropdownMenuItem(
value: '5000', child: new Text('5000'))
]),
),
);
},
),
),
FormField(builder: (FormFieldState state) {
return InputDecorator(
decoration: InputDecoration(
labelStyle: labelStyle,
labelText: 'Geofence Transistions'
//labelText: name
),
child: Column(children: <Widget>[
Row(children: <Widget>[
Expanded(
flex: 3,
child: Text('notifyOnEntry', style: labelStyle)),
Expanded(
flex: 1,
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Switch(
value: _notifyOnEntry,
onChanged: (bool value) {
setState(() {
_notifyOnEntry = value;
});
})
]))
]),
Row(children: <Widget>[
Expanded(
flex: 3,
child: Text('notifyOnExit', style: labelStyle)),
Expanded(
flex: 1,
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Switch(
value: _notifyOnExit,
onChanged: (bool value) {
setState(() {
_notifyOnExit = value;
});
})
]))
]),
Row(children: <Widget>[
Expanded(
flex: 3,
child: Text('notifyOnDwell',
style: TextStyle(
color: Colors.blue, fontSize: 15.0))),
Expanded(
flex: 1,
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Switch(
value: _notifyOnDwell,
onChanged: (bool value) {
setState(() {
_notifyOnDwell = value;
});
})
]))
]),
TextField(
onChanged: (String value) {
_loiteringDelay = int.parse(value);
},
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText:
'Delay in ms before DWELL transition fires',
labelText: "loiteringDelay (milliseconds)",
labelStyle: labelStyle,
border: InputBorder.none,
)),
]));
})
])));
}
}