Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit a7c4929

Browse files
authored
[camera] Expanded platform interface to support setting flash mode (#3313)
* Expanded platform interface so support setting flash mode * Formatted dart code * Manually serialize flash mode enum rather than relying on stringification. * Add default to flash mode serialization
1 parent f9d5525 commit a7c4929

File tree

9 files changed

+108
-1
lines changed

9 files changed

+108
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.1
2+
3+
- Added interface methods for setting flash mode.
4+
15
## 1.0.0
26

37
- Initial open-source release

packages/camera/camera_platform_interface/lib/src/method_channel/method_channel_camera.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,35 @@ class MethodChannelCamera extends CameraPlatform {
175175
<String, dynamic>{'cameraId': cameraId},
176176
);
177177

178+
@override
179+
Future<void> setFlashMode(int cameraId, FlashMode mode) =>
180+
_channel.invokeMethod<void>(
181+
'setFlashMode',
182+
<String, dynamic>{
183+
'cameraId': cameraId,
184+
'mode': _serializeFlashMode(mode),
185+
},
186+
);
187+
178188
@override
179189
Widget buildPreview(int cameraId) {
180190
return Texture(textureId: cameraId);
181191
}
182192

193+
/// Returns the flash mode as a String.
194+
String _serializeFlashMode(FlashMode flashMode) {
195+
switch (flashMode) {
196+
case FlashMode.off:
197+
return 'off';
198+
case FlashMode.auto:
199+
return 'auto';
200+
case FlashMode.always:
201+
return 'always';
202+
default:
203+
throw ArgumentError('Unknown FlashMode value');
204+
}
205+
}
206+
183207
/// Returns the resolution preset as a String.
184208
String _serializeResolutionPreset(ResolutionPreset resolutionPreset) {
185209
switch (resolutionPreset) {

packages/camera/camera_platform_interface/lib/src/platform_interface/camera_platform.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ abstract class CameraPlatform extends PlatformInterface {
108108
throw UnimplementedError('resumeVideoRecording() is not implemented.');
109109
}
110110

111+
/// Sets the flash mode for taking pictures.
112+
Future<void> setFlashMode(int cameraId, FlashMode mode) {
113+
throw UnimplementedError('setFlashMode() is not implemented.');
114+
}
115+
111116
/// Returns a widget showing a live camera preview.
112117
Widget buildPreview(int cameraId) {
113118
throw UnimplementedError('buildView() has not been implemented.');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// The possible flash modes that can be set for a camera
6+
enum FlashMode {
7+
/// Do not use the flash when taking a picture.
8+
off,
9+
10+
/// Let the device decide whether to flash the camera when taking a picture.
11+
auto,
12+
13+
/// Always use the flash when taking a picture.
14+
always,
15+
}

packages/camera/camera_platform_interface/lib/src/types/types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
export 'camera_description.dart';
66
export 'resolution_preset.dart';
77
export 'camera_exception.dart';
8+
export 'flash_mode.dart';

packages/camera/camera_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: A common platform interface for the camera plugin.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/camera/camera_platform_interface
44
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
55
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
6-
version: 1.0.0
6+
version: 1.0.1
77

88
dependencies:
99
flutter:

packages/camera/camera_platform_interface/test/camera_platform_interface_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ void main() {
173173
);
174174
});
175175

176+
test(
177+
'Default implementation of setFlashMode() should throw unimplemented error',
178+
() {
179+
// Arrange
180+
final cameraPlatform = ExtendsCameraPlatform();
181+
182+
// Act & Assert
183+
expect(
184+
() => cameraPlatform.setFlashMode(1, null),
185+
throwsUnimplementedError,
186+
);
187+
});
188+
176189
test(
177190
'Default implementation of startVideoRecording() should throw unimplemented error',
178191
() {

packages/camera/camera_platform_interface/test/method_channel/method_channel_camera_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,29 @@ void main() {
470470
]);
471471
});
472472

473+
test('Should set the flash mode', () async {
474+
// Arrange
475+
MethodChannelMock channel = MethodChannelMock(
476+
channelName: 'plugins.flutter.io/camera',
477+
methods: {'setFlashMode': null},
478+
);
479+
480+
// Act
481+
await camera.setFlashMode(cameraId, FlashMode.always);
482+
await camera.setFlashMode(cameraId, FlashMode.auto);
483+
await camera.setFlashMode(cameraId, FlashMode.off);
484+
485+
// Assert
486+
expect(channel.log, <Matcher>[
487+
isMethodCall('setFlashMode',
488+
arguments: {'cameraId': cameraId, 'mode': 'always'}),
489+
isMethodCall('setFlashMode',
490+
arguments: {'cameraId': cameraId, 'mode': 'auto'}),
491+
isMethodCall('setFlashMode',
492+
arguments: {'cameraId': cameraId, 'mode': 'off'}),
493+
]);
494+
});
495+
473496
test('Should build a texture widget as preview widget', () async {
474497
// Act
475498
Widget widget = camera.buildPreview(cameraId);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:camera_platform_interface/camera_platform_interface.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
8+
void main() {
9+
test('FlashMode should contain 3 options', () {
10+
final values = FlashMode.values;
11+
12+
expect(values.length, 3);
13+
});
14+
15+
test("FlashMode enum should have items in correct index", () {
16+
final values = FlashMode.values;
17+
18+
expect(values[0], FlashMode.off);
19+
expect(values[1], FlashMode.auto);
20+
expect(values[2], FlashMode.always);
21+
});
22+
}

0 commit comments

Comments
 (0)