image picker plugin for flutter
a flutter plugin for ios and android for picking images from the image library.
ios
add the following keys to your info.plist:
<key>nsphotolibraryusagedescription</key>
<string>使用图片</string>
<key>nscamerausagedescription</key>
<string>照相</string>
<key>nsmicrophoneusagedescription</key>
<string>录音</string>
android
add the following permission to your manifest:
<uses-permission android:name="android.permission.read_external_storage" />
<uses-permission android:name="android.permission.write_external_storage" />
<uses-permission android:name="android.permission.camera" />
api
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:image_picker_flutter/src/model/asset_data.dart';
import 'package:image_picker_flutter/src/page/mul_image_picker_page.dart';
import 'package:image_picker_flutter/src/page/single_image_picker_page.dart';
import 'package:image_picker_flutter/src/utils.dart';
typedef mulcallback = void function(list<assetdata>);
typedef singlecallback = void function(assetdata);
typedef callback = void function(assetdata);
class imagepicker {
imagepicker._();
static debug(bool isdebug) {
utils.isdebug = isdebug;
}
///单选图片
static void singlepicker(
buildcontext context, {
imagepickertype type = imagepickertype.imageandvideo,
language language,
imageprovider placeholder,
widget title,
widget back,
decoration decoration,
color appbarcolor = colors.blue,
singlecallback singlecallback,
}) {
navigator.of(context).push(
materialpageroute(
builder: (context) => singleimagepickerpage(
type: type,
language: language ?? language(),
placeholder: placeholder,
decoration: decoration,
appbarcolor: appbarcolor ?? colors.blue,
title: title,
back: back,
),
),
)..then((data) {
if (data != null && singlecallback != null) {
singlecallback(data);
}
});
}
///多选图片
static void mulpicker(
buildcontext context, {
list<assetdata> data,
imagepickertype type = imagepickertype.imageandvideo,
int limit = 9,
language language,
imageprovider placeholder,
widget title,
widget back,
widget menu,
decoration decoration,
color appbarcolor = colors.blue,
mulcallback mulcallback,
}) {
navigator.of(context).push(
materialpageroute(
builder: (context) => mulimagepickerpage(
selecteddata: data,
type: type,
limit: limit,
appbarcolor: appbarcolor ?? colors.blue,
language: language ?? language(),
placeholder: placeholder,
decoration: decoration,
title: title,
menu: menu,
back: back,
),
),
)..then((data) {
if (data != null && mulcallback != null) {
mulcallback(data);
}
});
}
///拍照返回图片路径
static void takepicture(callback callback) {
utils.takepicture().then((a) {
callback(a);
});
}
///录像返回图片路径
static void takevideo(callback callback) {
utils.takevideo().then((a) {
callback(a);
});
}
}
enum imagepickertype {
onlyimage,
onlyvideo,
imageandvideo,
}
///文字基类
class language {
string get title => "gallery";
string get showtoast => "only ### images can be selected";
}
example about picking images app
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/imagepicker.dart';
import 'package:image_picker/image/assetdataimage.dart';
import 'package:image_picker/model/assetdata.dart';
import 'package:image_picker/utils/utils.dart';
import 'package:permission_handler/permission_handler.dart';
void main() => runapp(materialapp(home: myapp()));
class myapp extends statefulwidget {
@override
_myappstate createstate() => _myappstate();
}
class _myappstate extends state<myapp> {
list<assetdata> _data = [];
@override
void initstate() {
if (platform.isandroid) {
permissionhandler().requestpermissions([permissiongroup.storage]);
}
if (platform.isios) {
permissionhandler().requestpermissions([permissiongroup.photos]);
}
super.initstate();
}
@override
widget build(buildcontext context) {
return scaffold(
appbar: appbar(
backgroundcolor: colors.red,
title: center(
child: text("demo"),
),
),
body: gridview.builder(
padding: edgeinsets.all(8),
griddelegate: slivergriddelegatewithfixedcrossaxiscount(
crossaxiscount: 3,
mainaxisspacing: 8,
crossaxisspacing: 8,
),
itembuilder: (context, index) {
return stack(
alignment: alignmentdirectional.center,
children: <widget>[
image(
image: assetdataimage(_data[index]),
fit: boxfit.cover,
width: double.infinity,
height: double.infinity,
),
iconvideo(_data[index]),
],
);
},
itemcount: _data.length,
),
bottomnavigationbar: container(
color: colors.grey,
height: 48 + mediaquery.of(context).padding.bottom,
padding: edgeinsets.only(bottom: mediaquery.of(context).padding.bottom),
alignment: alignmentdirectional.center,
child: row(
mainaxisalignment: mainaxisalignment.spacearound,
children: <widget>[
rawmaterialbutton(
onpressed: () {
imagepicker.mulpicker(
context,
data: _data,
mulcallback: (data) {
setstate(() {
_data = data;
});
},
);
},
fillcolor: colors.blue,
child: text("mulimagepikcer"),
),
rawmaterialbutton(
onpressed: () {
imagepicker.singlepicker(context, singlecallback: (data) {
setstate(() {
_data
..removewhere((a) => a == data)
..add(data);
});
});
},
fillcolor: colors.blue,
child: text("singleimagepikcer"),
),
],
),
),
);
}
widget iconvideo(assetdata data) {
if (data.isimage) {
return container(
width: 0,
height: 0,
);
}
return icon(
utils.video,
color: colors.blue,
);
}
}
Comments are closed.