Skip to content

Commit

Permalink
chore: improve state handling for camera example
Browse files Browse the repository at this point in the history
  • Loading branch information
jokerttu committed Feb 27, 2025
1 parent 17b0eeb commit 58fe7f2
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions example/lib/pages/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
calculateFocusCenter();
_minZoomLevel = await _navigationViewController.getMinZoomPreference();
_maxZoomLevel = await _navigationViewController.getMaxZoomPreference();
setState(() {});

if (mounted) {
setState(() {});
}
}

Future<void> _startNavigation() async {
Expand Down Expand Up @@ -78,6 +81,10 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
displayOptions: NavigationDisplayOptions(showDestinationMarkers: false),
);

if (!mounted) {
return;
}

setState(() {});
final NavigationRouteStatus status =
await GoogleMapsNavigator.setDestinations(msg);
Expand All @@ -88,9 +95,11 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
await _navigationViewController
.followMyLocation(CameraPerspective.tilted);

setState(() {
_navigationRunning = true;
});
if (mounted) {
setState(() {
_navigationRunning = true;
});
}
} else {
showMessage('Starting navigation failed.');
}
Expand All @@ -100,9 +109,11 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
if (_navigationRunning) {
await GoogleMapsNavigator.cleanup();

setState(() {
_navigationRunning = false;
});
if (mounted) {
setState(() {
_navigationRunning = false;
});
}
}
}

Expand Down Expand Up @@ -150,6 +161,9 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
]));

void _onCameraMoveStarted(CameraPosition position, bool gesture) {
if (!mounted) {
return;
}
if (_showCameraUpdates) {
showMessage(gesture
? 'Camera move started by gesture'
Expand All @@ -158,6 +172,9 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
}

void _onCameraMove(CameraPosition position) {
if (!mounted) {
return;
}
final String cameraState =
_isFollowingLocation ? 'Camera following' : 'Camera moving';
final String positionStr =
Expand All @@ -168,6 +185,9 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
}

void _onCameraIdle(CameraPosition position) {
if (!mounted) {
return;
}
setState(() {
_latestCameraUpdate =
'Camera idle\nPosition: ${position.target.latitude.toStringAsFixed(2)}, ${position.target.longitude.toStringAsFixed(2)}';
Expand All @@ -176,19 +196,28 @@ class _CameraPageState extends ExamplePageState<CameraPage> {

// Android only.
void _onCameraStartedFollowingLocation(CameraPosition position) {
if (!mounted) {
return;
}
setState(() {
_isFollowingLocation = true;
});
}

// Android only.
void _onCameraStoppedFollowingLocation(CameraPosition position) {
if (!mounted) {
return;
}
setState(() {
_isFollowingLocation = false;
});
}

void showMessage(String message) {
if (!mounted) {
return;
}
hideMessage();
if (isOverlayVisible) {
showOverlaySnackBar(message);
Expand All @@ -202,7 +231,10 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
ScaffoldMessenger.of(context).removeCurrentSnackBar();
}

void showAnimationFinishedMessage(bool success) {
void _showAnimationFinishedMessage(bool success) {
if (!mounted) {
return;
}
if (_displayAnimationFinished) {
showMessage(success ? 'Animation finished' : 'Animation canceled');
}
Expand Down Expand Up @@ -322,7 +354,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand All @@ -339,7 +371,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand All @@ -363,7 +395,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand All @@ -387,7 +419,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand All @@ -407,7 +439,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand All @@ -428,7 +460,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand All @@ -447,7 +479,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand All @@ -470,7 +502,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand All @@ -487,7 +519,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand All @@ -505,7 +537,7 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
? Duration(milliseconds: _animationDuration!)
: null,
onFinished: (bool success) =>
showAnimationFinishedMessage(success));
_showAnimationFinishedMessage(success));
} else {
_navigationViewController.moveCamera(cameraUpdate);
}
Expand Down

0 comments on commit 58fe7f2

Please sign in to comment.