@@ -9,7 +9,8 @@ import 'package:geolocator_platform_interface/geolocator_platform_interface.dart
99/// Defines the main theme color.
1010final MaterialColor themeMaterialColor =
1111 BaseflowPluginExample .createMaterialColor (
12- const Color .fromRGBO (48 , 49 , 60 , 1 ));
12+ const Color .fromRGBO (48 , 49 , 60 , 1 ),
13+ );
1314
1415void main () {
1516 runApp (const GeolocatorWidget ());
@@ -18,14 +19,14 @@ void main() {
1819/// Example [Widget] showing the functionalities of the geolocator plugin.
1920class GeolocatorWidget extends StatefulWidget {
2021 /// Creates a [PermissionHandlerWidget] .
21- const GeolocatorWidget ({
22- super .key,
23- });
22+ const GeolocatorWidget ({super .key});
2423
2524 /// Create a page containing the functionality of this plugin
2625 static ExamplePage createPage () {
2726 return ExamplePage (
28- Icons .location_on, (context) => const GeolocatorWidget ());
27+ Icons .location_on,
28+ (context) => const GeolocatorWidget (),
29+ );
2930 }
3031
3132 @override
@@ -77,110 +78,103 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
7778 break ;
7879 }
7980 },
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+ ],
10497 );
10598 }
10699
107100 @override
108101 Widget build (BuildContext context) {
109- const sizedBox = SizedBox (
110- height: 10 ,
111- );
102+ const sizedBox = SizedBox (height: 10 );
112103
113104 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,
147127 ),
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+ },
181142 ),
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+ );
184178 }
185179
186180 Future <void > _getCurrentPosition () async {
@@ -191,10 +185,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
191185 }
192186
193187 final position = await geolocatorAndroid.getCurrentPosition ();
194- _updatePositionList (
195- _PositionItemType .position,
196- position.toString (),
197- );
188+ _updatePositionList (_PositionItemType .position, position.toString ());
198189 }
199190
200191 Future <bool > _handlePermission () async {
@@ -224,10 +215,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
224215 // Android's shouldShowRequestPermissionRationale
225216 // returned true. According to Android guidelines
226217 // your App should show an explanatory UI now.
227- _updatePositionList (
228- _PositionItemType .log,
229- _kPermissionDeniedMessage,
230- );
218+ _updatePositionList (_PositionItemType .log, _kPermissionDeniedMessage);
231219
232220 return false ;
233221 }
@@ -245,10 +233,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
245233
246234 // When we reach here, permissions are granted and we can
247235 // continue accessing the position of the device.
248- _updatePositionList (
249- _PositionItemType .log,
250- _kPermissionGrantedMessage,
251- );
236+ _updatePositionList (_PositionItemType .log, _kPermissionGrantedMessage);
252237 return true ;
253238 }
254239
@@ -257,8 +242,9 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
257242 setState (() {});
258243 }
259244
260- bool _isListening () => ! (_positionStreamSubscription == null ||
261- _positionStreamSubscription! .isPaused);
245+ bool _isListening () =>
246+ ! (_positionStreamSubscription == null ||
247+ _positionStreamSubscription! .isPaused);
262248
263249 Color _determineButtonColor () {
264250 return _isListening () ? Colors .green : Colors .red;
@@ -267,22 +253,23 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
267253 void _toggleServiceStatusStream () {
268254 if (_serviceStatusStreamSubscription == null ) {
269255 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+ });
286273 }
287274 }
288275
@@ -313,17 +300,20 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
313300 ),
314301 );
315302 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+ });
327317 _positionStreamSubscription? .pause ();
328318 }
329319
@@ -361,10 +351,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
361351 void _getLastKnownPosition () async {
362352 final position = await geolocatorAndroid.getLastKnownPosition ();
363353 if (position != null ) {
364- _updatePositionList (
365- _PositionItemType .position,
366- position.toString (),
367- );
354+ _updatePositionList (_PositionItemType .position, position.toString ());
368355 } else {
369356 _updatePositionList (
370357 _PositionItemType .log,
@@ -410,10 +397,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
410397 displayValue = 'Error opening Application Settings.' ;
411398 }
412399
413- _updatePositionList (
414- _PositionItemType .log,
415- displayValue,
416- );
400+ _updatePositionList (_PositionItemType .log, displayValue);
417401 }
418402
419403 void _openLocationSettings () async {
@@ -426,17 +410,11 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
426410 displayValue = 'Error opening Location Settings' ;
427411 }
428412
429- _updatePositionList (
430- _PositionItemType .log,
431- displayValue,
432- );
413+ _updatePositionList (_PositionItemType .log, displayValue);
433414 }
434415}
435416
436- enum _PositionItemType {
437- log,
438- position,
439- }
417+ enum _PositionItemType { log, position }
440418
441419class _PositionItem {
442420 _PositionItem (this .type, this .displayValue);
0 commit comments