Image Picker In Flutter || Amazevalley

In this tutorial you will learn how to pick the image from gallery and capture from camera in flutter. You can explore more it on image_picker . Let take a look on the code first of all we will write the function for gallery


Demo :


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

void main() {
  runApp(MyApp());}

class MyApp extends StatelessWidget {
  @override  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Image Picker Demo',      home: MyHomePage(title: 'Image Picker Example'),      debugShowCheckedModeBanner: false,    );  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override  _MyHomePageState createState() => _MyHomePageState();}

class _MyHomePageState extends State<MyHomePage> {
  File _image;
  Future getImageFromCam() async { // for camera    var image = await ImagePicker.pickImage(source: ImageSource.camera);    setState(() {
      _image = image;    });  }

  Future getImageFromGallery() async {// for gallery    var image = await ImagePicker.pickImage(source: ImageSource.gallery);    setState(() {
      _image = image;    });  }

  @override  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Image Picker Example'),        ),        body: ListView(
          children: <Widget>[
            Container(
              width: MediaQuery.of(context).size.width,              height: 200.0,              child: Center(
                child: _image == null                    ? Text('No image selected.')
                    : Image.file(_image),              ),            ),            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,              children: <Widget>[
                FloatingActionButton(
                  onPressed: getImageFromCam,                  tooltip: 'Pick Image',                  child: Icon(Icons.add_a_photo),                ),                FloatingActionButton(
                  onPressed: getImageFromGallery,                  tooltip: 'Pick Image',                  child: Icon(Icons.wallpaper),                ),              ],            ),          ],
)
); } }

dependencies: image_picker:

I hope this article give you good information about how to get image from gallery  or take the image from camera. If you have any query do comment below thanks Flutter Developers

--------------------

Comments

Popular posts from this blog

Multi Image Picker In Flutter || Amazevalley

Listview sort by name in Flutter || Amazevalley