This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 6
updating example and adding method #366
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
be5831f
Introduce the PickedFile class to have a more platform agnostic return.
ditman 7b700a8
Modify the platform interface to return PickedFiles.
ditman ca8119a
Add new API to the image_picker, and mark old methods as deprecated. …
ditman b421fe0
Add image_picker_for_web plugin.
ditman e1ee13d
Make API Async, so web can use objectUrls internally, instead of bytes.
ditman 9e43c73
Modify core image_picker so it uses the new APIs, and add web example
ditman 9ca897d
[image_picker] Ignore deprecated use analyzer warnings
ditman ac807f8
[image_picker_platform_interface] Ignore deprecated use analyzer warn…
ditman 93ed49c
[image_picker_for_web] Remove copypaste from test file.
ditman aae9c08
[image_picker] Bump changelog and version
ditman c49f372
updating example and adding method
rodydavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/src/widgets/basic.dart'; | ||
import 'package:flutter/src/widgets/container.dart'; | ||
|
@@ -37,20 +38,25 @@ class MyHomePage extends StatefulWidget { | |
} | ||
|
||
class _MyHomePageState extends State<MyHomePage> { | ||
File _imageFile; | ||
PickedFile _imageFile; | ||
dynamic _pickImageError; | ||
bool isVideo = false; | ||
RetrieveType mediaSource = RetrieveType.video; | ||
VideoPlayerController _controller; | ||
String _retrieveDataError; | ||
|
||
final ImagePicker _picker = ImagePicker(); | ||
final TextEditingController maxWidthController = TextEditingController(); | ||
final TextEditingController maxHeightController = TextEditingController(); | ||
final TextEditingController qualityController = TextEditingController(); | ||
|
||
Future<void> _playVideo(File file) async { | ||
Future<void> _playVideo(PickedFile file) async { | ||
if (file != null && mounted) { | ||
await _disposeVideoController(); | ||
_controller = VideoPlayerController.file(file); | ||
if (kIsWeb) { | ||
_controller = VideoPlayerController.network(file.path); | ||
} else { | ||
_controller = VideoPlayerController.file(File(file.path)); | ||
} | ||
await _controller.setVolume(1.0); | ||
await _controller.initialize(); | ||
await _controller.setLooping(true); | ||
|
@@ -63,24 +69,32 @@ class _MyHomePageState extends State<MyHomePage> { | |
if (_controller != null) { | ||
await _controller.setVolume(0.0); | ||
} | ||
if (isVideo) { | ||
final File file = await ImagePicker.pickVideo( | ||
source: source, maxDuration: const Duration(seconds: 10)); | ||
await _playVideo(file); | ||
} else { | ||
await _displayPickImageDialog(context, | ||
(double maxWidth, double maxHeight, int quality) async { | ||
try { | ||
_imageFile = await ImagePicker.pickImage( | ||
switch (mediaSource) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not a fan of this global controlling the behavior of this method. Do you think it'd be possible to split (Anyway, that might be a refactor for another time) |
||
case RetrieveType.image: | ||
await _displayPickImageDialog(context, | ||
(double maxWidth, double maxHeight, int quality) async { | ||
try { | ||
final pickedFile = await _picker.getImage( | ||
source: source, | ||
maxWidth: maxWidth, | ||
maxHeight: maxHeight, | ||
imageQuality: quality); | ||
setState(() {}); | ||
} catch (e) { | ||
_pickImageError = e; | ||
} | ||
}); | ||
imageQuality: quality, | ||
); | ||
setState(() { | ||
_imageFile = pickedFile; | ||
}); | ||
} catch (e) { | ||
setState(() { | ||
_pickImageError = e; | ||
}); | ||
} | ||
}); | ||
break; | ||
case RetrieveType.video: | ||
final PickedFile file = await _picker.getVideo( | ||
source: source, maxDuration: const Duration(seconds: 10)); | ||
await _playVideo(file); | ||
break; | ||
} | ||
} | ||
|
||
|
@@ -132,7 +146,13 @@ class _MyHomePageState extends State<MyHomePage> { | |
return retrieveError; | ||
} | ||
if (_imageFile != null) { | ||
return Image.file(_imageFile); | ||
if (kIsWeb) { | ||
return Image.network(_imageFile.path); | ||
// Or from memory... | ||
} else { | ||
// This would also work from memory as well... | ||
return Image.file(File(_imageFile.path)); | ||
} | ||
} else if (_pickImageError != null) { | ||
return Text( | ||
'Pick image error: $_pickImageError', | ||
|
@@ -147,16 +167,16 @@ class _MyHomePageState extends State<MyHomePage> { | |
} | ||
|
||
Future<void> retrieveLostData() async { | ||
final LostDataResponse response = await ImagePicker.retrieveLostData(); | ||
final LostData response = await _picker.getLostData(); | ||
if (response.isEmpty) { | ||
return; | ||
} | ||
if (response.file != null) { | ||
if (response.type == RetrieveType.video) { | ||
isVideo = true; | ||
mediaSource = RetrieveType.video; | ||
await _playVideo(response.file); | ||
} else { | ||
isVideo = false; | ||
mediaSource = RetrieveType.image; | ||
setState(() { | ||
_imageFile = response.file; | ||
}); | ||
|
@@ -171,9 +191,18 @@ class _MyHomePageState extends State<MyHomePage> { | |
return Scaffold( | ||
appBar: AppBar( | ||
title: Text(widget.title), | ||
actions: [ | ||
IconButton( | ||
onPressed: () { | ||
_onImageButtonPressed(ImageSource.gallery, context: context); | ||
}, | ||
tooltip: 'Pick Media', | ||
icon: const Icon(Icons.add), | ||
), | ||
], | ||
), | ||
body: Center( | ||
child: Platform.isAndroid | ||
child: !kIsWeb && defaultTargetPlatform == TargetPlatform.android | ||
? FutureBuilder<void>( | ||
future: retrieveLostData(), | ||
builder: (BuildContext context, AsyncSnapshot<void> snapshot) { | ||
|
@@ -185,7 +214,9 @@ class _MyHomePageState extends State<MyHomePage> { | |
textAlign: TextAlign.center, | ||
); | ||
case ConnectionState.done: | ||
return isVideo ? _previewVideo() : _previewImage(); | ||
return mediaSource == RetrieveType.video | ||
? _previewVideo() | ||
: _previewImage(); | ||
default: | ||
if (snapshot.hasError) { | ||
return Text( | ||
|
@@ -201,25 +232,30 @@ class _MyHomePageState extends State<MyHomePage> { | |
} | ||
}, | ||
) | ||
: (isVideo ? _previewVideo() : _previewImage()), | ||
: (mediaSource == RetrieveType.video | ||
? _previewVideo() | ||
: _previewImage()), | ||
), | ||
floatingActionButton: Column( | ||
mainAxisAlignment: MainAxisAlignment.end, | ||
children: <Widget>[ | ||
FloatingActionButton( | ||
onPressed: () { | ||
isVideo = false; | ||
_onImageButtonPressed(ImageSource.gallery, context: context); | ||
}, | ||
heroTag: 'image0', | ||
tooltip: 'Pick Image from gallery', | ||
child: const Icon(Icons.photo_library), | ||
Padding( | ||
padding: const EdgeInsets.only(top: 0.0), | ||
child: FloatingActionButton( | ||
onPressed: () { | ||
mediaSource = RetrieveType.image; | ||
_onImageButtonPressed(ImageSource.gallery, context: context); | ||
}, | ||
heroTag: 'image0', | ||
tooltip: 'Pick Image from gallery', | ||
child: const Icon(Icons.photo_library), | ||
), | ||
), | ||
Padding( | ||
padding: const EdgeInsets.only(top: 16.0), | ||
child: FloatingActionButton( | ||
onPressed: () { | ||
isVideo = false; | ||
mediaSource = RetrieveType.image; | ||
_onImageButtonPressed(ImageSource.camera, context: context); | ||
}, | ||
heroTag: 'image1', | ||
|
@@ -232,7 +268,7 @@ class _MyHomePageState extends State<MyHomePage> { | |
child: FloatingActionButton( | ||
backgroundColor: Colors.red, | ||
onPressed: () { | ||
isVideo = true; | ||
mediaSource = RetrieveType.video; | ||
_onImageButtonPressed(ImageSource.gallery); | ||
}, | ||
heroTag: 'video0', | ||
|
@@ -245,7 +281,7 @@ class _MyHomePageState extends State<MyHomePage> { | |
child: FloatingActionButton( | ||
backgroundColor: Colors.red, | ||
onPressed: () { | ||
isVideo = true; | ||
mediaSource = RetrieveType.video; | ||
_onImageButtonPressed(ImageSource.camera); | ||
}, | ||
heroTag: 'video1', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta content="IE=Edge" http-equiv="X-UA-Compatible"> | ||
<meta name="description" content="An example of the image_picker on the web."> | ||
|
||
<!-- iOS meta tags & icons --> | ||
<meta name="apple-mobile-web-app-capable" content="yes"> | ||
<meta name="apple-mobile-web-app-status-bar-style" content="black"> | ||
<meta name="apple-mobile-web-app-title" content="example"> | ||
<link rel="apple-touch-icon" href="icons/Icon-192.png"> | ||
|
||
<!-- Favicon --> | ||
<link rel="shortcut icon" type="image/png" href="favicon.png"/> | ||
|
||
<title>url_launcher web example</title> | ||
<link rel="manifest" href="manifest.json"> | ||
</head> | ||
<body> | ||
<!-- This script installs service_worker.js to provide PWA functionality to | ||
application. For more information, see: | ||
https://developers.google.com/web/fundamentals/primers/service-workers --> | ||
<!-- <script> | ||
if ('serviceWorker' in navigator) { | ||
window.addEventListener('load', function () { | ||
navigator.serviceWorker.register('flutter_service_worker.js'); | ||
}); | ||
} | ||
</script> --> | ||
<script src="main.dart.js" type="application/javascript"></script> | ||
</body> | ||
</html> |
23 changes: 23 additions & 0 deletions
23
packages/image_picker/image_picker/example/web/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "image_picker example", | ||
"short_name": "image_picker", | ||
"start_url": ".", | ||
"display": "minimal-ui", | ||
"background_color": "#0175C2", | ||
"theme_color": "#0175C2", | ||
"description": "An example of the image_picker on the web.", | ||
"orientation": "portrait-primary", | ||
"prefer_related_applications": false, | ||
"icons": [ | ||
{ | ||
"src": "icons/Icon-192.png", | ||
"sizes": "192x192", | ||
"type": "image/png" | ||
}, | ||
{ | ||
"src": "icons/Icon-512.png", | ||
"sizes": "512x512", | ||
"type": "image/png" | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd call this
mediaType
to not confuse it with the currentsource
(gallery vs camera)