Flutter

Flutter,第1张

Flutter

具有类似_cameraOn = true的变量。如果为true,则显示CameraPreview,否则为false。导航到另一个屏幕时,将其设置为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"),    );  }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4903907.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存