Flutter开发篇--拍照或选择图片

小萌是做iOS开发的,虽然是flutter开发,但是xcode还是需要配置的,

Flutter开发篇--拍照或选择图片_第1张图片

在这个info.plist中需要配置

NSPhotoLibraryUsageDescription

Example usage description

NSCameraUsageDescription

Example usage description

配置完成之后就可以了,

下面是完整代码:

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

class AuthenticationId extends StatefulWidget {
  @override
  _AuthenticationIdState createState() => _AuthenticationIdState();
}


class _AuthenticationIdState extends State{

  var imgPath;
  /*拍照*/
  takePhoto() async {
    var image = await ImagePicker.pickImage(source: ImageSource.camera);

    setState(() {
      imgPath = image;
    });
  }

  /*相册*/
  openGallery() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      imgPath = image;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("认证"),
          elevation: 0.0,
        ),
        body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                  child: imgPath == null ? Text('请选择拍照'):Image.file(imgPath),
              ),
              RaisedButton(
                onPressed: takePhoto,
                child: Text("拍照"),
              ),
              RaisedButton(
                onPressed: openGallery,
                child: Text("选择照片"),
              ),
            ],
          ),
        ));
  }
}

 

你可能感兴趣的:(Flutter)