Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2021 16:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kinmanlam/5755c821413844456e3033421e947772 to your computer and use it in GitHub Desktop.
A refactored camera widget based on the "Take a Picture Using the Camera" cookbook found on For full walkthru see:
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'dart:io';
// A screen that allows users to take a picture using a given camera.
class TakePictureScreen extends StatefulWidget {
const TakePictureScreen({
Key? key,
}) : super(key: key);
TakePictureScreenState createState() => TakePictureScreenState();
class TakePictureScreenState extends State<TakePictureScreen> {
late CameraController _controller;
void dispose() {
// Dispose of the controller when the widget is disposed.
Widget build(BuildContext context) {
// Fill this out in the next steps.
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
future: initCamera(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return CameraPreview(_controller);
} else {
// Otherwise, display a loading indicator.
return const Center(child: CircularProgressIndicator());
child: FloatingActionButton(
// Provide an onPressed callback.
onPressed: () async {
// Take the Picture in a try / catch block. If anything goes wrong,
// catch the error.
try {
// Ensure that the camera is initialized.
await initCamera();
// Attempt to take a picture and get the file `image`
// where it was saved.
final image = await _controller.takePicture();
// If the picture was taken, display it on a new screen.
await Navigator.of(context).push(
builder: (context) => DisplayPictureScreen(
// Pass the automatically generated path to
// the DisplayPictureScreen widget.
imagePath: image.path,
} catch (e) {
// If an error occurs, log the error to the console.
child: const Icon(Icons.camera_alt),
Future<void> initCamera() async {
final cameras = await availableCameras();
// Get a specific camera from the list of available cameras.
final firstCamera = cameras.first;
// To display the current output from the Camera,
// create a CameraController.
_controller = CameraController(
// Get a specific camera from the list of available cameras.
// Define the resolution to use.
// Next, initialize the controller. This returns a Future.
await _controller.initialize();
// A widget that displays the picture taken by the user.
class DisplayPictureScreen extends StatelessWidget {
final String imagePath;
const DisplayPictureScreen({Key? key, required this.imagePath})
: super(key: key);
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Display the Picture')),
// The image is stored as a file on the device. Use the `Image.file`
// constructor with the given path to display the image.
body: Image.file(File(imagePath)),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment