How to create camera overlay with Flutter

In this Google Flutter code snippet example, we are going to learn How to create camera overlay with flutter

You can copy and adopt this source code example to your flutter project easily without issues.

Add this code to your pubspec.yaml file

  camera: ^0.5.2+1

Create a camera_overlay.dart file (use any name of your choice) and add this code:

 
 import 'dart:async';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';

class CamOverlay extends StatefulWidget {
  @override
  _CamOverlayState createState() => _CamOverlayState();
}

class _CamOverlayState extends State<CamOverlay> {
  CameraController _controller;
  Future<void> _initCamFuture;

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

  _initApp() async {
    final cameras = await availableCameras();
    final firstCam = cameras.first;

    _controller = CameraController(
      firstCam,
      ResolutionPreset.medium,
    );

    _initCamFuture = _controller.initialize();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Camera Overylay")),
      body: FutureBuilder<void>(
        future: _initCamFuture,
        builder: (context, snapshot) {
      
            return Stack(
              alignment: FractionalOffset.center,
              children: <Widget>[
                new Positioned.fill(
                  child: new AspectRatio(
                      aspectRatio: _controller.value.aspectRatio,
                      child: new CameraPreview(_controller)),
                ),
                new Positioned.fill(
                  child: new Opacity(
                      opacity: 0.3,
                      child: Container(
                        height: 200.0,
                        color: Colors.red,
                        child: Center(
                          child: Text("Inducesmile.com", style: TextStyle(fontSize: 20.0, color: Colors.white,)),
                        ),
                      )),
                ),
              ],
            );
          
        },
      ),
    );
  }
}

In your main.dart file (main entry point of the app), add this:

import 'package:inducesmile/camera_overlay.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Code Snippets',
      theme: new ThemeData(primarySwatch: Colors.red),
      home: new CamOverlay(),
    );
  }
}
A visible red overlay on the camera.

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.