Skip to content

Commit

Permalink
Merge pull request #6 from elias8/master
Browse files Browse the repository at this point in the history
fix: Handle all cases when a controller should be initialized and disposed
  • Loading branch information
juliansteenbakker authored Feb 21, 2022
2 parents 78cc11d + 2872265 commit e131d9d
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions lib/src/mobile_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ class MobileScanner extends StatefulWidget {
final BoxFit fit;

/// Create a [MobileScanner] with a [controller], the [controller] must has been initialized.
const MobileScanner(
{Key? key, this.onDetect, this.controller, this.fit = BoxFit.cover})
: assert((controller != null)),
const MobileScanner({
Key? key,
this.onDetect,
this.controller,
this.fit = BoxFit.cover,
}) : assert((controller != null)),
super(key: key);

@override
Expand All @@ -43,11 +46,7 @@ class _MobileScannerState extends State<MobileScanner>
@override
void initState() {
super.initState();
if (widget.controller == null) {
controller = MobileScannerController();
} else {
controller = widget.controller!;
}
controller = widget.controller ?? MobileScannerController();
}

@override
Expand Down Expand Up @@ -96,9 +95,26 @@ class _MobileScannerState extends State<MobileScanner>
});
}

@override
void didUpdateWidget(covariant MobileScanner oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.controller == null) {
if (widget.controller != null) {
controller.dispose();
controller = widget.controller!;
}
} else {
if (widget.controller == null) {
controller = MobileScannerController();
} else if (oldWidget.controller != widget.controller) {
controller = widget.controller!;
}
}
}

@override
void dispose() {
controller.dispose();
if (widget.controller == null) controller.dispose();
super.dispose();
}
}

0 comments on commit e131d9d

Please sign in to comment.