Skip to content

Commit

Permalink
Merge pull request #36 from Ahmadre/issue_35
Browse files Browse the repository at this point in the history
Issue 35
  • Loading branch information
TesteurManiak authored Mar 22, 2022
2 parents 08c9f5a + a087cea commit 472505c
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.1.1 - [22/03/2022]

* Updated example project to null safety.
* Fixed potential issue with null value for methods `getImageInfo`, `getVideoAsBytes` and `getVideoInfo`.

## 2.1.0 - [14/12/2021]

* Deprecated `getImage`, `getMultiImages` and `getVideo` methods.
Expand Down
86 changes: 86 additions & 0 deletions example/lib/big_video_upload/big_video_upload_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:html' as html;
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:image_picker_web/image_picker_web.dart';
import 'package:video_player/video_player.dart';

class BigVideoUploadView extends StatefulWidget {
const BigVideoUploadView({Key? key}) : super(key: key);

@override
State<BigVideoUploadView> createState() => _BigVideoUploadViewState();
}

class _BigVideoUploadViewState extends State<BigVideoUploadView> {
VideoPlayerController? _controller;

Future<void> _createVideo(Uint8List bytes) async {
final blob = html.Blob([bytes]);
final url = html.Url.createObjectUrlFromBlob(blob);
_controller = VideoPlayerController.network(url);
await _controller?.initialize();
setState(() {});
}

Future<Uint8List> _loadImage(html.File file) async {
final reader = html.FileReader();
reader.readAsArrayBuffer(file);
await reader.onLoad.first;
reader.onLoadEnd;
return reader.result as Uint8List;
}

Future<void> _pickAndLoadVideo() async {
final file = await ImagePickerWeb.getVideoAsFile();
if (file != null) {
final bytes = await _loadImage(file);
await _createVideo(bytes);
}
}

@override
void dispose() {
_controller?.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final controller = _controller;
return Scaffold(
appBar: AppBar(title: const Text('Big Video Upload')),
floatingActionButton: controller != null
? FloatingActionButton(
onPressed: () {
setState(() {
controller.value.isPlaying
? controller.pause()
: controller.play();
});
},
child: Icon(
controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
)
: null,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (controller != null && controller.value.isInitialized)
AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: VideoPlayer(controller),
),
ElevatedButton(
onPressed: _pickAndLoadVideo,
child: Text('Load Video with FileReader'),
),
],
),
),
);
}
}
13 changes: 10 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:image_picker_web_example/photoHistory_add_view.dart';
import 'package:image_picker_web_example/sample_page.dart';
import 'package:image_picker_web_example/big_video_upload/big_video_upload_view.dart';
import 'package:image_picker_web_example/photo_history/photo_history_add_view.dart';
import 'package:image_picker_web_example/sample/sample_page.dart';

void main() => runApp(MaterialApp(home: HomePage()));

