Christopher Zenzel

Contents

Table of Contents

Share

Building your First Flutter Camera Application

black and gray film camera near printed photos

Have you ever wondered how to build your own Camera application with Flutter?

In this tutorial I will walk you through the basics to create a Camera application with Flutter and Dart for use on your mobile or desktop device. Please notate that not all camera features are supported across each platform.

Starting with Visual Studio Code

Let’s start by opening Visual Studio Code and creating a new Flutter project.

Choose to create a new Empty Application

Create a new Empty Application called DemoCam

Adding the Packages

When ready inside of Visual Studio Code we are going to begin to import the Camera package from Dart.

Navigate to camera | Flutter package (pub.dev) and copy the header to paste in your pubspec.yaml file under dependencies:

camera: ^0.10.5+9

Implementing the Camera Controller

Let’s prepare to implement the CameraController in our main.dart file in the lib folder to start building our Camera app.

Inside the main.dart file add to the top of the file:

late List<CameraDescription> cameraDescription;
late CameraController cameraController;

Inside the main.dart file change main() :

void main() async {
WidgetsFlutterBinding.ensureInitialized();
cameraDescription = await availableCameras();
runApp(const MainApp());
}

Implementing the Views and States

After changing the main.dart file’s main() function begin to initialize the Camera Controller under the screen.

First we must change the way the App is initialized by using Stateful Widgets.

class MainApp extends StatefulWidget
{
const MainApp({super.key});
@override
State<StatefulWidget> createState() {
return _MainAppState();
}
}

Then we must create our actual class to manage the state:

class _MainAppState extends State<MainApp>
{
late CameraController controller;
@override
Widget build(BuildContext context)
{
}
}

Building the Camera Preview

Inside the build function we are going to build out our Application’s user interface and the Camera view for the live camera controller.

@override
Widget build(BuildContext context)
{
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(body:
CameraPreview(controller),
),
);
}

Disposal of Camera Views and Controllers

Implement a dispose function:

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

Implementing State

Implement a initialization of state (initState) function:

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

controller = CameraController(cameraDescription[0],
ResolutionPreset.max,
enableAudio: false);

controller.initialize().then((value) {
setState(() {});
});
}

Running the Project

And drum roll please… We should be now ready to run the essential part of our application which is the Camera with Camera Preview.

To begin let’s just debug this on our computer. We won’t have access to the essentials such as Exposure, Flash, or other camera controls, but we can at least see ourselves in the camera!

When ready from the Terminal run the following command: flutter run and choose your web browser such as Edge or Chrome.

When you launch your browser you should see something similar to this:

Final Results

Click the Allow button and you should then shortly see your Camera present on screen.

You have now successfully completed creating a Flutter based Camera Application with the Dart Camera package!