Last active
March 10, 2019 18:26
-
-
Save redbluenat/e501cb8e3bfa44073917f7dd37f9ff57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:camera/camera.dart'; | |
List<CameraDescription> cameras; | |
class CameraWidget extends StatefulWidget { | |
@override | |
CameraState createState() => CameraState(); | |
} | |
class CameraState extends State<CameraWidget> { | |
List<CameraDescription> cameras; | |
CameraController controller; | |
bool isReady = false; | |
@override | |
void initState() { | |
super.initState(); | |
setupCameras(); | |
} | |
Future<void> setupCameras() async { | |
try { | |
cameras = await availableCameras(); | |
controller = new CameraController(cameras[0], ResolutionPreset.medium); | |
await controller.initialize(); | |
} on CameraException catch (_) { | |
setState(() { | |
isReady = false; | |
}); | |
} | |
setState(() { | |
isReady = true; | |
}); | |
} | |
Widget build(BuildContext context) { | |
if (!isReady && !controller.value.isInitialized) { | |
return Container(); | |
} | |
return AspectRatio( | |
aspectRatio: controller.value.aspectRatio, | |
child: CameraPreview(controller)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment