flutter拍照和选择图片

flutter拍照和选择图片

一、导入

image_picker: ^0.6.1+4
qrscan: ^0.2.17

二、代码

import 'dart:io';
import 'package:image_picker/image_picker.dart';
import 'package:qrscan/qrscan.dart' as scanner;

class ImagePickerUtil{
  //选图片
  static Future<File> openGallery() async {
    File image = await ImagePicker.pickImage(source: ImageSource.gallery);
    return image;
  }

  /*拍照*/
    static Future takePhoto() async {
      File image = await ImagePicker.pickImage(source: ImageSource.camera);
      return image;
  }

  /*扫码*/
  static Future<String> scanQR() async {
    String barcode = await scanner.scan();
    return barcode;
  }

}

三、使用

File headImage;

-------------
//获取图片
void _SelectPhoto(){
    ImagePickerUtil.openGallery().then((imgFile){
      setState(() {
        headImage = imgFile;
      });
    });
  }


---------------
//在布局中使用
Container(
    height: 120,
    decoration: BoxDecoration(
        shape: BoxShape.circle,
        image: DecorationImage(
        fit: BoxFit.cover,
        image:headImage == null ?  AssetImage(UIDataUtils.xxx) : FileImage(headImage),
   ),
  ),
),

你可能感兴趣的:(flutter拍照和选择图片)