当前位置:   article > 正文

Flutter 图片选取 image_picker_flutter imagepicker

flutter imagepicker

image_picker: Flutter 照片选取插件; 该老版本插件需要兼容 AndroidX, 如果是老版本 可以参照这边的进行配置!

flutter 官方androidx 兼容设置https://flutter.dev/docs/development/androidx-migrationhttps://flutter.dev/docs/development/androidx-migrationIOS 需要配置  .plist  权限 (老版本和新版本都需要)

image_picker | Flutter PackageFlutter plugin for selecting images from the Android and iOS image library, and taking new pictures with the camera.https://pub.flutter-io.cn/packages/image_pickerpubspec.yaml:

  1. dev_dependencies:
  2. flutter_test:
  3. sdk: flutter
  4. image_picker: ^0.8.4+1

 

 

引入插件的头文件
import 'package:image_picker/image_picker.dart';
  1. List<File> imgArray = [];
  2. final ImagePicker _picker = ImagePicker();
  3. Future pickImage({required ImageSource type}) async {
  4. Navigator.pop(context);
  5. var image = await _picker.pickImage(source: type);
  6. // List<File> cacheList = [];
  7. // // for (int i = 0; i < imgArray.length; i++) {
  8. // // cacheList.add(imgArray[i]);
  9. // // }
  10. // cacheList.addAll(imgArray);
  11. // cacheList.add(File(image!.path));
  12. setState(() {
  13. imgArray.add(File(image!.path));
  14. //imgArray = cacheList;
  15. // _image = _image;
  16. // imgArray = imgArray.add(image!.path);
  17. // print('_image.path ${_image!.path}');
  18. });
  19. }

  1. import 'package:flutter/material.dart';
  2. import 'dart:io';
  3. import 'package:image_picker/image_picker.dart';
  4. class PhotoSelect extends StatefulWidget {
  5. @override
  6. State<StatefulWidget> createState() {
  7. // TODO: implement createState
  8. return _PhotoSelectState();
  9. }
  10. }
  11. class _PhotoSelectState extends State<PhotoSelect> {
  12. List<File> imgArray = [];
  13. final ImagePicker _picker = ImagePicker();
  14. Future pickImage({required ImageSource type}) async {
  15. Navigator.pop(context);
  16. var image = await _picker.pickImage(source: type);
  17. // List<File> cacheList = [];
  18. // // for (int i = 0; i < imgArray.length; i++) {
  19. // // cacheList.add(imgArray[i]);
  20. // // }
  21. // cacheList.addAll(imgArray);
  22. // cacheList.add(File(image!.path));
  23. setState(() {
  24. imgArray.add(File(image!.path));
  25. //imgArray = cacheList;
  26. // _image = _image;
  27. // imgArray = imgArray.add(image!.path);
  28. // print('_image.path ${_image!.path}');
  29. });
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. String title = (ModalRoute.of(context)!.settings.arguments as Map)['desc'];
  34. // TODO: implement build
  35. return Scaffold(
  36. appBar: AppBar(
  37. title: Text(title),
  38. ),
  39. body: Stack(
  40. children: [
  41. Positioned(
  42. right: 10,
  43. bottom: 20,
  44. child: SizedBox(
  45. width: 50,
  46. height: 50,
  47. child: ClipOval(
  48. child: Container(
  49. color: Colors.blue,
  50. child: IconButton(
  51. color: Colors.white,
  52. onPressed: _showSheetAction,
  53. icon: Icon(
  54. Icons.photo_camera,
  55. size: 30,
  56. ),
  57. ),
  58. ),
  59. ),
  60. ),
  61. ),
  62. Center(
  63. child: imgArray.length > 0
  64. ? Wrap(
  65. spacing: 5,
  66. runSpacing: 5,
  67. children: imgArray
  68. .map((item) => _imageItem(imagePth: item))
  69. .toList(),
  70. )
  71. : Text('您还没有添加图片'),
  72. )
  73. ],
  74. ),
  75. );
  76. }
  77. Widget _imageItem({required File imagePth}) {
  78. return Stack(
  79. children: [
  80. ClipRRect(
  81. borderRadius: BorderRadius.circular(7),
  82. child: Image.file(
  83. imagePth,
  84. width: 110,
  85. height: 70,
  86. fit: BoxFit.fitWidth,
  87. ),
  88. ),
  89. Positioned(
  90. right: 5,
  91. top: 5,
  92. child: GestureDetector(
  93. onTap: () {
  94. //print('点击了删除');
  95. // List<File> cacheList = [];
  96. // cacheList.addAll(imgArray);
  97. // cacheList.remove(imagePth);
  98. setState(() {
  99. imgArray.remove(imagePth);
  100. });
  101. },
  102. child: ClipOval(
  103. child: Container(
  104. color: Colors.white.withOpacity(0.7),
  105. width: 20,
  106. height: 20,
  107. child: Icon(
  108. Icons.close,
  109. size: 20,
  110. ),
  111. ),
  112. ),
  113. ),
  114. ),
  115. ],
  116. );
  117. }
  118. _showSheetAction() {
  119. showModalBottomSheet(
  120. context: context,
  121. builder: (context) => Container(
  122. height: 120,
  123. child: Column(
  124. children: [
  125. Expanded(
  126. child: FlatButton(
  127. onPressed: () {
  128. pickImage(type: ImageSource.camera);
  129. },
  130. child: Container(
  131. width: double.infinity,
  132. height: double.infinity - 20,
  133. child: Center(
  134. child: Text('拍照'),
  135. ),
  136. )),
  137. ),
  138. Divider(
  139. height: 5,
  140. ),
  141. Expanded(
  142. child: FlatButton(
  143. onPressed: () {
  144. pickImage(type: ImageSource.gallery);
  145. },
  146. child: Container(
  147. width: double.infinity,
  148. height: double.infinity - 20,
  149. color: Colors.white24,
  150. child: Center(
  151. child: Text('从相册选取'),
  152. ),
  153. )),
  154. )
  155. ],
  156. )));
  157. }
  158. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/586289
推荐阅读
相关标签
  

闽ICP备14008679号