Skip to content

Commit 5177393

Browse files
committed
🔥 Remove the internal value notifier
1 parent 103562b commit 5177393

File tree

1 file changed

+28
-45
lines changed

1 file changed

+28
-45
lines changed

lib/src/widget/camera_picker.dart

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,10 @@ class CameraPickerState extends State<CameraPicker>
241241
final ValueNotifier<bool> _isExposureModeDisplays =
242242
ValueNotifier<bool>(false);
243243

244-
/// The [ValueNotifier] to keep the [CameraController].
245-
/// 用于保持 [CameraController][ValueNotifier]
246-
final ValueNotifier<CameraController?> _controllerNotifier =
247-
ValueNotifier<CameraController?>(null);
248-
249244
/// The controller for the current camera.
250245
/// 当前相机实例的控制器
251-
CameraController get controller => _controllerNotifier.value!;
246+
CameraController get controller => _controller!;
247+
CameraController? _controller;
252248

253249
/// Available cameras.
254250
/// 可用的相机实例
@@ -390,7 +386,7 @@ class CameraPickerState extends State<CameraPicker>
390386
}
391387
WidgetsBinding.instance?.removeObserver(this);
392388
controller.dispose();
393-
_controllerNotifier.dispose();
389+
_controller?.dispose();
394390
_currentExposureOffset.dispose();
395391
_lastExposurePoint.dispose();
396392
_exposureMode.dispose();
@@ -405,7 +401,7 @@ class CameraPickerState extends State<CameraPicker>
405401
@override
406402
void didChangeAppLifecycleState(AppLifecycleState state) {
407403
// App state changed before we got the chance to initialize.
408-
if (_controllerNotifier.value == null || !controller.value.isInitialized) {
404+
if (_controller == null || !controller.value.isInitialized) {
409405
return;
410406
}
411407
if (state == AppLifecycleState.inactive) {
@@ -434,14 +430,14 @@ class CameraPickerState extends State<CameraPicker>
434430
/// 初始化相机实例
435431
void initCameras([CameraDescription? cameraDescription]) {
436432
// Save the current controller to a local variable.
437-
final CameraController? _c = _controllerNotifier.value;
433+
final CameraController? _c = _controller;
438434
// Then unbind the controller from widgets, which requires a build frame.
439-
setState(() {
435+
safeSetState(() {
440436
_maxAvailableZoom = 1;
441437
_minAvailableZoom = 1;
442438
_currentZoom = 1;
443439
_baseZoom = 1;
444-
_controllerNotifier.value = null;
440+
_controller = null;
445441
// Meanwhile, cancel the existed exposure point and mode display.
446442
_exposureModeDisplayTimer?.cancel();
447443
_exposurePointDisplayTimer?.cancel();
@@ -472,7 +468,7 @@ class CameraPickerState extends State<CameraPicker>
472468
}
473469

474470
// Initialize the controller with the given resolution preset.
475-
_controllerNotifier.value = CameraController(
471+
_controller = CameraController(
476472
cameraDescription ?? cameras[0],
477473
widget.resolutionPreset,
478474
enableAudio: enableAudio,
@@ -1252,23 +1248,18 @@ class CameraPickerState extends State<CameraPicker>
12521248
bool Function()? isInitialized,
12531249
Widget? child,
12541250
}) {
1255-
return ValueListenableBuilder<CameraController?>(
1256-
valueListenable: _controllerNotifier,
1257-
builder: (_, CameraController? controller, __) {
1258-
if (controller != null) {
1259-
return ValueListenableBuilder<CameraValue>(
1260-
valueListenable: controller,
1261-
builder: (_, CameraValue value, Widget? w) {
1262-
return isInitialized?.call() ?? value.isInitialized
1263-
? builder(value, w)
1264-
: const SizedBox.shrink();
1265-
},
1266-
child: child,
1267-
);
1268-
}
1269-
return const SizedBox.shrink();
1270-
},
1271-
);
1251+
if (_controller != null) {
1252+
return ValueListenableBuilder<CameraValue>(
1253+
valueListenable: controller,
1254+
builder: (_, CameraValue value, Widget? w) {
1255+
return isInitialized?.call() ?? value.isInitialized
1256+
? builder(value, w)
1257+
: const SizedBox.shrink();
1258+
},
1259+
child: child,
1260+
);
1261+
}
1262+
return const SizedBox.shrink();
12721263
}
12731264

12741265
Widget _cameraBuilder({
@@ -1277,7 +1268,7 @@ class CameraPickerState extends State<CameraPicker>
12771268
required BoxConstraints constraints,
12781269
}) {
12791270
return AspectRatio(
1280-
aspectRatio: controller.value.aspectRatio,
1271+
aspectRatio: value.aspectRatio,
12811272
child: RepaintBoundary(
12821273
child: Stack(
12831274
children: <Widget>[
@@ -1300,21 +1291,13 @@ class CameraPickerState extends State<CameraPicker>
13001291
return SafeArea(
13011292
child: Padding(
13021293
padding: const EdgeInsets.only(bottom: 20.0),
1303-
child: ValueListenableBuilder<CameraController?>(
1304-
valueListenable: _controllerNotifier,
1305-
builder: (
1306-
BuildContext context,
1307-
CameraController? controller,
1308-
_,
1309-
) =>
1310-
Column(
1311-
children: <Widget>[
1312-
settingsAction,
1313-
const Spacer(),
1314-
tipsTextWidget(controller),
1315-
shootingActions(context, controller, constraints),
1316-
],
1317-
),
1294+
child: Column(
1295+
children: <Widget>[
1296+
settingsAction,
1297+
const Spacer(),
1298+
tipsTextWidget(_controller),
1299+
shootingActions(context, _controller, constraints),
1300+
],
13181301
),
13191302
),
13201303
);

0 commit comments

Comments
 (0)