Camera Access in Flutter with Example

How to access the device's camera in Flutter using the camera package:

Getting Started

Create a new Flutter project in your preferred IDE.

Add the camera package to your pubspec.yaml file:

dependencies:
  camera: ^0.10.0+2

Import the package in your Dart code:

import 'package:camera/camera.dart';

Accessing the Camera

To access the device's camera in Flutter, follow these steps:

Define a CameraController variable in your stateful widget:

CameraController _controller;

Define a boolean variable to keep track of whether the camera is initialized:

bool _isInitialized = false;

Override the initState method to initialize the camera:

@override
void initState() {
  super.initState();
  _initializeCamera();
}

Define a private _initializeCamera method to set up the camera:

Future<void> _initializeCamera() async {
  final cameras = await availableCameras();
  final firstCamera = cameras.first;
  _controller = CameraController(
    firstCamera,
    ResolutionPreset.medium,
  );
  await _controller.initialize();
  setState(() {
    _isInitialized = true;
  });
}

Override the dispose method to dispose of the camera controller:

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

Wrap your camera preview widget in a Stack and position it in the top left corner:

Stack(
  children: <Widget>[
    Positioned(
      top: 0.0,
      left: 0.0,
      right: 0.0,
      bottom: 0.0,
      child: _isInitialized
          ? CameraPreview(_controller)
          : Container(),
    ),
  ],
)

Add a floating action button to take a picture:

FloatingActionButton(
  onPressed: () async {
    try {
      final image = await _controller.takePicture();
      // Do something with the image
    } catch (e) {
      print(e);
    }
  },
  child: Icon(Icons.camera),
)

That's it! You can now access the device's camera in your Flutter app. The full code for this example can be found below:

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Camera Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CameraExample(),
    );
  }
}
class CameraExample extends StatefulWidget {
  @override
  _CameraExampleState createState() => _CameraExampleState();
}
class _CameraExampleState extends State<CameraExample> {
  CameraController _controller;
  bool _isInitialized = false;
  @override
  void initState() {
    super.initState();
    _initializeCamera();
  }
  Future<void> _initializeCamera() async {
    final cameras = await availableCameras();
    final firstCamera = cameras.first;
    _controller = CameraController(
      firstCamera,
      ResolutionPreset.medium,
    );
    await _controller.initialize();
    setState(() {
      _isInitialized = true;
    });
  }
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context