Skip to content

Commit 8c1707f

Browse files
authored
🌐 Allow text delegates to be obtained by Locale (#99)
1 parent 817c9b2 commit 8c1707f

File tree

5 files changed

+85
-75
lines changed

5 files changed

+85
-75
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ that can be found in the LICENSE file. -->
1111
- Allow the foreground builder to be used all the time (#97) .
1212
The signature of the `ForegroundBuilder` has changed
1313
but can be easily migrated.
14+
- Allow text delegates to be obtained by `Locale`. (#99)
1415

1516
## 3.2.0+1
1617

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: wechat_camera_picker_demo
22
description: A new Flutter project.
3-
version: 3.2.0+10
3+
version: 3.2.1+11
44
publish_to: none
55

66
environment:

lib/src/constants/constants.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ export 'package:photo_manager/photo_manager.dart';
99
class Constants {
1010
const Constants._();
1111

12-
static CameraPickerTextDelegate textDelegate = CameraPickerTextDelegate();
12+
static CameraPickerTextDelegate textDelegate =
13+
const CameraPickerTextDelegate();
1314
}

lib/src/delegates/camera_picker_text_delegate.dart

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,56 @@
33
// in the LICENSE file.
44

55
import 'package:camera/camera.dart' show CameraLensDirection, FlashMode;
6+
import 'package:flutter/rendering.dart';
7+
8+
/// All text delegates.
9+
const List<CameraPickerTextDelegate> cameraPickerTextDelegates =
10+
<CameraPickerTextDelegate>[
11+
CameraPickerTextDelegate(),
12+
EnglishCameraPickerTextDelegate(),
13+
];
14+
15+
/// Obtain the text delegate from the given locale.
16+
CameraPickerTextDelegate cameraPickerTextDelegateFromLocale(Locale? locale) {
17+
if (locale == null) {
18+
return const CameraPickerTextDelegate();
19+
}
20+
final String languageCode = locale.languageCode.toLowerCase();
21+
for (final CameraPickerTextDelegate delegate in cameraPickerTextDelegates) {
22+
if (delegate.languageCode == languageCode) {
23+
return delegate;
24+
}
25+
}
26+
return const CameraPickerTextDelegate();
27+
}
628

7-
/// Text delegate that controls text in widgets, implemented with Chinese.
8-
/// 控制部件中的文字实现,中文。
29+
/// Text delegate implemented with Chinese.
30+
/// 中文文字实现
931
class CameraPickerTextDelegate {
32+
const CameraPickerTextDelegate();
33+
34+
String get languageCode => 'zh';
35+
1036
/// Confirm string for the confirm button.
1137
/// 确认按钮的字段
1238
String get confirm => '确认';
1339

14-
/// Tips string above the shooting button before shooting.
40+
/// Tips above the shooting button before shooting.
1541
/// 拍摄前确认按钮上方的提示文字
1642
String get shootingTips => '轻触拍照';
1743

44+
/// Tips with recording above the shooting button before shooting.
45+
/// 拍摄前确认按钮上方的提示文字(带录像)
46+
String get shootingWithRecordingTips => '轻触拍照,长按摄像';
47+
48+
/// Tips with only recording above the shooting button before shooting.
49+
/// 拍摄前确认按钮上方的提示文字(仅录像)
50+
String get shootingOnlyRecordingTips => '长按摄像';
51+
52+
/// Tips with tap recording above the shooting button before shooting.
53+
/// 拍摄前确认按钮上方的提示文字(点击录像)
54+
String get shootingTapRecordingTips => '轻触摄像';
55+
1856
/// Load failed string for item.
1957
/// 资源加载失败时的字段
2058
String get loadFailed => '加载失败';
@@ -77,37 +115,28 @@ class CameraPickerTextDelegate {
77115
}
78116
}
79117

80-
/// Default text delegate including recording implements with Chinese.
81-
/// 中文文字实现(包括摄像)
82-
class CameraPickerTextDelegateWithRecording extends CameraPickerTextDelegate {
118+
/// Text delegate implements with English.
119+
class EnglishCameraPickerTextDelegate extends CameraPickerTextDelegate {
120+
const EnglishCameraPickerTextDelegate();
121+
83122
@override
84-
String get shootingTips => '轻触拍照,长按摄像';
85-
}
123+
String get languageCode => 'en';
86124

87-
/// Default text delegate including only recording implements with Chinese.
88-
/// 中文文字实现(仅摄像)
89-
class CameraPickerTextDelegateWithOnlyRecording
90-
extends CameraPickerTextDelegate {
91125
@override
92-
String get shootingTips => '长按摄像';
93-
}
126+
String get confirm => 'Confirm';
94127

95-
/// Default text delegate including tap recording implements with Chinese.
96-
/// 中文文字实现(仅轻触摄像)
97-
class CameraPickerTextDelegateWithTapRecording
98-
extends CameraPickerTextDelegate {
99128
@override
100-
String get shootingTips => '轻触摄像';
101-
}
129+
String get shootingTips => 'Tap to take photo.';
102130

103-
/// Default text delegate implements with English.
104-
/// 英文文字实现
105-
class EnglishCameraPickerTextDelegate extends CameraPickerTextDelegate {
106131
@override
107-
String get confirm => 'Confirm';
132+
String get shootingWithRecordingTips =>
133+
'Tap to take photo. Long press to record video.';
108134

109135
@override
110-
String get shootingTips => 'Tap to take photo.';
136+
String get shootingOnlyRecordingTips => 'Long press to record video.';
137+
138+
@override
139+
String get shootingTapRecordingTips => 'Tap to record video.';
111140

112141
@override
113142
String get loadFailed => 'Load failed';
@@ -148,27 +177,3 @@ class EnglishCameraPickerTextDelegate extends CameraPickerTextDelegate {
148177
String sSwitchCameraLensDirectionLabel(CameraLensDirection value) =>
149178
'Switch to the ${sCameraLensDirectionLabel(value)} camera';
150179
}
151-
152-
/// Default text delegate including recording implements with English.
153-
/// 英文文字实现(包括摄像)
154-
class EnglishCameraPickerTextDelegateWithRecording
155-
extends EnglishCameraPickerTextDelegate {
156-
@override
157-
String get shootingTips => 'Tap to take photo. Long press to record video.';
158-
}
159-
160-
/// Default text delegate including only recording implements with English.
161-
/// 英文文字实现(仅摄像)
162-
class EnglishCameraPickerTextDelegateWithOnlyRecording
163-
extends EnglishCameraPickerTextDelegate {
164-
@override
165-
String get shootingTips => 'Long press to record video.';
166-
}
167-
168-
/// Default text delegate including tap recording implements with English.
169-
/// 英文文字实现(仅轻触摄像)
170-
class EnglishCameraPickerTextDelegateWithTapRecording
171-
extends EnglishCameraPickerTextDelegate {
172-
@override
173-
String get shootingTips => 'Tap to record video.';
174-
}

lib/src/widgets/camera_picker.dart

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,10 @@ class CameraPicker extends StatefulWidget {
4040
CameraPicker({
4141
Key? key,
4242
this.pickerConfig = const CameraPickerConfig(),
43+
Locale? locale,
4344
}) : super(key: key) {
44-
// Set text delegate accordingly.
45-
if (pickerConfig.textDelegate != null) {
46-
Constants.textDelegate = pickerConfig.textDelegate!;
47-
} else if (pickerConfig.enableRecording &&
48-
pickerConfig.onlyEnableRecording &&
49-
pickerConfig.enableTapRecording) {
50-
Constants.textDelegate = CameraPickerTextDelegateWithTapRecording();
51-
} else if (pickerConfig.enableRecording &&
52-
pickerConfig.onlyEnableRecording) {
53-
Constants.textDelegate = CameraPickerTextDelegateWithOnlyRecording();
54-
} else if (pickerConfig.enableRecording) {
55-
Constants.textDelegate = CameraPickerTextDelegateWithRecording();
56-
} else {
57-
Constants.textDelegate = CameraPickerTextDelegate();
58-
}
45+
Constants.textDelegate =
46+
pickerConfig.textDelegate ?? cameraPickerTextDelegateFromLocale(locale);
5947
}
6048

6149
final CameraPickerConfig pickerConfig;
@@ -67,8 +55,12 @@ class CameraPicker extends StatefulWidget {
6755
CameraPickerConfig pickerConfig = const CameraPickerConfig(),
6856
bool useRootNavigator = true,
6957
CameraPickerPageRouteBuilder<AssetEntity>? pageRouteBuilder,
58+
Locale? locale,
7059
}) {
71-
final Widget picker = CameraPicker(pickerConfig: pickerConfig);
60+
final Widget picker = CameraPicker(
61+
pickerConfig: pickerConfig,
62+
locale: locale ?? Localizations.maybeLocaleOf(context),
63+
);
7264
return Navigator.of(
7365
context,
7466
rootNavigator: useRootNavigator,
@@ -880,7 +872,7 @@ class CameraPickerState extends State<CameraPicker>
880872
return const SizedBox.shrink();
881873
}
882874
return Padding(
883-
padding: const EdgeInsets.symmetric(horizontal: 12.0),
875+
padding: const EdgeInsets.symmetric(horizontal: 12),
884876
child: Row(
885877
children: <Widget>[
886878
if (cameras.length > 1) switchCamerasButton,
@@ -936,15 +928,26 @@ class CameraPickerState extends State<CameraPicker>
936928
/// Text widget for shooting tips.
937929
/// 拍摄的提示文字
938930
Widget tipsTextWidget(CameraController? controller) {
931+
final String tips;
932+
if (config.enableRecording) {
933+
if (config.onlyEnableRecording) {
934+
if (config.enableTapRecording) {
935+
tips = _textDelegate.shootingTapRecordingTips;
936+
} else {
937+
tips = _textDelegate.shootingOnlyRecordingTips;
938+
}
939+
} else {
940+
tips = _textDelegate.shootingWithRecordingTips;
941+
}
942+
} else {
943+
tips = _textDelegate.shootingTips;
944+
}
939945
return AnimatedOpacity(
940946
duration: recordDetectDuration,
941947
opacity: controller?.value.isRecordingVideo ?? false ? 0 : 1,
942948
child: Padding(
943-
padding: const EdgeInsets.symmetric(vertical: 20.0),
944-
child: Text(
945-
_textDelegate.shootingTips,
946-
style: const TextStyle(fontSize: 15.0),
947-
),
949+
padding: const EdgeInsets.all(20),
950+
child: Text(tips, style: const TextStyle(fontSize: 15)),
948951
),
949952
);
950953
}
@@ -1049,7 +1052,7 @@ class CameraPickerState extends State<CameraPicker>
10491052
duration: maximumRecordingDuration!,
10501053
outerRadius: outerSize.width,
10511054
ringsColor: theme.indicatorColor,
1052-
ringsWidth: 2.0,
1055+
ringsWidth: 2,
10531056
),
10541057
),
10551058
],
@@ -1351,7 +1354,7 @@ class CameraPickerState extends State<CameraPicker>
13511354
Widget _contentBuilder(BoxConstraints constraints) {
13521355
return SafeArea(
13531356
child: Padding(
1354-
padding: const EdgeInsets.only(bottom: 20.0),
1357+
padding: const EdgeInsets.only(bottom: 20),
13551358
child: Column(
13561359
children: <Widget>[
13571360
Semantics(

0 commit comments

Comments
 (0)