Image Picker Flutter - Take Image From Camera or Gallery

Last updated Aug 21, 2020


If we are going to create a Flutter application that is accessing the camera to take a picture or using the gallery to pick an image then in this tutorial we are going to learn how to access Camera or Gallery in a flutter application.

In this Image Picker Example, we are going to use ImagePicker dependency to access the camera/gallery.

 

dependencies:

    image_picker:

 

How to capture Images from Camera?

To capture an image from the camera first we need to create ImagePicker instance

This ImagePicker contains method getImage()  will return Image File.

To get from Camera we need to pass the ImageSource.camera parameter to this getImage().

 

Image PIcker Flutter

 

void _openCamera(BuildContext context)  async{
    final pickedFile = await ImagePicker().getImage(
          source: ImageSource.camera ,
          );
          setState(() {
          imageFile = pickedFile;
    });
    
}

 

we don't know how much time will it take to capture the image from the camera, so we are written this method inside the future method. 

similarly to pick an image from the gallery we need to pass ImageSource.gallery as a parameter.

 

void _openGallery(BuildContext context) async{
  final pickedFile = await ImagePicker().getImage(
    source: ImageSource.gallery ,
  );
  setState(() {
    imageFile = pickedFile;
  });

  
}

 

In this example, we are showing the Camera and Gallery options in the Dialog when tapping on Button.

 

Show Dialog In a flutter

With the showDialog() widget we are showing the dialog by returns AlertDialog widget

Future_showChoiceDialog(BuildContext context)
{
  return showDialog(context: context,builder: (BuildContext context){

    return AlertDialog(
      title: Text("Choose option",style: TextStyle(color: Colors.blue),),
      content: SingleChildScrollView(
      child: ListBody(
        children: [
          Divider(height: 1,color: Colors.blue,),
          ListTile(
            onTap: (){
              _openGallery(context);
            },
          title: Text("Gallery"),
            leading: Icon(Icons.account_box,color: Colors.blue,),
      ),

          Divider(height: 1,color: Colors.blue,),
          ListTile(
            onTap: (){
              _openCamera(context);
            },
            title: Text("Camera"),
            leading: Icon(Icons.camera,color: Colors.blue,),
          ),
        ],
      ),
    ),);
  });
}

 

How to close Dialog in flutter?

To close the dialog in flutter we will use Navigator to dismiss the dialog.

Navigator.pop(context);

 

Example  for Pick Image in Flutter


import 'dart:io';

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

class CameraWidget extends StatefulWidget{
  @override
  State createState() {
    // TODO: implement createState
   return CameraWidgetState();
  }

}

class CameraWidgetState extends State{
  PickedFile imageFile;
   Future_showChoiceDialog(BuildContext context)
  {
    return showDialog(context: context,builder: (BuildContext context){

      return AlertDialog(
        title: Text("Choose option",style: TextStyle(color: Colors.blue),),
        content: SingleChildScrollView(
        child: ListBody(
          children: [
            Divider(height: 1,color: Colors.blue,),
            ListTile(
              onTap: (){
                _openGallery(context);
              },
            title: Text("Gallery"),
              leading: Icon(Icons.account_box,color: Colors.blue,),
        ),

            Divider(height: 1,color: Colors.blue,),
            ListTile(
              onTap: (){
                _openCamera(context);
              },
              title: Text("Camera"),
              leading: Icon(Icons.camera,color: Colors.blue,),
            ),
          ],
        ),
      ),);
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return  Scaffold(
        appBar: AppBar(
          title: Text("Pick Image Camera"),
          backgroundColor: Colors.green,
        ),
        body: Center(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Card(
                  child:( imageFile==null)?Text("Choose Image"): Image.file( File(  imageFile.path)),
                ),
                RaisedButton(
                  onPressed: (){
                    _showChoiceDialog(context);
                  },
                  child: Text("Select Image"),

                )
              ],
            ),
          ),
        ),
      );
  }

  void _openGallery(BuildContext context) async{
    final pickedFile = await ImagePicker().getImage(
      source: ImageSource.gallery ,
    );
    setState(() {
      imageFile = pickedFile;
    });

    Navigator.pop(context);
  }

  void _openCamera(BuildContext context)  async{
      final pickedFile = await ImagePicker().getImage(
            source: ImageSource.camera ,
            );
            setState(() {
            imageFile = pickedFile;
      });
      Navigator.pop(context);
  }
}

 

Refer

Custom Rating Dialog, Dialog with TextFiled 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

5041 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions