@@ -9,7 +9,8 @@ import 'package:geolocator_platform_interface/geolocator_platform_interface.dart
9
9
/// Defines the main theme color.
10
10
final MaterialColor themeMaterialColor =
11
11
BaseflowPluginExample .createMaterialColor (
12
- const Color .fromRGBO (48 , 49 , 60 , 1 ));
12
+ const Color .fromRGBO (48 , 49 , 60 , 1 ),
13
+ );
13
14
14
15
void main () {
15
16
runApp (const GeolocatorWidget ());
@@ -18,14 +19,14 @@ void main() {
18
19
/// Example [Widget] showing the functionalities of the geolocator plugin.
19
20
class GeolocatorWidget extends StatefulWidget {
20
21
/// Creates a [PermissionHandlerWidget] .
21
- const GeolocatorWidget ({
22
- super .key,
23
- });
22
+ const GeolocatorWidget ({super .key});
24
23
25
24
/// Create a page containing the functionality of this plugin
26
25
static ExamplePage createPage () {
27
26
return ExamplePage (
28
- Icons .location_on, (context) => const GeolocatorWidget ());
27
+ Icons .location_on,
28
+ (context) => const GeolocatorWidget (),
29
+ );
29
30
}
30
31
31
32
@override
@@ -77,110 +78,103 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
77
78
break ;
78
79
}
79
80
},
80
- itemBuilder: (context) => [
81
- const PopupMenuItem (
82
- value: 1 ,
83
- child: Text ("Get Location Accuracy" ),
84
- ),
85
- if (Platform .isIOS)
86
- const PopupMenuItem (
87
- value: 2 ,
88
- child: Text ("Request Temporary Full Accuracy" ),
89
- ),
90
- const PopupMenuItem (
91
- value: 3 ,
92
- child: Text ("Open App Settings" ),
93
- ),
94
- if (Platform .isAndroid)
95
- const PopupMenuItem (
96
- value: 4 ,
97
- child: Text ("Open Location Settings" ),
98
- ),
99
- const PopupMenuItem (
100
- value: 5 ,
101
- child: Text ("Clear" ),
102
- ),
103
- ],
81
+ itemBuilder:
82
+ (context) => [
83
+ const PopupMenuItem (value: 1 , child: Text ("Get Location Accuracy" )),
84
+ if (Platform .isIOS)
85
+ const PopupMenuItem (
86
+ value: 2 ,
87
+ child: Text ("Request Temporary Full Accuracy" ),
88
+ ),
89
+ const PopupMenuItem (value: 3 , child: Text ("Open App Settings" )),
90
+ if (Platform .isAndroid)
91
+ const PopupMenuItem (
92
+ value: 4 ,
93
+ child: Text ("Open Location Settings" ),
94
+ ),
95
+ const PopupMenuItem (value: 5 , child: Text ("Clear" )),
96
+ ],
104
97
);
105
98
}
106
99
107
100
@override
108
101
Widget build (BuildContext context) {
109
- const sizedBox = SizedBox (
110
- height: 10 ,
111
- );
102
+ const sizedBox = SizedBox (height: 10 );
112
103
113
104
return BaseflowPluginExample (
114
- pluginName: 'Geolocator' ,
115
- githubURL: 'https://github.com/Baseflow/flutter-geolocator' ,
116
- pubDevURL: 'https://pub.dev/packages/geolocator' ,
117
- appBarActions: [
118
- _createActions ()
119
- ],
120
- pages: [
121
- ExamplePage (
122
- Icons .location_on,
123
- (context) => Scaffold (
124
- backgroundColor: Theme .of (context).colorScheme.surface,
125
- body: ListView .builder (
126
- itemCount: _positionItems.length,
127
- itemBuilder: (context, index) {
128
- final positionItem = _positionItems[index];
129
-
130
- if (positionItem.type == _PositionItemType .log) {
131
- return ListTile (
132
- title: Text (positionItem.displayValue,
133
- textAlign: TextAlign .center,
134
- style: const TextStyle (
135
- color: Colors .white,
136
- fontWeight: FontWeight .bold,
137
- )),
138
- );
139
- } else {
140
- return Card (
141
- child: ListTile (
142
- tileColor: themeMaterialColor,
143
- title: Text (
144
- positionItem.displayValue,
145
- style: const TextStyle (color: Colors .white),
146
- ),
105
+ pluginName: 'Geolocator' ,
106
+ githubURL: 'https://github.com/Baseflow/flutter-geolocator' ,
107
+ pubDevURL: 'https://pub.dev/packages/geolocator' ,
108
+ appBarActions: [_createActions ()],
109
+ pages: [
110
+ ExamplePage (
111
+ Icons .location_on,
112
+ (context) => Scaffold (
113
+ backgroundColor: Theme .of (context).colorScheme.surface,
114
+ body: ListView .builder (
115
+ itemCount: _positionItems.length,
116
+ itemBuilder: (context, index) {
117
+ final positionItem = _positionItems[index];
118
+
119
+ if (positionItem.type == _PositionItemType .log) {
120
+ return ListTile (
121
+ title: Text (
122
+ positionItem.displayValue,
123
+ textAlign: TextAlign .center,
124
+ style: const TextStyle (
125
+ color: Colors .white,
126
+ fontWeight: FontWeight .bold,
147
127
),
148
- );
149
- }
150
- },
151
- ),
152
- floatingActionButton: Column (
153
- crossAxisAlignment: CrossAxisAlignment .end,
154
- mainAxisAlignment: MainAxisAlignment .end,
155
- children: [
156
- FloatingActionButton (
157
- onPressed: _toggleListening,
158
- tooltip: (_positionStreamSubscription == null )
159
- ? 'Start position updates'
160
- : _positionStreamSubscription! .isPaused
161
- ? 'Resume'
162
- : 'Pause' ,
163
- backgroundColor: _determineButtonColor (),
164
- child: (_positionStreamSubscription == null ||
165
- _positionStreamSubscription! .isPaused)
166
- ? const Icon (Icons .play_arrow)
167
- : const Icon (Icons .pause),
168
- ),
169
- sizedBox,
170
- FloatingActionButton (
171
- onPressed: _getCurrentPosition,
172
- child: const Icon (Icons .my_location),
173
- ),
174
- sizedBox,
175
- FloatingActionButton (
176
- onPressed: _getLastKnownPosition,
177
- child: const Icon (Icons .bookmark),
178
- ),
179
- ],
180
- ),
128
+ ),
129
+ );
130
+ } else {
131
+ return Card (
132
+ child: ListTile (
133
+ tileColor: themeMaterialColor,
134
+ title: Text (
135
+ positionItem.displayValue,
136
+ style: const TextStyle (color: Colors .white),
137
+ ),
138
+ ),
139
+ );
140
+ }
141
+ },
181
142
),
182
- )
183
- ]);
143
+ floatingActionButton: Column (
144
+ crossAxisAlignment: CrossAxisAlignment .end,
145
+ mainAxisAlignment: MainAxisAlignment .end,
146
+ children: [
147
+ FloatingActionButton (
148
+ onPressed: _toggleListening,
149
+ tooltip:
150
+ (_positionStreamSubscription == null )
151
+ ? 'Start position updates'
152
+ : _positionStreamSubscription! .isPaused
153
+ ? 'Resume'
154
+ : 'Pause' ,
155
+ backgroundColor: _determineButtonColor (),
156
+ child:
157
+ (_positionStreamSubscription == null ||
158
+ _positionStreamSubscription! .isPaused)
159
+ ? const Icon (Icons .play_arrow)
160
+ : const Icon (Icons .pause),
161
+ ),
162
+ sizedBox,
163
+ FloatingActionButton (
164
+ onPressed: _getCurrentPosition,
165
+ child: const Icon (Icons .my_location),
166
+ ),
167
+ sizedBox,
168
+ FloatingActionButton (
169
+ onPressed: _getLastKnownPosition,
170
+ child: const Icon (Icons .bookmark),
171
+ ),
172
+ ],
173
+ ),
174
+ ),
175
+ ),
176
+ ],
177
+ );
184
178
}
185
179
186
180
Future <void > _getCurrentPosition () async {
@@ -191,10 +185,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
191
185
}
192
186
193
187
final position = await geolocatorAndroid.getCurrentPosition ();
194
- _updatePositionList (
195
- _PositionItemType .position,
196
- position.toString (),
197
- );
188
+ _updatePositionList (_PositionItemType .position, position.toString ());
198
189
}
199
190
200
191
Future <bool > _handlePermission () async {
@@ -224,10 +215,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
224
215
// Android's shouldShowRequestPermissionRationale
225
216
// returned true. According to Android guidelines
226
217
// your App should show an explanatory UI now.
227
- _updatePositionList (
228
- _PositionItemType .log,
229
- _kPermissionDeniedMessage,
230
- );
218
+ _updatePositionList (_PositionItemType .log, _kPermissionDeniedMessage);
231
219
232
220
return false ;
233
221
}
@@ -245,10 +233,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
245
233
246
234
// When we reach here, permissions are granted and we can
247
235
// continue accessing the position of the device.
248
- _updatePositionList (
249
- _PositionItemType .log,
250
- _kPermissionGrantedMessage,
251
- );
236
+ _updatePositionList (_PositionItemType .log, _kPermissionGrantedMessage);
252
237
return true ;
253
238
}
254
239
@@ -257,8 +242,9 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
257
242
setState (() {});
258
243
}
259
244
260
- bool _isListening () => ! (_positionStreamSubscription == null ||
261
- _positionStreamSubscription! .isPaused);
245
+ bool _isListening () =>
246
+ ! (_positionStreamSubscription == null ||
247
+ _positionStreamSubscription! .isPaused);
262
248
263
249
Color _determineButtonColor () {
264
250
return _isListening () ? Colors .green : Colors .red;
@@ -267,22 +253,23 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
267
253
void _toggleServiceStatusStream () {
268
254
if (_serviceStatusStreamSubscription == null ) {
269
255
final serviceStatusStream = geolocatorAndroid.getServiceStatusStream ();
270
- _serviceStatusStreamSubscription =
271
- serviceStatusStream.handleError ((error) {
272
- _serviceStatusStreamSubscription? .cancel ();
273
- _serviceStatusStreamSubscription = null ;
274
- }).listen ((serviceStatus) {
275
- String serviceStatusValue;
276
- if (serviceStatus == ServiceStatus .enabled) {
277
- serviceStatusValue = 'enabled' ;
278
- } else {
279
- serviceStatusValue = 'disabled' ;
280
- }
281
- _updatePositionList (
282
- _PositionItemType .log,
283
- 'Location service has been $serviceStatusValue ' ,
284
- );
285
- });
256
+ _serviceStatusStreamSubscription = serviceStatusStream
257
+ .handleError ((error) {
258
+ _serviceStatusStreamSubscription? .cancel ();
259
+ _serviceStatusStreamSubscription = null ;
260
+ })
261
+ .listen ((serviceStatus) {
262
+ String serviceStatusValue;
263
+ if (serviceStatus == ServiceStatus .enabled) {
264
+ serviceStatusValue = 'enabled' ;
265
+ } else {
266
+ serviceStatusValue = 'disabled' ;
267
+ }
268
+ _updatePositionList (
269
+ _PositionItemType .log,
270
+ 'Location service has been $serviceStatusValue ' ,
271
+ );
272
+ });
286
273
}
287
274
}
288
275
@@ -313,17 +300,20 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
313
300
),
314
301
);
315
302
final positionStream = geolocatorAndroid.getPositionStream (
316
- locationSettings: androidSettings);
317
- _positionStreamSubscription = positionStream.handleError ((error) {
318
- _positionStreamSubscription? .cancel ();
319
- _positionStreamSubscription = null ;
320
- }).listen ((position) {
321
- debugPrint (position.altitude.toString ());
322
- _updatePositionList (
323
- _PositionItemType .position,
324
- position.toString (),
325
- );
326
- });
303
+ locationSettings: androidSettings,
304
+ );
305
+ _positionStreamSubscription = positionStream
306
+ .handleError ((error) {
307
+ _positionStreamSubscription? .cancel ();
308
+ _positionStreamSubscription = null ;
309
+ })
310
+ .listen ((position) {
311
+ debugPrint (position.altitude.toString ());
312
+ _updatePositionList (
313
+ _PositionItemType .position,
314
+ position.toString (),
315
+ );
316
+ });
327
317
_positionStreamSubscription? .pause ();
328
318
}
329
319
@@ -361,10 +351,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
361
351
void _getLastKnownPosition () async {
362
352
final position = await geolocatorAndroid.getLastKnownPosition ();
363
353
if (position != null ) {
364
- _updatePositionList (
365
- _PositionItemType .position,
366
- position.toString (),
367
- );
354
+ _updatePositionList (_PositionItemType .position, position.toString ());
368
355
} else {
369
356
_updatePositionList (
370
357
_PositionItemType .log,
@@ -410,10 +397,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
410
397
displayValue = 'Error opening Application Settings.' ;
411
398
}
412
399
413
- _updatePositionList (
414
- _PositionItemType .log,
415
- displayValue,
416
- );
400
+ _updatePositionList (_PositionItemType .log, displayValue);
417
401
}
418
402
419
403
void _openLocationSettings () async {
@@ -426,17 +410,11 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
426
410
displayValue = 'Error opening Location Settings' ;
427
411
}
428
412
429
- _updatePositionList (
430
- _PositionItemType .log,
431
- displayValue,
432
- );
413
+ _updatePositionList (_PositionItemType .log, displayValue);
433
414
}
434
415
}
435
416
436
- enum _PositionItemType {
437
- log,
438
- position,
439
- }
417
+ enum _PositionItemType { log, position }
440
418
441
419
class _PositionItem {
442
420
_PositionItem (this .type, this .displayValue);
0 commit comments