flutter image_picker 控件的使用 ---选择系统图片、拍照

在pubspec.yaml中添加依赖

dependencies: 
   image_picker: ^0.4.12+1

 

Dart文件如下

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


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

class   MySelectImage extends State{
 List imgList=new List(); 
  Future getImage() async {
     //  
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      imgList.add(image);
    });
  }
  dynamic  getBody() {
    if (showLoadingDialog()) {
      return getProgressDialog();
    } else {
      return getListView();
    }
  }

  bool showLoadingDialog() {
    if (imgList.length == 0) {
      return true;
    }

    return false;
  }

  Center getProgressDialog() {
    return new Center(child: new CircularProgressIndicator());
  }

  ListView getListView()=>new ListView.builder(
      itemCount: imgList.length,
      itemBuilder: (BuildContext context, int position) {
        return Image.file(imgList[position],height: 300);
      }
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Image Picker Example'),
      ),
      body:getBody(),

      floatingActionButton: FloatingActionButton(
        onPressed: getImage,
        tooltip: 'Pick Image',
        child: Icon(Icons.add_a_photo),
      ),
    );
  }
}



 

你可能感兴趣的:(Flutter)