Expand All @@ -23,7 +24,13 @@ class HomePage extends StatelessWidget {
ElevatedButton(
onPressed: () => Navigator.push(context,
MaterialPageRoute(builder: (_) => PhotosHistoryAddPage())),
child: Text('Sample 2'),
child: Text('Photo History'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () => Navigator.push(context,
MaterialPageRoute(builder: (_) => BigVideoUploadView())),
child: Text('Big Video Upload'),
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PhotosHistoryAddPage extends StatelessWidget {
Widget build(BuildContext context) => ImagePickerWidget();
}

enum PageStatus { LOADING, ERROR, LOADED }
enum PageStatus { loading, error, loaded }

class ImagePickerWidget extends StatefulWidget {
@override
Expand All @@ -18,12 +18,12 @@ class ImagePickerWidget extends StatefulWidget {

class _ImagePickerWidgetState extends State<ImagePickerWidget> {
final _photos = <Image>[];
PageStatus _pageStatus = PageStatus.LOADED;
PageStatus _pageStatus = PageStatus.loaded;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Sample 2')),
appBar: AppBar(title: Text('Photo History')),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
Expand Down Expand Up @@ -53,7 +53,7 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {
},
),
),
if (_pageStatus == PageStatus.LOADED)
if (_pageStatus == PageStatus.loaded)
Container(
margin: EdgeInsets.all(16),
child: ElevatedButton(
Expand All @@ -67,7 +67,7 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {
}

InkWell _buildAddPhoto() {
if (_pageStatus == PageStatus.LOADING) {
if (_pageStatus == PageStatus.loading) {
return InkWell(
onTap: () => null,
child: Container(
Expand Down Expand Up @@ -99,7 +99,7 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {

Future<void> _onAddPhotoClicked(context) async {
setState(() {
_pageStatus = PageStatus.LOADING;
_pageStatus = PageStatus.loading;
});

final image = await ImagePickerWeb.getImageAsWidget();
Expand All @@ -108,11 +108,11 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {
if (image != null) {
setState(() {
_photos.add(image);
_pageStatus = PageStatus.LOADED;
_pageStatus = PageStatus.loaded;
});
} else {
setState(() {
_pageStatus = PageStatus.LOADED;
_pageStatus = PageStatus.loaded;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ class _SamplePageState extends State<SamplePage> {
Future<void> _getImgFile() async {
final infos = await ImagePickerWeb.getImageAsFile();
setState(() => _imageInfo =
'Name: ${infos.name}\nRelative Path: ${infos.relativePath}');
'Name: ${infos?.name}\nRelative Path: ${infos?.relativePath}');
}

Future<void> _getImgInfo() async {
final infos = await ImagePickerWeb.getImageInfo;
setState(() {
_pickedImages.clear();
_pickedImages.add(Image.memory(
infos.data,
semanticLabel: infos.fileName,
));
_imageInfo = '${infos.toJson()}';
});
final data = infos?.data;
if (data != null) {
setState(() {
_pickedImages.clear();
_pickedImages.add(Image.memory(data, semanticLabel: infos?.fileName));
_imageInfo = '${infos?.toJson()}';
});
}
}

@override
Expand All @@ -81,10 +81,8 @@ class _SamplePageState extends State<SamplePage> {
height: 200,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount:
_pickedImages == null ? 0 : _pickedImages.length,
itemBuilder: (context, index) =>
_pickedImages[index]),
itemCount: _pickedImages.length,
itemBuilder: (_, index) => _pickedImages[index]),
),
),
Container(
Expand Down
3 changes: 2 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ description: Demonstrates how to use the image_picker_web plugin.
publish_to: "none"

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.15.0 <3.0.0"

dependencies:
cupertino_icons: ^0.1.2
flutter:
sdk: flutter
video_player: ^2.3.0

dev_dependencies:
flutter_test:
Expand Down
13 changes: 8 additions & 5 deletions lib/image_picker_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,11 @@ class ImagePickerWeb {
/// Help to retrieve further image's informations about your picked source.
///
/// Return an object [MediaInfo] containing image's informations.
static Future<MediaInfo> get getImageInfo async {
static Future<MediaInfo?> get getImageInfo async {
final data =
await (_methodChannel.invokeMapMethod<String, dynamic>('pickImage'));
return MediaInfo.fromJson(data!);
if (data == null) return null;
return MediaInfo.fromJson(data);
}

/// Picker that allows multi-image selection. Here are the different instance
Expand Down Expand Up @@ -286,7 +287,8 @@ class ImagePickerWeb {
static Future<Uint8List?> getVideoAsBytes() async {
final data =
await _methodChannel.invokeMapMethod<String, dynamic>('pickVideo');
final imageData = base64.decode(data!['data']);
if (data == null) return null;
final imageData = base64.decode(data['data']);
return imageData;
}

Expand All @@ -299,9 +301,10 @@ class ImagePickerWeb {
/// Help to retrieve further video's informations about your picked source.
///
/// Return an object [MediaInfo] containing video's informations.
static Future<MediaInfo> get getVideoInfo async {
static Future<MediaInfo?> get getVideoInfo async {
final data =
await _methodChannel.invokeMapMethod<String, dynamic>('pickVideo');
return MediaInfo.fromJson(data!);
if (data == null) return null;
return MediaInfo.fromJson(data);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: image_picker_web
description: Flutter Web Plugin to pick Images (as Widget, File or Uint8List)
and Videos (as File or Uint8List)
version: 2.1.0
version: 2.1.1
repository: https://github.com/Ahmadre/image_picker_web

environment:
Expand Down

0 comments on commit 472505c

Please sign in to comment.