Showing
1 changed file
with
11 additions
and
43 deletions
| @@ -51,16 +51,7 @@ class _MobileScannerState extends State<MobileScanner> | @@ -51,16 +51,7 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 51 | StreamSubscription<BarcodeCapture>? _barcodesSubscription; | 51 | StreamSubscription<BarcodeCapture>? _barcodesSubscription; |
| 52 | 52 | ||
| 53 | /// The internally managed controller. | 53 | /// The internally managed controller. |
| 54 | - MobileScannerController? _controller; | ||
| 55 | - | ||
| 56 | - /// Get the effective controller. | ||
| 57 | - MobileScannerController get _effectiveController { | ||
| 58 | - if (widget.controller != null) { | ||
| 59 | - return widget.controller!; | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - return _controller!; | ||
| 63 | - } | 54 | + late MobileScannerController _controller; |
| 64 | 55 | ||
| 65 | /// Whether the controller should resume | 56 | /// Whether the controller should resume |
| 66 | /// when the application comes back to the foreground. | 57 | /// when the application comes back to the foreground. |
| @@ -80,40 +71,35 @@ class _MobileScannerState extends State<MobileScanner> | @@ -80,40 +71,35 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 80 | void initState() { | 71 | void initState() { |
| 81 | super.initState(); | 72 | super.initState(); |
| 82 | WidgetsBinding.instance.addObserver(this); | 73 | WidgetsBinding.instance.addObserver(this); |
| 83 | - | ||
| 84 | - if (widget.controller == null) { | ||
| 85 | - _controller = MobileScannerController(); | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - _barcodesSubscription = _effectiveController.barcodes.listen( | 74 | + _controller = widget.controller ?? MobileScannerController(); |
| 75 | + | ||
| 76 | + _barcodesSubscription = _controller.barcodes.listen( | ||
| 89 | widget.onDetect, | 77 | widget.onDetect, |
| 90 | ); | 78 | ); |
| 91 | 79 | ||
| 92 | - final internalController = _controller; | ||
| 93 | - | ||
| 94 | - if (internalController != null && !internalController.isStarting) { | ||
| 95 | - _startScanner(internalController); | 80 | + if (!_controller.isStarting) { |
| 81 | + _startScanner(_controller); | ||
| 96 | } | 82 | } |
| 97 | } | 83 | } |
| 98 | 84 | ||
| 99 | @override | 85 | @override |
| 100 | void didChangeAppLifecycleState(AppLifecycleState state) { | 86 | void didChangeAppLifecycleState(AppLifecycleState state) { |
| 101 | // App state changed before the controller was initialized. | 87 | // App state changed before the controller was initialized. |
| 102 | - if (_effectiveController.isStarting) { | 88 | + if (_controller.isStarting) { |
| 103 | return; | 89 | return; |
| 104 | } | 90 | } |
| 105 | 91 | ||
| 106 | switch (state) { | 92 | switch (state) { |
| 107 | case AppLifecycleState.resumed: | 93 | case AppLifecycleState.resumed: |
| 108 | _resumeFromBackground = false; | 94 | _resumeFromBackground = false; |
| 109 | - _startScanner(_effectiveController); | 95 | + _startScanner(_controller); |
| 110 | break; | 96 | break; |
| 111 | case AppLifecycleState.paused: | 97 | case AppLifecycleState.paused: |
| 112 | _resumeFromBackground = true; | 98 | _resumeFromBackground = true; |
| 113 | break; | 99 | break; |
| 114 | case AppLifecycleState.inactive: | 100 | case AppLifecycleState.inactive: |
| 115 | if (!_resumeFromBackground) { | 101 | if (!_resumeFromBackground) { |
| 116 | - _effectiveController.stop(); | 102 | + _controller.stop(); |
| 117 | } | 103 | } |
| 118 | break; | 104 | break; |
| 119 | case AppLifecycleState.detached: | 105 | case AppLifecycleState.detached: |
| @@ -122,27 +108,9 @@ class _MobileScannerState extends State<MobileScanner> | @@ -122,27 +108,9 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 122 | } | 108 | } |
| 123 | 109 | ||
| 124 | @override | 110 | @override |
| 125 | - void didUpdateWidget(covariant MobileScanner oldWidget) { | ||
| 126 | - super.didUpdateWidget(oldWidget); | ||
| 127 | - | ||
| 128 | - if (oldWidget.controller == null) { | ||
| 129 | - if (widget.controller != null) { | ||
| 130 | - _controller?.dispose(); | ||
| 131 | - _controller = widget.controller; | ||
| 132 | - } | ||
| 133 | - } else { | ||
| 134 | - if (widget.controller == null) { | ||
| 135 | - _controller = MobileScannerController(); | ||
| 136 | - } else if (oldWidget.controller != widget.controller) { | ||
| 137 | - _controller = widget.controller; | ||
| 138 | - } | ||
| 139 | - } | ||
| 140 | - } | ||
| 141 | - | ||
| 142 | - @override | ||
| 143 | Widget build(BuildContext context) { | 111 | Widget build(BuildContext context) { |
| 144 | return ValueListenableBuilder<MobileScannerArguments?>( | 112 | return ValueListenableBuilder<MobileScannerArguments?>( |
| 145 | - valueListenable: _effectiveController.startArguments, | 113 | + valueListenable: _controller.startArguments, |
| 146 | builder: (context, value, child) { | 114 | builder: (context, value, child) { |
| 147 | if (value == null) { | 115 | if (value == null) { |
| 148 | return widget.placeholderBuilder?.call(context, child) ?? | 116 | return widget.placeholderBuilder?.call(context, child) ?? |
| @@ -176,7 +144,7 @@ class _MobileScannerState extends State<MobileScanner> | @@ -176,7 +144,7 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 176 | void dispose() { | 144 | void dispose() { |
| 177 | WidgetsBinding.instance.removeObserver(this); | 145 | WidgetsBinding.instance.removeObserver(this); |
| 178 | _barcodesSubscription?.cancel(); | 146 | _barcodesSubscription?.cancel(); |
| 179 | - _controller?.dispose(); | 147 | + _controller.dispose(); |
| 180 | super.dispose(); | 148 | super.dispose(); |
| 181 | } | 149 | } |
| 182 | } | 150 | } |
-
Please register or login to post a comment