gpt4 book ai didi

android - Flutter - 当用户移动到其他(预览)屏幕时如何正确暂停相机?

转载 作者:IT王子 更新时间:2023-10-29 07:17:00 25 4
gpt4 key购买 nike

当我移动到导航树上的另一个屏幕时,我需要暂停相机以节省电池和性能。

我尝试dispose() cameraController,但是当它从另一个屏幕返回时,flutter 不会重新初始化状态(虽然很明显)。

我使用相机的主要代码:

 @override
void initState() {
super.initState();
availableCameras().then((cameras) {
setState(() {
_firstCamera = cameras.first;
_controller = CameraController(_firstCamera, ResolutionPreset.high);

_initializeControllerFuture = _controller.initialize();
});
});
}

@override
void dispose() {
_controller?.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: Stack(
children: <Widget>[
FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {

if (snapshot.connectionState == ConnectionState.done) {
return Stack(
alignment: FractionalOffset.center,
children: <Widget>[
new Positioned.fill(
child: _getCameraPreview(context),
),
...
],
);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
Align(
alignment: Alignment.bottomCenter,
child: BottomAppBar(
color: Color.fromARGB(0, 0, 0, 0),
child: _getBottomAppBarRow(context),
),
),
],
),
);
}

_getCameraPreview(BuildContext context) {
final size = MediaQuery.of(context).size;
final deviceRatio = size.width / size.height;
return Transform.scale(
scale: _controller.value.aspectRatio / deviceRatio,
child: Center(
child: AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: CameraPreview(_controller),
),
),
);
}

最佳答案

有一个像 _cameraOn = true 这样的变量。为真时显示 CameraPreview,为假时不显示。导航到另一个屏幕时将其设置为 false

您可以在单独的小部件中拥有与相机相关的功能。因此,每次显示时都会对其进行初始化,不显示时会对其进行处理。

一个简单的工作示例

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';

List<CameraDescription> cameras;

Future<void> main() async {
cameras = await availableCameras();
runApp(MaterialApp(
home: CameraApp(),
));
}

class CameraApp extends StatefulWidget {
@override
_CameraAppState createState() => _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
bool _cameraOn = true;

@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
child: _cameraOn ? Camera() : Container(),
),
FlatButton(
onPressed: () {
setState(() {
_cameraOn = false;
});
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => Post())).then((res) {
setState(() {
_cameraOn = true;
});
}).catchError((err) {
print(err);
});
},
child: Text("NEXT PAGE"),
),
],
),
);
}
}

class Camera extends StatefulWidget {
@override
_CameraState createState() => _CameraState();
}

class _CameraState extends State<Camera> {
CameraController controller;

@override
void initState() {
super.initState();
controller = CameraController(cameras[0], ResolutionPreset.medium);
controller.initialize().then((_) {
if (!mounted) {
return;
}
setState(() {});
});
}

@override
Widget build(BuildContext context) {
if (!controller.value.isInitialized) {
return Container();
}

return AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller),
);
}

@override
void dispose() {
controller?.dispose();
super.dispose();
}
}

class Post extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text("Post"),
);
}
}

关于android - Flutter - 当用户移动到其他(预览)屏幕时如何正确暂停相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57071817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com