Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:camera/camera.dart';
- import 'package:flutter/services.dart';
- class CameraView extends StatefulWidget {
- @override
- _CameraViewState createState() => _CameraViewState();
- }
- class _CameraViewState extends State<CameraView> {
- CameraController _cameraController;
- bool _isLoading;
- void _initializeCamera() async {
- setState(() {
- _isLoading = true;
- });
- final List<CameraDescription> cameras = await availableCameras();
- final CameraDescription description = cameras.firstWhere(
- (camera) => camera.lensDirection == CameraLensDirection.back);
- _cameraController = CameraController(
- description,
- defaultTargetPlatform == TargetPlatform.iOS
- ? ResolutionPreset.low
- : ResolutionPreset.medium,
- enableAudio: false,
- );
- await _cameraController.initialize();
- setState(() {
- _isLoading = false;
- });
- }
- @override
- void initState() {
- super.initState();
- SystemChrome.setEnabledSystemUIOverlays(<SystemUiOverlay>[]);
- SystemChrome.setPreferredOrientations(
- <DeviceOrientation>[DeviceOrientation.landscapeLeft],
- );
- _initializeCamera();
- }
- @override
- void dispose() {
- _cameraController.dispose().then((_) {
- // Caso precise desativare alguma coisa interno na camera
- });
- SystemChrome.setPreferredOrientations(<DeviceOrientation>[]);
- SystemChrome.setEnabledSystemUIOverlays(<SystemUiOverlay>[
- SystemUiOverlay.top,
- SystemUiOverlay.bottom,
- ]);
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- final Size querySizes = MediaQuery.of(context).size;
- final double captureHeight = 144;
- final double vSpacing = (querySizes.height - captureHeight) / 2;
- final double hSpacing = 30;
- final Color overlayColor = Colors.green.withOpacity(0.3);
- return Material(
- child: _isLoading
- ? Center(
- child: Text(
- "Iniciando Câmera...",
- style: TextStyle(
- color: Colors.green,
- fontSize: 30,
- ),
- ),
- )
- : Stack(
- alignment: FractionalOffset.center,
- children: <Widget>[
- Positioned.fill(
- child: AspectRatio(
- aspectRatio: _cameraController.value.aspectRatio,
- child: CameraPreview(_cameraController),
- ),
- ),
- Column(
- children: <Widget>[
- Container(
- height: vSpacing,
- width: double.infinity,
- color: overlayColor,
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Container(
- width: hSpacing,
- height: captureHeight,
- color: overlayColor,
- ),
- Expanded(
- child: Container(
- alignment: Alignment.center,
- height: captureHeight,
- decoration: BoxDecoration(
- border: Border.all(
- width: 2,
- color: Colors.white.withGreen(255),
- ),
- // borderRadius: BorderRadius.circular(6),
- color: Colors.white.withOpacity(0),
- ),
- ),
- ),
- Container(
- width: hSpacing,
- height: captureHeight,
- color: overlayColor,
- ),
- ],
- ),
- Container(
- height: vSpacing,
- width: double.infinity,
- color: overlayColor,
- ),
- ],
- ),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment