@@ -241,14 +241,10 @@ class CameraPickerState extends State<CameraPicker>
241
241
final ValueNotifier <bool > _isExposureModeDisplays =
242
242
ValueNotifier <bool >(false );
243
243
244
- /// The [ValueNotifier] to keep the [CameraController] .
245
- /// 用于保持 [CameraController] 的 [ValueNotifier]
246
- final ValueNotifier <CameraController ?> _controllerNotifier =
247
- ValueNotifier <CameraController ?>(null );
248
-
249
244
/// The controller for the current camera.
250
245
/// 当前相机实例的控制器
251
- CameraController get controller => _controllerNotifier.value! ;
246
+ CameraController get controller => _controller! ;
247
+ CameraController ? _controller;
252
248
253
249
/// Available cameras.
254
250
/// 可用的相机实例
@@ -390,7 +386,7 @@ class CameraPickerState extends State<CameraPicker>
390
386
}
391
387
WidgetsBinding .instance? .removeObserver (this );
392
388
controller.dispose ();
393
- _controllerNotifier .dispose ();
389
+ _controller ? .dispose ();
394
390
_currentExposureOffset.dispose ();
395
391
_lastExposurePoint.dispose ();
396
392
_exposureMode.dispose ();
@@ -405,7 +401,7 @@ class CameraPickerState extends State<CameraPicker>
405
401
@override
406
402
void didChangeAppLifecycleState (AppLifecycleState state) {
407
403
// 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) {
409
405
return ;
410
406
}
411
407
if (state == AppLifecycleState .inactive) {
@@ -434,14 +430,14 @@ class CameraPickerState extends State<CameraPicker>
434
430
/// 初始化相机实例
435
431
void initCameras ([CameraDescription ? cameraDescription]) {
436
432
// Save the current controller to a local variable.
437
- final CameraController ? _c = _controllerNotifier.value ;
433
+ final CameraController ? _c = _controller ;
438
434
// Then unbind the controller from widgets, which requires a build frame.
439
- setState (() {
435
+ safeSetState (() {
440
436
_maxAvailableZoom = 1 ;
441
437
_minAvailableZoom = 1 ;
442
438
_currentZoom = 1 ;
443
439
_baseZoom = 1 ;
444
- _controllerNotifier.value = null ;
440
+ _controller = null ;
445
441
// Meanwhile, cancel the existed exposure point and mode display.
446
442
_exposureModeDisplayTimer? .cancel ();
447
443
_exposurePointDisplayTimer? .cancel ();
@@ -472,7 +468,7 @@ class CameraPickerState extends State<CameraPicker>
472
468
}
473
469
474
470
// Initialize the controller with the given resolution preset.
475
- _controllerNotifier.value = CameraController (
471
+ _controller = CameraController (
476
472
cameraDescription ?? cameras[0 ],
477
473
widget.resolutionPreset,
478
474
enableAudio: enableAudio,
@@ -1252,23 +1248,18 @@ class CameraPickerState extends State<CameraPicker>
1252
1248
bool Function ()? isInitialized,
1253
1249
Widget ? child,
1254
1250
}) {
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 ();
1272
1263
}
1273
1264
1274
1265
Widget _cameraBuilder ({
@@ -1277,7 +1268,7 @@ class CameraPickerState extends State<CameraPicker>
1277
1268
required BoxConstraints constraints,
1278
1269
}) {
1279
1270
return AspectRatio (
1280
- aspectRatio: controller. value.aspectRatio,
1271
+ aspectRatio: value.aspectRatio,
1281
1272
child: RepaintBoundary (
1282
1273
child: Stack (
1283
1274
children: < Widget > [
@@ -1300,21 +1291,13 @@ class CameraPickerState extends State<CameraPicker>
1300
1291
return SafeArea (
1301
1292
child: Padding (
1302
1293
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
+ ],
1318
1301
),
1319
1302
),
1320
1303
);
0 commit comments