Skip to content

Commit

Permalink
Merge pull request #52 from bymoye/master
Browse files Browse the repository at this point in the history
support wasm
  • Loading branch information
TesteurManiak authored Aug 6, 2024
2 parents f8e9f37 + 648587c commit 8fcd2ec
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 176 deletions.
1 change: 0 additions & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
/build/

# Web related
lib/generated_plugin_registrant.dart

# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
26 changes: 7 additions & 19 deletions example/lib/big_video_upload/big_video_upload_view.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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';
import 'package:web/web.dart' as web;

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

@override
State<BigVideoUploadView> createState() => _BigVideoUploadViewState();
Expand All @@ -15,27 +13,18 @@ class BigVideoUploadView extends StatefulWidget {
class _BigVideoUploadViewState extends State<BigVideoUploadView> {
VideoPlayerController? _controller;

Future<void> _createVideo(Uint8List bytes) async {
final blob = html.Blob([bytes]);
final url = html.Url.createObjectUrlFromBlob(blob);
Future<void> _createVideo(web.File file) async {
final url = web.URL.createObjectURL(file);
_controller = VideoPlayerController.networkUrl(Uri.parse(url));

await _controller?.initialize();
setState(() {});
}

Future<Uint8List> _load(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 _load(file);
await _createVideo(bytes);
await _createVideo(file);
}
}

Expand Down Expand Up @@ -67,7 +56,6 @@ class _BigVideoUploadViewState extends State<BigVideoUploadView> {
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (controller != null && controller.value.isInitialized)
AspectRatio(
Expand All @@ -76,7 +64,7 @@ class _BigVideoUploadViewState extends State<BigVideoUploadView> {
),
ElevatedButton(
onPressed: _pickAndLoadVideo,
child: Text('Load Video with FileReader'),
child: const Text('Load Video with FileReader'),
),
],
),
Expand Down
10 changes: 5 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home')),
appBar: AppBar(title: const Text('Home')),
body: Center(
child: SeparatedColumn(
mainAxisAlignment: MainAxisAlignment.center,
separator: SizedBox(height: 8),
separator: const SizedBox(height: 8),
children: [
_Button(
label: 'Sample 1',
Expand All @@ -24,11 +24,11 @@ class HomePage extends StatelessWidget {
label: 'Photo History',
page: PhotosHistoryAddPage(),
),
_Button(
const _Button(
label: 'Big Video Upload',
page: BigVideoUploadView(),
),
_Button(
const _Button(
page: MultiVideoUploadView(),
label: 'Upload Multi Videos',
),
Expand All @@ -53,7 +53,7 @@ class _Button extends StatelessWidget {
return ElevatedButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (_) => page),
MaterialPageRoute<Widget>(builder: (_) => page),
),
child: Text(label),
);
Expand Down
27 changes: 8 additions & 19 deletions example/lib/multi_video_upload/multi_video_upload.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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';
import 'package:web/web.dart' as web;

class MultiVideoUploadView extends StatefulWidget {
const MultiVideoUploadView({super.key});
Expand All @@ -15,31 +13,22 @@ class MultiVideoUploadView extends StatefulWidget {
class _MultiVideoUploadViewState extends State<MultiVideoUploadView> {
final _controllers = <VideoPlayerController>[];

Future<void> _createVideos(List<Uint8List> bytesList) async {
for (final bytes in bytesList) {
final blob = html.Blob([bytes]);
final url = html.Url.createObjectUrlFromBlob(blob);
final controller = VideoPlayerController.networkUrl(Uri.parse(url));
Future<void> _createVideos(List<web.File> files) async {
for (final file in files) {
final String url = web.URL.createObjectURL(file);
final VideoPlayerController controller =
VideoPlayerController.networkUrl(Uri.parse(url));
await controller.initialize();
_controllers.add(controller);
}
setState(() {});
}

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

Future<void> _pickAndLoadVideos() async {
setState(_disposeControllers);
final files = await ImagePickerWeb.getMultiVideosAsFile();
if (files != null) {
final bytesList = await Future.wait(files.map(_load));
await _createVideos(bytesList);
await _createVideos(files);
}
}

Expand All @@ -63,7 +52,7 @@ class _MultiVideoUploadViewState extends State<MultiVideoUploadView> {
),
ElevatedButton(
onPressed: _pickAndLoadVideos,
child: Text('Pick videos'),
child: const Text('Pick videos'),
),
],
),
Expand Down
34 changes: 16 additions & 18 deletions example/lib/photo_history/photo_history_add_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum PageStatus { loading, error, loaded }

class ImagePickerWidget extends StatefulWidget {
@override
_ImagePickerWidgetState createState() => _ImagePickerWidgetState();
State<ImagePickerWidget> createState() => _ImagePickerWidgetState();
}

class _ImagePickerWidgetState extends State<ImagePickerWidget> {
Expand All @@ -23,10 +23,9 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Photo History')),
appBar: AppBar(title: const Text('Photo History')),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
height: 100,
Expand All @@ -37,16 +36,17 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {
if (index == 0) {
return _buildAddPhoto();
}
var image = _photos[index - 1];
final image = _photos[index - 1];
return Stack(
children: <Widget>[
InkWell(
child: Container(
margin: EdgeInsets.all(5),
height: 100,
width: 100,
color: kLightGray,
child: image),
margin: const EdgeInsets.all(5),
height: 100,
width: 100,
color: kLightGray,
child: image,
),
),
],
);
Expand All @@ -55,12 +55,12 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {
),
if (_pageStatus == PageStatus.loaded)
Container(
margin: EdgeInsets.all(16),
margin: const EdgeInsets.all(16),
child: ElevatedButton(
onPressed: () {},
child: Text('Save'),
child: const Text('Save'),
),
)
),
],
),
);
Expand All @@ -69,24 +69,23 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {
InkWell _buildAddPhoto() {
if (_pageStatus == PageStatus.loading) {
return InkWell(
onTap: () => null,
child: Container(
margin: EdgeInsets.all(5),
margin: const EdgeInsets.all(5),
height: 100,
width: 100,
color: kDarkGray,
child: Center(child: Text('Please wait..')),
child: const Center(child: Text('Please wait..')),
),
);
} else {
return InkWell(
onTap: () => _onAddPhotoClicked(context),
child: Container(
margin: EdgeInsets.all(5),
margin: const EdgeInsets.all(5),
height: 100,
width: 100,
color: kDarkGray,
child: Center(
child: const Center(
child: Icon(
Icons.add_to_photos,
color: kLightGray,
Expand All @@ -103,7 +102,6 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {
});

final image = await ImagePickerWeb.getImageAsWidget();
print(image);

if (image != null) {
setState(() {
Expand Down
Loading

0 comments on commit 8fcd2ec

Please sign in to comment.