插件地址:https://pub.dev/packages/image_picker
- 添加插件
- 添加配置
android無需配置開箱即用,ios還需要配置
info.plist
<key>NSPhotoLibraryUsageDescription</key> <string>應用需要訪問相冊讀取文件</string>
<key>NSCameraUsageDescription</key> <string>應用需要訪問相機拍照</string>
<key>NSMicrophoneUsageDescription</key> <string>應用需要訪問麥克風錄制視頻</string>
- 核心代碼
import 'package:image_picker/image_picker.dart';final ImagePicker picker = ImagePicker();
// 從相冊選擇相機
final XFile? image = await picker.pickImage(source: ImageSource.gallery);
// 拍照
final XFile? photo = await picker.pickImage(source: ImageSource.camera);
// 從相冊選擇視頻
final XFile? galleryVideo =await picker.pickVideo(source: ImageSource.gallery);
// 錄制視頻
final XFile? cameraVideo = await picker.pickVideo(source: ImageSource.camera);
- 完整代碼
import 'dart:io';import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
// 本地視頻播放的邏輯
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';class ImagePickerDemo extends StatefulWidget {const ImagePickerDemo({super.key});State<ImagePickerDemo> createState() => _ImagePickerDemoState();
}class _ImagePickerDemoState extends State<ImagePickerDemo> {final ImagePicker _imagePicker = ImagePicker();XFile? _pickerImage;XFile? _pickerVideo;// 配置加載本地視頻late VideoPlayerController _videoPlayerController;late ChewieController _chewieController;// 拍照void _imagePickerCamera() async {final XFile? image = await _imagePicker.pickImage(source: ImageSource.camera,// 可以不設置,設置上可以避免數據太大maxHeight: 800,maxWidth: 800);if (image != null) {print(image.path);setState(() {_pickerVideo = null;_pickerImage = image;});}}// 相冊選擇圖片void _imagePickerGallery() async {final XFile? image = await _imagePicker.pickImage(source: ImageSource.gallery,// 可以不設置,設置上可以避免數據太大maxHeight: 800,maxWidth: 800);if (image != null) {print(image.path);setState(() {_pickerVideo = null;_pickerImage = image;});} }// 視頻-選擇相機錄制void _videoPickerCamera() async {final XFile? video = await _imagePicker.pickVideo(source: ImageSource.camera);if (video != null) {print(video.path);await _initVideo(File(video.path));setState(() {_pickerImage = null;_pickerVideo = video;});}}// 視頻-選擇相冊void _videoPickerGallery() async {final XFile? video = await _imagePicker.pickVideo(source: ImageSource.gallery);if (video != null) {print(video.path);await _initVideo(File(video.path));setState(() {_pickerImage = null;_pickerVideo = video;});}}// 顯示圖片或者顯示視頻Widget _showImageOrVideoWidget() {if (_pickerImage != null) {return Image.file(File(_pickerImage!.path));} else if (_pickerVideo != null) {return AspectRatio(aspectRatio: _videoPlayerController.value.aspectRatio,child: Chewie(controller: _chewieController));} else {return Text("請選擇圖片或者視頻");}}// 初始化播放器--用于播放本地視頻Future _initVideo(File fileDir) async {_videoPlayerController = VideoPlayerController.file(fileDir);await _videoPlayerController.initialize();_chewieController = ChewieController(videoPlayerController: _videoPlayerController,aspectRatio: _videoPlayerController.value.aspectRatio,autoPlay: true,looping: false);}void dispose() {try {_videoPlayerController.dispose();_chewieController.dispose();} catch (e) {print(e);}super.dispose();}Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("圖片選擇器"),),body: ListView(padding: EdgeInsets.all(10),children: [Center(child: ElevatedButton(onPressed: _imagePickerGallery, child: Text("圖片-選擇相冊"))),Center(child: ElevatedButton(onPressed: _imagePickerCamera, child: Text("圖片-選擇相機"))),Center(child: ElevatedButton(onPressed: _videoPickerGallery, child: Text("視頻-選擇相冊"))),Center(child: ElevatedButton(onPressed: _videoPickerCamera, child: Text("視頻-選擇相機"))),SizedBox(height: 20),// 展示圖片或者視頻_showImageOrVideoWidget()],),);}
}
- 效果圖
- 上傳文件(未測試,先記錄)
_uploadFile(String imagePath) async {var formData = FormData.fromMap({'name': 'wendux', 'age': 25, 'file': await MultipartFile.fromFile(imagePath, filename: 'aaa.png')}); var response = await Dio().post('https://xxx/imgupload', data: formData);print(response); }
}