我写了一个flutter插件,显示摄像头预览并扫描条形码 . 我有一个 Widget
,显示 CameraPreview
,当检测到条形码时导航到新的 Route
.
Problem: 当我将新路线( SearchProductPage
)推到导航堆栈时, CameraController
继续检测条形码 . 当 ScanPage
从屏幕上移除时,我需要在我的 CameraController
上调用 stop()
. 当用户返回 ScanPage
时,我需要再次调用 start()
.
What I tried: CameraController
实现 WidgetsBindingObserver
并对 didChangeAppLifecycleState()
作出反应 . 当我按下主页按钮时,这可以很好地工作,但是当我将新的 Route
推到导航堆栈时则不行 .
Question: iOS上的 viewDidAppear()
和 viewWillDisappear()
或者Android上的 onPause()
和 onResume()
是否等同于Flutter中的 Widgets
?如果没有,我如何启动和停止 CameraController
,以便当另一个Widget位于导航堆栈顶部时,它会停止扫描条形码?
class ScanPage extends StatefulWidget {
ScanPage({ Key key} ) : super(key: key);
@override
_ScanPageState createState() => new _ScanPageState();
}
class _ScanPageState extends State<ScanPage> {
//implements WidgetsBindingObserver
CameraController controller;
@override
void initState() {
controller = new CameraController(this.didDetectBarcode);
WidgetsBinding.instance.addObserver(controller);
controller.initialize().then((_) {
if (!mounted) {
return;
}
setState(() {});
});
}
//navigate to new page
void didDetectBarcode(String barcode) {
Navigator.of(context).push(
new MaterialPageRoute(
builder: (BuildContext buildContext) {
return new SearchProductPage(barcode);
},
)
);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(controller);
controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (!controller.value.initialized) {
return new Center(
child: new Text("Lade Barcodescanner..."),
);
}
return new CameraPreview(controller);
}
}
编辑:
/// Controls a device camera.
///
///
/// Before using a [CameraController] a call to [initialize] must complete.
///
/// To show the camera preview on the screen use a [CameraPreview] widget.
class CameraController extends ValueNotifier<CameraValue> with WidgetsBindingObserver {
int _textureId;
bool _disposed = false;
Completer<Null> _creatingCompleter;
BarcodeHandler handler;
CameraController(this.handler) : super(const CameraValue.uninitialized());
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch(state){
case AppLifecycleState.inactive:
print("--inactive--");
break;
case AppLifecycleState.paused:
print("--paused--");
stop();
break;
case AppLifecycleState.resumed:
print("--resumed--");
start();
break;
case AppLifecycleState.suspending:
print("--suspending--");
dispose();
break;
}
}
/// Initializes the camera on the device.
Future<Null> initialize() async {
if (_disposed) {
return;
}
try {
_creatingCompleter = new Completer<Null>();
_textureId = await BarcodeScanner.initCamera();
print("TextureId: $_textureId");
value = value.copyWith(
initialized: true,
);
_applyStartStop();
} on PlatformException catch (e) {
value = value.copyWith(errorDescription: e.message);
throw new CameraException(e.code, e.message);
}
BarcodeScanner._channel.setMethodCallHandler((MethodCall call){
if(call.method == "barcodeDetected"){
String barcode = call.arguments;
debounce(2500, this.handler, [barcode]);
}
});
_creatingCompleter.complete(null);
}
void _applyStartStop() {
if (value.initialized && !_disposed) {
if (value.isStarted) {
BarcodeScanner.startCamera();
} else {
BarcodeScanner.stopCamera();
}
}
}
/// Starts the preview.
///
/// If called before [initialize] it will take effect just after
/// initialization is done.
void start() {
value = value.copyWith(isStarted: true);
_applyStartStop();
}
/// Stops the preview.
///
/// If called before [initialize] it will take effect just after
/// initialization is done.
void stop() {
value = value.copyWith(isStarted: false);
_applyStartStop();
}
/// Releases the resources of this camera.
@override
Future<Null> dispose() {
if (_disposed) {
return new Future<Null>.value(null);
}
_disposed = true;
super.dispose();
if (_creatingCompleter == null) {
return new Future<Null>.value(null);
} else {
return _creatingCompleter.future.then((_) async {
BarcodeScanner._channel.setMethodCallHandler(null);
await BarcodeScanner.disposeCamera();
});
}
}
}
2 回答
在调用
pop()
之前,我在导航到另一页并重新启动之前最终停止了controller
.另一个解决方案是设置
route
的maintainState
属性,将ScanPage
打开为false
.也许您可以覆盖窗口小部件的
dispose
方法并让控制器停在其中 . AFAIK,这将是一个很好的方式来处理它,因为每次你处理小部件时它都会停止它,所以你不必在启动或停止相机时自己保持标签 .顺便说一句,我需要一个带有实时预览的条形码/ QR码扫描仪 . 你介意在git(或zip)上分享你的插件吗?