瀏覽代碼

代码优化

gjh 1 周之前
父節點
當前提交
c9979bec1a
共有 3 個文件被更改,包括 50 次插入22 次删除
  1. 30 18
      lib/pages/view/widget/video_operation_view.dart
  2. 8 1
      lib/widget/count_down_text.dart
  3. 12 3
      lib/widget/webview_progress_bar.dart

+ 30 - 18
lib/pages/view/widget/video_operation_view.dart

@@ -45,8 +45,8 @@ class _VideoOperationViewState extends State<VideoOperationView> {
45 45
   void initState() {
46 46
     super.initState();
47 47
     widget.controller._setCountController(_countController);
48
-    widget.controller._recordingState.addListener(_onUpdateRecordingState);
49
-    widget.controller._isCountingDown.addListener(_onUpdateIsCountingDown);
48
+    widget.controller.recordingState.addListener(_onUpdateRecordingState);
49
+    widget.controller.isCountingDown.addListener(_onUpdateIsCountingDown);
50 50
   }
51 51
 
52 52
   ///更新录像状态
@@ -71,7 +71,7 @@ class _VideoOperationViewState extends State<VideoOperationView> {
71 71
   ///更新倒计时状态
72 72
   void _onUpdateIsCountingDown() {
73 73
     setState(() {
74
-      _isCountingDown = widget.controller._isCountingDown.value;
74
+      _isCountingDown = widget.controller.isCounting;
75 75
     });
76 76
   }
77 77
 
@@ -79,8 +79,9 @@ class _VideoOperationViewState extends State<VideoOperationView> {
79 79
   void dispose() {
80 80
     super.dispose();
81 81
     _recordingTimer?.cancel();
82
-    widget.controller._recordingState.removeListener(_onUpdateRecordingState);
83
-    widget.controller._isCountingDown.removeListener(_onUpdateIsCountingDown);
82
+    _countController.dispose();
83
+    widget.controller.recordingState.removeListener(_onUpdateRecordingState);
84
+    widget.controller.isCountingDown.removeListener(_onUpdateIsCountingDown);
84 85
   }
85 86
 
86 87
   @override
@@ -180,9 +181,8 @@ class _VideoOperationViewState extends State<VideoOperationView> {
180 181
             width: 17.5.r,
181 182
             height: 17.5.r,
182 183
             decoration: BoxDecoration(
183
-                color: widget.controller._recordingState.value
184
-                    ? Colors.red
185
-                    : Colors.white,
184
+                color:
185
+                    widget.controller.isRecording ? Colors.red : Colors.white,
186 186
                 shape: BoxShape.circle),
187 187
           ));
188 188
 
@@ -229,7 +229,7 @@ class _VideoOperationViewState extends State<VideoOperationView> {
229 229
     int delay = await SpUtil.getDelayShotTime();
230 230
     _countController.startCount(isDelay ? delay : 0);
231 231
     if (isDelay) {
232
-      widget.controller._isCountingDown.value = true;
232
+      widget.controller.isCounting = true;
233 233
     }
234 234
   }
235 235
 
@@ -250,7 +250,7 @@ class _VideoOperationViewState extends State<VideoOperationView> {
250 250
   ///关闭视频
251 251
   void _stopVideo() {
252 252
     widget.onStopVideo();
253
-    widget.controller._isCountingDown.value = false;
253
+    widget.controller.isCounting = false;
254 254
   }
255 255
 
256 256
   ///拍照倒计时
@@ -259,16 +259,22 @@ class _VideoOperationViewState extends State<VideoOperationView> {
259 259
       return;
260 260
     }
261 261
     widget.onTakePhoto();
262
-    widget.controller._isCountingDown.value = false;
262
+    widget.controller.isCounting = false;
263 263
   }
264 264
 }
265 265
 
266 266
 class VideoOperationViewController {
267 267
   CountDownTextController? _countController;
268 268
 
269
-  final ValueNotifier<bool> _recordingState = ValueNotifier(false);
269
+  ValueNotifier<bool>? _recordingState;
270 270
 
271
-  final ValueNotifier<bool> _isCountingDown = ValueNotifier(false);
271
+  ValueNotifier<bool> get recordingState =>
272
+      _recordingState ??= ValueNotifier(false);
273
+
274
+  ValueNotifier<bool>? _isCountingDown;
275
+
276
+  ValueNotifier<bool> get isCountingDown =>
277
+      _isCountingDown ??= ValueNotifier(false);
272 278
 
273 279
   void _setCountController(CountDownTextController controller) {
274 280
     _countController = controller;
@@ -276,16 +282,22 @@ class VideoOperationViewController {
276 282
 
277 283
   void dispose() {
278 284
     _countController?.stopCount();
279
-    _recordingState.dispose();
280
-    _isCountingDown.dispose();
285
+    _recordingState?.dispose();
286
+    _recordingState = null;
287
+    _isCountingDown?.dispose();
288
+    _isCountingDown = null;
281 289
   }
282 290
 
283 291
   void updateRecordingState(bool isRecording) {
284
-    if (_recordingState.value == isRecording) {
292
+    if (recordingState.value == isRecording) {
285 293
       return;
286 294
     }
287
-    _recordingState.value = isRecording;
295
+    recordingState.value = isRecording;
288 296
   }
289 297
 
290
-  bool get isRecording => _recordingState.value;
298
+  bool get isRecording => recordingState.value;
299
+
300
+  bool get isCounting => isCountingDown.value;
301
+
302
+  set isCounting(bool bo) => isCountingDown.value = bo;
291 303
 }

+ 8 - 1
lib/widget/count_down_text.dart

@@ -41,7 +41,9 @@ class _CountDownTextState extends State<CountDownText> {
41 41
 }
42 42
 
43 43
 class CountDownTextController {
44
-  ValueNotifier<String> countText = ValueNotifier("");
44
+  ValueNotifier<String>? _countText;
45
+
46
+  ValueNotifier<String> get countText => _countText ??= ValueNotifier("");
45 47
 
46 48
   void Function(int tick, bool complete)? onCount;
47 49
 
@@ -81,4 +83,9 @@ class CountDownTextController {
81 83
   void _timeUp() {
82 84
     stopCount();
83 85
   }
86
+
87
+  void dispose() {
88
+    _countText?.dispose();
89
+    _countText = null;
90
+  }
84 91
 }

+ 12 - 3
lib/widget/webview_progress_bar.dart

@@ -1,3 +1,4 @@
1
+import 'package:eitc_erm_dental_flutter/funcs.dart';
1 2
 import 'package:flutter/material.dart';
2 3
 
3 4
 ///Webview进度条
@@ -30,9 +31,17 @@ class _WebviewProgressBarState extends State<WebviewProgressBar> {
30 31
   @override
31 32
   void initState() {
32 33
     super.initState();
33
-    widget.controller.progress.addListener(() {
34
-      setState(() {});
35
-    });
34
+    widget.controller.progress.addListener(_onUpdateProgress);
35
+  }
36
+
37
+  void _onUpdateProgress() {
38
+    setState(() {});
39
+  }
40
+
41
+  @override
42
+  void dispose() {
43
+    super.dispose();
44
+    widget.controller.progress.removeListener(_onUpdateProgress);
36 45
   }
37 46
 
38 47
   @override