Skip to content

Commit

Permalink
[image_picker] Update image_picker to 1.0.1 (flutter-tizen#600)
Browse files Browse the repository at this point in the history
Co-authored-by: JunsuChoi <[email protected]>
  • Loading branch information
Swanseo0 and JSUYA authored Jul 28, 2023
1 parent 246d6b0 commit 5736eeb
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 86 deletions.
4 changes: 3 additions & 1 deletion packages/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 2.3.0

* Update image_picker to 1.0.1.
* Update image_picker_platform_interface to 2.8.0.
* Increase the minimum Flutter version to 3.3.
* Update example app pubspec.

Expand Down
6 changes: 4 additions & 2 deletions packages/image_picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ To use this plugin, add `image_picker` and `image_picker_tizen` as [dependencies

```yaml
dependencies:
image_picker: ^0.8.6
image_picker_tizen: ^2.2.0
image_picker: ^1.0.1
image_picker_tizen: ^2.3.0
```
Then you can import `image_picker` in your Dart code.
Expand All @@ -33,8 +33,10 @@ For detailed usage, see https://pub.dev/packages/image_picker#example.

- [x] `ImagePicker.pickImage` (only `ImageSource.gallery` is available as `source`)
- [x] `ImagePicker.pickMultiImage`
- [x] `ImagePicker.pickMedia`
- [ ] `ImagePicker.pickVideo` (no file manager app available)
- [ ] `ImagePicker.retrieveLostData` (Android-only)
- [x] `ImagePicker.supportsImageSource` (only `ImageSource.gallery` is available as `source`)

## Required privileges

Expand Down
253 changes: 175 additions & 78 deletions packages/image_picker/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs, avoid_print
// ignore_for_file: public_member_api_docs

import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mime/mime.dart';
import 'package:video_player/video_player.dart';

void main() {
Expand Down Expand Up @@ -38,10 +39,10 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
List<XFile>? _imageFileList;
List<XFile>? _mediaFileList;

void _setImageFileListFromFile(XFile? value) {
_imageFileList = value == null ? null : <XFile>[value];
_mediaFileList = value == null ? null : <XFile>[value];
}

dynamic _pickImageError;
Expand All @@ -61,6 +62,10 @@ class _MyHomePageState extends State<MyHomePage> {
await _disposeVideoController();
late VideoPlayerController controller;
if (kIsWeb) {
// TODO(gabrielokura): remove the ignore once the following line can migrate to
// use VideoPlayerController.networkUrl after the issue is resolved.
// https://github.com/flutter/flutter/issues/121927
// ignore: deprecated_member_use
controller = VideoPlayerController.network(file.path);
} else {
controller = VideoPlayerController.file(File(file.path));
Expand All @@ -80,52 +85,86 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

Future<void> _onImageButtonPressed(ImageSource source,
{BuildContext? context, bool isMultiImage = false}) async {
Future<void> _onImageButtonPressed(
ImageSource source, {
required BuildContext context,
bool isMultiImage = false,
bool isMedia = false,
}) async {
if (_controller != null) {
await _controller!.setVolume(0.0);
}
if (isVideo) {
final XFile? file = await _picker.pickVideo(
source: source, maxDuration: const Duration(seconds: 10));
await _playVideo(file);
} else if (isMultiImage) {
await _displayPickImageDialog(context!,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final List<XFile> pickedFileList = await _picker.pickMultiImage(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
setState(() {
_imageFileList = pickedFileList;
});
} catch (e) {
setState(() {
_pickImageError = e;
});
}
});
} else {
await _displayPickImageDialog(context!,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final XFile? pickedFile = await _picker.pickImage(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
setState(() {
_setImageFileListFromFile(pickedFile);
});
} catch (e) {
setState(() {
_pickImageError = e;
});
}
});
if (context.mounted) {
if (isVideo) {
final XFile? file = await _picker.pickVideo(
source: source, maxDuration: const Duration(seconds: 10));
await _playVideo(file);
} else if (isMultiImage) {
await _displayPickImageDialog(context,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final List<XFile> pickedFileList = isMedia
? await _picker.pickMultipleMedia(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
)
: await _picker.pickMultiImage(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
setState(() {
_mediaFileList = pickedFileList;
});
} catch (e) {
setState(() {
_pickImageError = e;
});
}
});
} else if (isMedia) {
await _displayPickImageDialog(context,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final List<XFile> pickedFileList = <XFile>[];
final XFile? media = await _picker.pickMedia(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
if (media != null) {
pickedFileList.add(media);
setState(() {
_mediaFileList = pickedFileList;
});
}
} catch (e) {
setState(() {
_pickImageError = e;
});
}
});
} else {
await _displayPickImageDialog(context,
(double? maxWidth, double? maxHeight, int? quality) async {
try {
final XFile? pickedFile = await _picker.pickImage(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
setState(() {
_setImageFileListFromFile(pickedFile);
});
} catch (e) {
setState(() {
_pickImageError = e;
});
}
});
}
}
}

Expand Down Expand Up @@ -177,22 +216,34 @@ class _MyHomePageState extends State<MyHomePage> {
if (retrieveError != null) {
return retrieveError;
}
if (_imageFileList != null) {
if (_mediaFileList != null) {
return Semantics(
label: 'image_picker_example_picked_images',
child: ListView.builder(
key: UniqueKey(),
itemBuilder: (BuildContext context, int index) {
final String? mime = lookupMimeType(_mediaFileList![index].path);

// Why network for web?
// See https://pub.dev/packages/image_picker#getting-ready-for-the-web-platform
// See https://pub.dev/packages/image_picker_for_web#limitations-on-the-web-platform
return Semantics(
label: 'image_picker_example_picked_image',
child: kIsWeb
? Image.network(_imageFileList![index].path)
: Image.file(File(_imageFileList![index].path)),
? Image.network(_mediaFileList![index].path)
: (mime == null || mime.startsWith('image/')
? Image.file(
File(_mediaFileList![index].path),
errorBuilder: (BuildContext context, Object error,
StackTrace? stackTrace) {
return const Center(
child:
Text('This image type is not supported'));
},
)
: _buildInlineVideoPlayer(index)),
);
},
itemCount: _imageFileList!.length,
itemCount: _mediaFileList!.length,
),
);
} else if (_pickImageError != null) {
Expand All @@ -208,6 +259,17 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

Widget _buildInlineVideoPlayer(int index) {
final VideoPlayerController controller =
VideoPlayerController.file(File(_mediaFileList![index].path));
const double volume = kIsWeb ? 0.0 : 1.0;
controller.setVolume(volume);
controller.initialize();
controller.setLooping(true);
controller.play();
return Center(child: AspectRatioVideo(controller));
}

Widget _handlePreview() {
if (isVideo) {
return _previewVideo();
Expand All @@ -231,7 +293,7 @@ class _MyHomePageState extends State<MyHomePage> {
if (response.files == null) {
_setImageFileListFromFile(response.file);
} else {
_imageFileList = response.files;
_mediaFileList = response.files;
}
});
}
Expand Down Expand Up @@ -292,6 +354,39 @@ class _MyHomePageState extends State<MyHomePage> {
child: const Icon(Icons.photo),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: FloatingActionButton(
onPressed: () {
isVideo = false;
_onImageButtonPressed(
ImageSource.gallery,
context: context,
isMultiImage: true,
isMedia: true,
);
},
heroTag: 'multipleMedia',
tooltip: 'Pick Multiple Media from gallery',
child: const Icon(Icons.photo_library),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: FloatingActionButton(
onPressed: () {
isVideo = false;
_onImageButtonPressed(
ImageSource.gallery,
context: context,
isMedia: true,
);
},
heroTag: 'media',
tooltip: 'Pick Single Media from gallery',
child: const Icon(Icons.photo_library),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: FloatingActionButton(
Expand All @@ -308,44 +403,46 @@ class _MyHomePageState extends State<MyHomePage> {
child: const Icon(Icons.photo_library),
),
),
// Padding(
// padding: const EdgeInsets.only(top: 16.0),
// child: FloatingActionButton(
// onPressed: () {
// isVideo = false;
// _onImageButtonPressed(ImageSource.camera, context: context);
// },
// heroTag: 'image2',
// tooltip: 'Take a Photo',
// child: const Icon(Icons.camera_alt),
// ),
// ),
if (_picker.supportsImageSource(ImageSource.camera))
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: FloatingActionButton(
onPressed: () {
isVideo = false;
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'image2',
tooltip: 'Take a Photo',
child: const Icon(Icons.camera_alt),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () {
isVideo = true;
_onImageButtonPressed(ImageSource.gallery);
_onImageButtonPressed(ImageSource.gallery, context: context);
},
heroTag: 'video0',
tooltip: 'Pick Video from gallery',
child: const Icon(Icons.video_library),
),
),
// Padding(
// padding: const EdgeInsets.only(top: 16.0),
// child: FloatingActionButton(
// backgroundColor: Colors.red,
// onPressed: () {
// isVideo = true;
// _onImageButtonPressed(ImageSource.camera);
// },
// heroTag: 'video1',
// tooltip: 'Take a Video',
// child: const Icon(Icons.videocam),
// ),
// ),
if (_picker.supportsImageSource(ImageSource.camera))
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () {
isVideo = true;
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'video1',
tooltip: 'Take a Video',
child: const Icon(Icons.videocam),
),
),
],
),
);
Expand Down
6 changes: 4 additions & 2 deletions packages/image_picker/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ environment:
dependencies:
flutter:
sdk: flutter
image_picker: ^0.8.6
image_picker: ^1.0.1
image_picker_tizen:
path: ../
video_player: ">=2.1.4 <2.7.0"
mime: ^1.0.4
video_player: ^2.4.2
video_player_tizen: ^2.4.7

dev_dependencies:
flutter_driver:
Expand Down
Loading

0 comments on commit 5736eeb

Please sign in to comment.