1+ import 'dart:async' ;
12import 'dart:math' ;
23
34import 'package:arcgis_example/main.dart' ;
@@ -9,12 +10,10 @@ class LocationIndicatorExamplePage extends StatefulWidget {
910 const LocationIndicatorExamplePage ({super .key});
1011
1112 @override
12- State <LocationIndicatorExamplePage > createState () =>
13- _LocationIndicatorExamplePageState ();
13+ State <LocationIndicatorExamplePage > createState () => _LocationIndicatorExamplePageState ();
1414}
1515
16- class _LocationIndicatorExamplePageState
17- extends State <LocationIndicatorExamplePage > {
16+ class _LocationIndicatorExamplePageState extends State <LocationIndicatorExamplePage > {
1817 final _mockLocations = [
1918 LatLng (48.1234963 , 11.5910182 ),
2019 LatLng (48.1239241 , 11.45897063 ),
@@ -34,6 +33,8 @@ class _LocationIndicatorExamplePageState
3433 var _activeAutoPanMode = AutoPanMode .off;
3534 var _wanderExtentFactor = 0.5 ;
3635
36+ StreamSubscription ? _panUpdateSubscription;
37+
3738 @override
3839 Widget build (BuildContext context) {
3940 return Scaffold (
@@ -48,8 +49,7 @@ class _LocationIndicatorExamplePageState
4849 : await _controller! .locationDisplay.startSource ();
4950 } catch (e, stack) {
5051 if (! mounted) return ;
51- ScaffoldMessenger .of (_snackBarKey.currentContext! )
52- .showSnackBar (SnackBar (content: Text ("$e " )));
52+ ScaffoldMessenger .of (_snackBarKey.currentContext! ).showSnackBar (SnackBar (content: Text ("$e " )));
5353 debugPrint ("$e " );
5454 debugPrintStack (stackTrace: stack);
5555 }
@@ -71,16 +71,18 @@ class _LocationIndicatorExamplePageState
7171 _controller = controller;
7272 _requestLocationPermission ();
7373 _configureLocationDisplay (Colors .blue);
74+
75+ _panUpdateSubscription? .cancel ();
76+ _panUpdateSubscription = controller.centerPosition ().listen ((_) => _refreshAutoPanMode ());
7477 },
7578 ),
7679 ),
7780 const SizedBox (height: 16 ),
81+ Text ("Current auto pan mode: ${_activeAutoPanMode .name }" ),
7882 ElevatedButton (
7983 onPressed: _switchLocationSource,
8084 child: Text (
81- _isManualLocationSource
82- ? "Use auto location source"
83- : "Use manual location source" ,
85+ _isManualLocationSource ? "Use auto location source" : "Use manual location source" ,
8486 ),
8587 ),
8688 if (_isManualLocationSource) ...[
@@ -101,15 +103,12 @@ class _LocationIndicatorExamplePageState
101103 ElevatedButton (
102104 onPressed: () {
103105 setState (
104- () =>
105- _useCourseSymbolForMovement = ! _useCourseSymbolForMovement,
106+ () => _useCourseSymbolForMovement = ! _useCourseSymbolForMovement,
106107 );
107108 _configureLocationDisplay (Colors .red);
108109 },
109110 child: Text (
110- _useCourseSymbolForMovement
111- ? "Disable course indicator"
112- : "Enable course indicator" ,
111+ _useCourseSymbolForMovement ? "Disable course indicator" : "Enable course indicator" ,
113112 ),
114113 ),
115114 ElevatedButton (
@@ -158,9 +157,7 @@ class _LocationIndicatorExamplePageState
158157 Future <void > _switchLocationSource () async {
159158 await _controller! .locationDisplay.stopSource ();
160159 await _controller! .setLocationDisplay (
161- _isManualLocationSource
162- ? ArcgisLocationDisplay ()
163- : ArcgisManualLocationDisplay (),
160+ _isManualLocationSource ? ArcgisLocationDisplay () : ArcgisManualLocationDisplay (),
164161 );
165162 setState (() => _isManualLocationSource = ! _isManualLocationSource);
166163
@@ -237,13 +234,30 @@ class _LocationIndicatorExamplePageState
237234 );
238235
239236 if (newSetAutoPanMode != null ) {
240- // No need to use setState
241- _activeAutoPanMode = newSetAutoPanMode;
237+ setState (() => _activeAutoPanMode = newSetAutoPanMode);
242238 _controller? .locationDisplay.setAutoPanMode (newSetAutoPanMode);
243239 }
244240 if (_activeAutoPanMode == AutoPanMode .recenter) {
245241 _wanderExtentFactor = newSetWanderExtentFactor;
246242 _controller? .locationDisplay.setWanderExtentFactor (_wanderExtentFactor);
247243 }
248244 }
245+
246+ @override
247+ void dispose () {
248+ _panUpdateSubscription? .cancel ();
249+ _refreshAutoPanModeTimer? .cancel ();
250+ super .dispose ();
251+ }
252+
253+ Timer ? _refreshAutoPanModeTimer;
254+
255+ Future <void > _refreshAutoPanMode () async {
256+ _refreshAutoPanModeTimer? .cancel ();
257+ _refreshAutoPanModeTimer = Timer (const Duration (milliseconds: 50 ), () async {
258+ final panMode = await _controller! .locationDisplay.getAutoPanMode ();
259+ if (! mounted) return ;
260+ setState (() => _activeAutoPanMode = panMode);
261+ });
262+ }
249263}
0 commit comments