Sub topic 14
Flash, Zoom, and Switching Cameras
This chapter assumes you know Dart async/await, StatefulWidget, and the basic CameraController setup from earlier chapters. The controller must be initialized before you change its flash mode, zoom level, or active camera.
Understanding camera controls
The camera plugin exposes controls through CameraController:
setFlashModechanges the flash or continuous light.getMinZoomLevelandgetMaxZoomLevelreport device-specific zoom limits.setZoomLevelapplies a zoom value.- A new
CameraControlleris required when switching to another camera.
A phone can have several cameras, such as front, rear, ultra-wide, and telephoto. Do not assume that index 0 always means the rear camera. Inspect each CameraDescription and choose by its lensDirection.
Changing the flash mode
The available modes include off, auto, always, and torch. A torch stays on while the preview is active. It is useful for continuous lighting, but it is different from a flash fired during photo capture.
Future<void> setFlash(FlashMode mode) async {
final controller = _controller;
if (controller == null || !controller.value.isInitialized) return;
try {
await controller.setFlashMode(mode);
if (mounted) setState(() {});
} on CameraException catch (error) {
debugPrint('Flash error: ${error.code}');
}
}A device may not support every mode. Keep the control available only when the platform and camera report support, and always handle CameraException.
Applying safe zoom values
Zoom limits differ between devices. Read them after initialization, then clamp user input to that range.
double _minZoom = 1.0;
double _maxZoom = 1.0;
Future<void> loadZoomLimits() async {
final controller = _controller;
if (controller == null) return;
_minZoom = await controller.getMinZoomLevel();
_maxZoom = await controller.getMaxZoomLevel();
}
Future<void> setZoom(double requested) async {
final controller = _controller;
if (controller == null || !controller.value.isInitialized) return;
final zoom = requested.clamp(_minZoom, _maxZoom).toDouble();
await controller.setZoomLevel(zoom);
}Use a slider whose minimum is _minZoom and maximum is _maxZoom. Do not hard-code a maximum such as 10.0; some cameras support less, while others support more.
Switching between cameras
Keep the available camera descriptions in state. To switch, select another description, dispose the old controller, create a new controller, and wait for initialization before showing its preview.
Future<void> switchCamera() async {
if (_cameras.length < 2) return;
final current = _controller;
final currentDirection = current?.description.lensDirection;
final next = _cameras.firstWhere(
(camera) => camera.lensDirection != currentDirection,
orElse: () => _cameras.first,
);
await current?.dispose();
final replacement = CameraController(
next,
ResolutionPreset.medium,
enableAudio: false,
);
try {
await replacement.initialize();
if (!mounted) {
await replacement.dispose();
return;
}
setState(() => _controller = replacement);
await loadZoomLimits();
} on CameraException catch (error) {
await replacement.dispose();
debugPrint('Camera switch failed: ${error.code}');
}
}Disposing first releases the hardware resource. During this short transition, show a loading indicator instead of calling capture or zoom methods on the old controller.
Exercise
Why should an app read the camera's zoom limits instead of assuming that the maximum zoom is 10.0?
Show answer
Zoom limits vary by device and lens, so reading and clamping the reported limits prevents invalid values and failed camera operations.