点击选中图片,底部弹窗让用户选择使用相册还是相机,用户选中选项后,跳转到对应的相册或者相机功能,结果将图片显示出来

image_picker: ^0.6.1+4

iOS使用image_picker需要在info.plist中添加3个字符串字段

   Privacy - Photo Library Usage Description
Privacy - Camera Usage Description
Privacy - Microphone Usage Description

相关代码

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart'; class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
} class _HomePageState extends State<HomePage> {
File _image;
File _image2; Future getImage(ImageSource source, int type) async {
var image = await ImagePicker.pickImage(source: source);
setState(() {
if (type == 0) {
_image = image;
} else {
_image2 = image;
}
});
} @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('aaaa'),
),
body: Center(
child: Column(
// crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: Container(
width: 100,
height: 100,
color: Colors.blue,
child: _image == null
? Text('No image selected.')
: Image.file(_image,fit: BoxFit.cover,),
),
onTap: () {
//点击选取图片
// getImage(0);
_showSelectionDialog(context,0);
},
),
GestureDetector(
child: Container(
width: 100,
height: 100,
color: Colors.orange,
child: _image2 == null
? Text('No image selected.')
: Image.file(_image2,fit: BoxFit.cover),
),
onTap: () {
_showSelectionDialog(context,1);
// getImage(1); },
),
],
),
],
)),
);
} void _showSelectionDialog(BuildContext context,int type) {
showModalBottomSheet(
context: context,
isScrollControlled: false,
builder: (ctx) {
return Container(
color: Colors.grey,
height: 130,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
GestureDetector(
child: _itemCreat(context, '相机'),
onTap: (){
print('选中相机');
Navigator.pop(context);
getImage(ImageSource.camera,type);
},
),
GestureDetector(
child: _itemCreat(context, '相册'),
onTap: (){
print('选中相册');
Navigator.pop(context);
getImage(ImageSource.gallery,type);
},
),
GestureDetector(
child: Padding(
padding: EdgeInsets.only(top: 10),
child: _itemCreat(context, '取消'),
),
onTap: (){
Navigator.pop(context);
},
)
],
),
);
},
);
} Widget _itemCreat(BuildContext context, String title) {
return Container(
color: Colors.white,
height: 40,
width: MediaQuery.of(context).size.width,
child: Center(
child: Text(
title,
style: TextStyle(fontSize: 16, color: Colors.black),
textAlign: TextAlign.center,
),
),
);
}
}

flutter image_picker的更多相关文章

  1. flutter image_picker使用照相机

    dependencies: image_picker: ^0.4.12+1 最新的^0.5+9编译无法通过 import 'dart:io'; import 'dart:async'; import ...

  2. 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传

    一.Flutter image_picker 实现相机拍照和相册选择   https://pub.dev/packages/image_picker   二.Flutter 上传图片到服务器   ht ...

  3. flutter 上传图片 image_picker 的使用

    Github地址: https://github.com/flutter/plugins/tree/master/packages/image_picker packages地址: https://p ...

  4. 【译】使用 Flutter 实现跨平台移动端开发

    作者: Mike Bluestein   | 原文地址:[https://www.smashingmagazine.com/2018/06/google-flutter-mobile-developm ...

  5. iOS开发者学习Flutter

    Flutter for iOS 开发者 本文档适用那些希望将现有 iOS 经验应用于 Flutter 的开发者.如果你拥有 iOS 开发基础,那么你可以使用这篇文档开始学习 Flutter 的开发. ...

  6. flutter插件汇总2

    作者:知乎用户链接:https://www.zhihu.com/question/307594373/answer/568969429来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  7. flutter插件汇总

    audio_recorder: any #录音.播放 flutter_sound: ^#录音 dropdown_menu: ^#下拉菜单 simple_permissions:#权限获取 easy_a ...

  8. flutter 常用插件

    audio_recorder: any #录音.播放 flutter_sound: ^1.1.5#录音 dropdown_menu: ^1.1.0#下拉菜单 simple_permissions:#权 ...

  9. Flutter 相机定制

    Flutter中与硬件相关的部分,一直都挺蛋疼的.方案基本上有两种,自己写,或者等出相关的库. 最近做的一个项目中,需要对相机做定制.有过相关模块开发经验的,就知道这种需求并不简单,况且是这种跨平台解 ...

随机推荐

  1. 31、NTP时间服务器

    1.NTP简介 NTP服务器顾名思义就是时间同步服务器(Network Time Protocol),Linux下的ntp服务器配置相对来说都比较容易,但在Linux下有一个弊端,不同时区或者说是时间 ...

  2. linux上如何安装postgresql

    安装对应的postgresql的yum源 rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_64 ...

  3. Charles中使用Map Local提高测试效率

    书接上回,上次说到Charles中可以使用修改返回值来模拟接口返回,这次我们来说一下Charles中另外一个强大的功能. 我们用手机连接Charles,具体可以参考上一篇<借助Charles来测 ...

  4. oracle监听启动很慢

    TNS-12531: TNS:cannot allocate memory 首先查看内存,free -m 发现当前的空闲内存还有很多,那就不是内存不足的问题 想到之前重启过数据库服务器,查看主机名ho ...

  5. tarjan算法比较详细的讲解&&tarjan常见疑难解答&&洛谷P2002 消息扩散题解

    因为有大佬写的比我更长更具体,所以我也就写写总结一下了 引入: 众所周知,很多图中有个东西名叫环. 对于这个东西很多算法都很头疼.(suchas 迪杰斯特拉) 更深层:环属于强联通分量(strongl ...

  6. 使用RedisTemplate的操作类访问Redis(转载)

    原文地址:http://www.cnblogs.com/luochengqiuse/p/4641256.html private ValueOperations<K, V> valueOp ...

  7. 兼容系列-IE678的兼容

    1. 最简单的CSS Hack 区分 IE6 . IE7 .IE8 css .color{ background-color: #CC00FF; /*所有浏览器都会显示为紫色*/ background ...

  8. JAVA基础编程之打印99乘法表

    需求:打印9*9乘法表 技术考核: 1.for嵌套循环 代码: // 打印99乘法表 public static void print99Table() { System.out.println(&q ...

  9. 使用 Capistrano 部署总结

    使用 Capistrano 部署总结 2014年6月27日 admin发表评论阅读评论 简介 Capistrano 是一个 Ruby 程序,它提供高级的工具集来部署你的 Web应用到服务器上.Capi ...

  10. C# 扩展方法——去重(Distinct)

    其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html IEnumerable的Distinct扩展方法,当集合元素为对象时,可用于元素对 ...