Skip to content

Commit bd65231

Browse files
authored
🐛 Make ThemeData properly wraps the content (#303)
This PR also makes build widget methods' signatures consistent.
1 parent 210fcbf commit bd65231

File tree

5 files changed

+102
-58
lines changed

5 files changed

+102
-58
lines changed

lib/src/states/camera_picker_state.dart

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,10 @@ class CameraPickerState extends State<CameraPicker>
12451245
return const SizedBox.shrink();
12461246
}
12471247
Widget backButton = buildBackButton(context);
1248-
Widget flashModeSwitch = buildFlashModeSwitch(context, v);
1248+
Widget flashModeSwitch = buildFlashModeSwitch(
1249+
context: context,
1250+
cameraValue: v,
1251+
);
12491252
if (isCameraRotated && !enableScaledPreview) {
12501253
backButton = RotatedBox(
12511254
quarterTurns: cameraQuarterTurns,
@@ -1296,9 +1299,12 @@ class CameraPickerState extends State<CameraPicker>
12961299

12971300
/// The button to switch flash modes.
12981301
/// 切换闪光灯模式的按钮
1299-
Widget buildFlashModeSwitch(BuildContext context, CameraValue value) {
1302+
Widget buildFlashModeSwitch({
1303+
required BuildContext context,
1304+
required CameraValue cameraValue,
1305+
}) {
13001306
final IconData icon;
1301-
switch (value.flashMode) {
1307+
switch (cameraValue.flashMode) {
13021308
case FlashMode.off:
13031309
icon = Icons.flash_off;
13041310
break;
@@ -1313,15 +1319,18 @@ class CameraPickerState extends State<CameraPicker>
13131319
break;
13141320
}
13151321
return IconButton(
1316-
onPressed: () => switchFlashesMode(value),
1317-
tooltip: textDelegate.sFlashModeLabel(value.flashMode),
1322+
onPressed: () => switchFlashesMode(cameraValue),
1323+
tooltip: textDelegate.sFlashModeLabel(cameraValue.flashMode),
13181324
icon: Icon(icon, size: 24),
13191325
);
13201326
}
13211327

13221328
/// Text widget for shooting tips.
13231329
/// 拍摄的提示文字
1324-
Widget buildCaptureTips(CameraController? controller) {
1330+
Widget buildCaptureTips({
1331+
required BuildContext context,
1332+
CameraController? controller,
1333+
}) {
13251334
return AnimatedOpacity(
13261335
duration: recordDetectDuration,
13271336
opacity: controller?.value.isRecordingVideo ?? false ? 0 : 1,
@@ -1398,7 +1407,10 @@ class CameraPickerState extends State<CameraPicker>
13981407
const Spacer(),
13991408
Expanded(
14001409
child: Center(
1401-
child: buildCaptureButton(context, constraints),
1410+
child: buildCaptureButton(
1411+
context: context,
1412+
constraints: constraints,
1413+
),
14021414
),
14031415
),
14041416
if (controller != null &&
@@ -1429,7 +1441,10 @@ class CameraPickerState extends State<CameraPicker>
14291441

14301442
/// The shooting button.
14311443
/// 拍照按钮
1432-
Widget buildCaptureButton(BuildContext context, BoxConstraints constraints) {
1444+
Widget buildCaptureButton({
1445+
required BuildContext context,
1446+
required BoxConstraints constraints,
1447+
}) {
14331448
final showProgressIndicator =
14341449
isCaptureButtonTapDown || MediaQuery.accessibleNavigationOf(context);
14351450

@@ -1500,7 +1515,8 @@ class CameraPickerState extends State<CameraPicker>
15001515
showProgressIndicator && isShootingButtonAnimate,
15011516
duration: pickerConfig.maximumRecordingDuration!,
15021517
size: size,
1503-
ringsColor: theme.indicatorColor,
1518+
// ignore: deprecated_member_use
1519+
ringsColor: Theme.of(context).indicatorColor,
15041520
ringsWidth: 3,
15051521
),
15061522
),
@@ -1514,13 +1530,14 @@ class CameraPickerState extends State<CameraPicker>
15141530
}
15151531

15161532
Widget buildExposureSlider({
1533+
required BuildContext context,
15171534
required ExposureMode mode,
15181535
required double size,
15191536
required double height,
15201537
required double gap,
15211538
}) {
15221539
final bool isLocked = mode == ExposureMode.locked;
1523-
final Color? color = isLocked ? _lockedColor : theme.iconTheme.color;
1540+
final color = isLocked ? _lockedColor : Theme.of(context).iconTheme.color;
15241541
final Widget lineWidget = ValueListenableBuilder<bool>(
15251542
valueListenable: isFocusPointDisplays,
15261543
builder: (_, bool value, Widget? child) => AnimatedOpacity(
@@ -1580,6 +1597,7 @@ class CameraPickerState extends State<CameraPicker>
15801597
/// The area widget for the last exposure point that user manually set.
15811598
/// 用户手动设置的曝光点的区域显示
15821599
Widget buildFocusingPoint({
1600+
required BuildContext context,
15831601
required CameraValue cameraValue,
15841602
required BoxConstraints constraints,
15851603
int quarterTurns = 0,
@@ -1612,6 +1630,7 @@ class CameraPickerState extends State<CameraPicker>
16121630
const SizedBox(height: verticalGap),
16131631
Expanded(
16141632
child: buildExposureSlider(
1633+
context: context,
16151634
mode: exposureMode,
16161635
size: size,
16171636
height: height,
@@ -1698,7 +1717,7 @@ class CameraPickerState extends State<CameraPicker>
16981717
size: pointWidth,
16991718
color: cameraValue.exposureMode == ExposureMode.locked
17001719
? _lockedColor
1701-
: theme.iconTheme.color!,
1720+
: Theme.of(context).iconTheme.color,
17021721
),
17031722
),
17041723
),
@@ -1718,10 +1737,10 @@ class CameraPickerState extends State<CameraPicker>
17181737

17191738
/// The [GestureDetector] widget for setting exposure point manually.
17201739
/// 用于手动设置曝光点的 [GestureDetector]
1721-
Widget buildExposureDetector(
1722-
BuildContext context,
1723-
BoxConstraints constraints,
1724-
) {
1740+
Widget buildExposureDetector({
1741+
required BuildContext context,
1742+
required BoxConstraints constraints,
1743+
}) {
17251744
return Semantics(
17261745
label: textDelegate.sCameraPreviewLabel(
17271746
innerController?.description.lensDirection,
@@ -1812,8 +1831,9 @@ class CameraPickerState extends State<CameraPicker>
18121831
children: <Widget>[
18131832
preview,
18141833
if (pickerConfig.enableSetExposure)
1815-
buildExposureDetector(context, constraints),
1834+
buildExposureDetector(context: context, constraints: constraints),
18161835
buildFocusingPoint(
1836+
context: context,
18171837
cameraValue: cameraValue,
18181838
constraints: constraints,
18191839
quarterTurns: cameraQuarterTurns,
@@ -1865,11 +1885,11 @@ class CameraPickerState extends State<CameraPicker>
18651885
);
18661886
}
18671887

1868-
Widget buildForegroundBody(
1869-
BuildContext context,
1870-
BoxConstraints constraints,
1888+
Widget buildForegroundBody({
1889+
required BuildContext context,
1890+
required BoxConstraints constraints,
18711891
DeviceOrientation? deviceOrientation,
1872-
) {
1892+
}) {
18731893
final orientation = deviceOrientation ?? MediaQuery.orientationOf(context);
18741894
final isPortrait = orientation.toString().contains('portrait');
18751895
return SafeArea(
@@ -1888,7 +1908,12 @@ class CameraPickerState extends State<CameraPicker>
18881908
child: buildSettingActions(context),
18891909
),
18901910
const Spacer(),
1891-
ExcludeSemantics(child: buildCaptureTips(innerController)),
1911+
ExcludeSemantics(
1912+
child: buildCaptureTips(
1913+
context: context,
1914+
controller: innerController,
1915+
),
1916+
),
18921917
Semantics(
18931918
sortKey: const OrdinalSortKey(2),
18941919
hidden: innerController == null,
@@ -1974,9 +1999,13 @@ class CameraPickerState extends State<CameraPicker>
19741999
previewWidget,
19752000
if (enableScaledPreview) ...<Widget>[
19762001
if (pickerConfig.enableSetExposure)
1977-
buildExposureDetector(context, constraints),
2002+
buildExposureDetector(
2003+
context: context,
2004+
constraints: constraints,
2005+
),
19782006
buildInitializeWrapper(
19792007
builder: (CameraValue v, _) => buildFocusingPoint(
2008+
context: context,
19802009
cameraValue: v,
19812010
constraints: constraints,
19822011
),
@@ -1988,13 +2017,17 @@ class CameraPickerState extends State<CameraPicker>
19882017
),
19892018
],
19902019
if (innerController == null)
1991-
buildForegroundBody(context, constraints, null)
2020+
buildForegroundBody(
2021+
context: context,
2022+
constraints: constraints,
2023+
deviceOrientation: null,
2024+
)
19922025
else
19932026
buildInitializeWrapper(
19942027
builder: (CameraValue v, _) => buildForegroundBody(
1995-
context,
1996-
constraints,
1997-
v.deviceOrientation,
2028+
context: context,
2029+
constraints: constraints,
2030+
deviceOrientation: v.deviceOrientation,
19982031
),
19992032
),
20002033
],
@@ -2005,21 +2038,6 @@ class CameraPickerState extends State<CameraPicker>
20052038

20062039
@override
20072040
Widget build(BuildContext context) {
2008-
Widget body = Builder(builder: buildBody);
2009-
if (isCameraRotated && enableScaledPreview) {
2010-
final MediaQueryData mq = MediaQuery.of(context);
2011-
body = RotatedBox(
2012-
quarterTurns: pickerConfig.cameraQuarterTurns,
2013-
child: MediaQuery(
2014-
data: mq.copyWith(
2015-
size: pickerConfig.cameraQuarterTurns.isOdd
2016-
? mq.size.flipped
2017-
: mq.size,
2018-
),
2019-
child: body,
2020-
),
2021-
);
2022-
}
20232041
return AnnotatedRegion<SystemUiOverlayStyle>(
20242042
value: const SystemUiOverlayStyle(
20252043
systemNavigationBarIconBrightness: Brightness.light,
@@ -2028,9 +2046,25 @@ class CameraPickerState extends State<CameraPicker>
20282046
),
20292047
child: Theme(
20302048
data: theme,
2031-
child: Material(
2032-
color: Colors.black,
2033-
child: body,
2049+
child: Builder(
2050+
builder: (context) {
2051+
Widget body = buildBody(context);
2052+
if (isCameraRotated && enableScaledPreview) {
2053+
final MediaQueryData mq = MediaQuery.of(context);
2054+
body = RotatedBox(
2055+
quarterTurns: pickerConfig.cameraQuarterTurns,
2056+
child: MediaQuery(
2057+
data: mq.copyWith(
2058+
size: pickerConfig.cameraQuarterTurns.isOdd
2059+
? mq.size.flipped
2060+
: mq.size,
2061+
),
2062+
child: body,
2063+
),
2064+
);
2065+
}
2066+
return Material(color: Colors.black, child: body);
2067+
},
20342068
),
20352069
),
20362070
);

lib/src/states/camera_picker_viewer_state.dart

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class CameraPickerViewerState extends State<CameraPickerViewer> {
289289
minWidth: 20,
290290
height: 32,
291291
padding: const EdgeInsets.symmetric(horizontal: 20),
292-
color: theme.colorScheme.secondary,
292+
color: Theme.of(context).colorScheme.secondary,
293293
shape: RoundedRectangleBorder(
294294
borderRadius: BorderRadius.circular(3),
295295
),
@@ -298,7 +298,7 @@ class CameraPickerViewerState extends State<CameraPickerViewer> {
298298
child: Text(
299299
Singleton.textDelegate.confirm,
300300
style: TextStyle(
301-
color: theme.textTheme.bodyLarge?.color,
301+
color: Theme.of(context).textTheme.bodyLarge?.color,
302302
fontSize: 17,
303303
fontWeight: FontWeight.normal,
304304
),
@@ -402,15 +402,20 @@ class CameraPickerViewerState extends State<CameraPickerViewer> {
402402
deletePreviewFileIfConfigured();
403403
}
404404
},
405-
child: Material(
406-
color: Colors.black,
407-
child: Stack(
408-
fit: StackFit.expand,
409-
children: <Widget>[
410-
buildPreview(context),
411-
buildForeground(context),
412-
if (isSavingEntity) buildLoading(context),
413-
],
405+
child: Theme(
406+
data: theme,
407+
child: Builder(
408+
builder: (context) => Material(
409+
color: Colors.black,
410+
child: Stack(
411+
fit: StackFit.expand,
412+
children: <Widget>[
413+
buildPreview(context),
414+
buildForeground(context),
415+
if (isSavingEntity) buildLoading(context),
416+
],
417+
),
418+
),
414419
),
415420
),
416421
);

lib/src/widgets/camera_focus_point.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class CameraFocusPoint extends StatelessWidget {
1313
});
1414

1515
final double size;
16-
final Color color;
16+
final Color? color;
1717

1818
@override
1919
Widget build(BuildContext context) {
@@ -51,7 +51,7 @@ final class CameraFocusPointPainter extends CustomPainter {
5151
final double size;
5252
final double radius;
5353
final double strokeWidth;
54-
final Color color;
54+
final Color? color;
5555

5656
Radius get _circularRadius => Radius.circular(radius);
5757

@@ -61,8 +61,10 @@ final class CameraFocusPointPainter extends CustomPainter {
6161
final double lineLength = dividedSize.width - radius;
6262
final Paint paint = Paint()
6363
..style = PaintingStyle.stroke
64-
..color = color
6564
..strokeWidth = strokeWidth;
65+
if (color != null) {
66+
paint.color = color!;
67+
}
6668

6769
final Path path = Path()
6870
// Move to the start of the arc-line group at the left-top.

lib/src/widgets/camera_picker.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class CameraPicker extends StatefulWidget {
7878
selectionColor: themeColor.withAlpha(100),
7979
selectionHandleColor: themeColor,
8080
),
81+
// ignore: deprecated_member_use
8182
indicatorColor: themeColor,
8283
appBarTheme: const AppBarTheme(
8384
systemOverlayStyle: SystemUiOverlayStyle(

lib/src/widgets/camera_picker_viewer.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CameraPickerViewer extends StatefulWidget {
4141
/// 跳转至选择预览的静态方法
4242
static Future<AssetEntity?> pushToViewer(
4343
BuildContext context, {
44+
Key? key,
4445
required CameraPickerConfig pickerConfig,
4546
required CameraPickerViewType viewType,
4647
required XFile previewXFile,
@@ -53,6 +54,7 @@ class CameraPickerViewer extends StatefulWidget {
5354
).push<AssetEntity?>(
5455
PageRouteBuilder<AssetEntity?>(
5556
pageBuilder: (_, __, ___) => CameraPickerViewer._(
57+
key: key,
5658
viewType: viewType,
5759
previewXFile: previewXFile,
5860
pickerConfig: pickerConfig,

0 commit comments

Comments
 (0)