From 807673bf7405c5018c81208ad518ed4001449206 Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Tue, 22 Mar 2022 22:47:36 +0100 Subject: [PATCH] finalized example --- .../big_video_upload_view.dart | 65 +++++++------------ example/lib/sample/sample_page.dart | 24 ++++--- example/pubspec.yaml | 2 +- 3 files changed, 37 insertions(+), 54 deletions(-) diff --git a/example/lib/big_video_upload/big_video_upload_view.dart b/example/lib/big_video_upload/big_video_upload_view.dart index eae2197..207a841 100644 --- a/example/lib/big_video_upload/big_video_upload_view.dart +++ b/example/lib/big_video_upload/big_video_upload_view.dart @@ -6,20 +6,20 @@ 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); + const BigVideoUploadView({Key? key}) : super(key: key); @override State createState() => _BigVideoUploadViewState(); } class _BigVideoUploadViewState extends State { - VideoPlayerController _controller; + VideoPlayerController? _controller; Future _createVideo(Uint8List bytes) async { final blob = html.Blob([bytes]); final url = html.Url.createObjectUrlFromBlob(blob); _controller = VideoPlayerController.network(url); - await _controller.initialize(); + await _controller?.initialize(); setState(() {}); } @@ -32,20 +32,11 @@ class _BigVideoUploadViewState extends State { } Future _pickAndLoadVideo() async { - final timer = Stopwatch()..start(); final file = await ImagePickerWeb.getVideoAsFile(); - final bytes = await _loadImage(file); - timer.stop(); - print('Loaded in ${timer.elapsedMilliseconds}ms'); - await _createVideo(bytes); - } - - Future _readAndLoadVideo() async { - final timer = Stopwatch()..start(); - final bytes = await ImagePickerWeb.getVideoAsBytes(); - timer.stop(); - print('Loaded in ${timer.elapsedMilliseconds}ms'); - await _createVideo(bytes); + if (file != null) { + final bytes = await _loadImage(file); + await _createVideo(bytes); + } } @override @@ -56,43 +47,37 @@ class _BigVideoUploadViewState extends State { @override Widget build(BuildContext context) { + final controller = _controller; return Scaffold( appBar: AppBar(title: const Text('Big Video Upload')), - floatingActionButton: FloatingActionButton( - onPressed: () { - if (_controller != null) { - setState(() { - _controller.value.isPlaying - ? _controller.pause() - : _controller.play(); - }); - } - }, - child: Icon( - _controller != null && _controller.value.isPlaying - ? Icons.pause - : Icons.play_arrow, - ), - ), + 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) + if (controller != null && controller.value.isInitialized) AspectRatio( - aspectRatio: _controller.value.aspectRatio, - child: VideoPlayer(_controller), + aspectRatio: controller.value.aspectRatio, + child: VideoPlayer(controller), ), ElevatedButton( onPressed: _pickAndLoadVideo, child: Text('Load Video with FileReader'), ), - const SizedBox(height: 16), - ElevatedButton( - onPressed: _readAndLoadVideo, - child: Text('Load Video with getVideoAsBytes'), - ), ], ), ), diff --git a/example/lib/sample/sample_page.dart b/example/lib/sample/sample_page.dart index a712aaf..d0b7b6d 100644 --- a/example/lib/sample/sample_page.dart +++ b/example/lib/sample/sample_page.dart @@ -43,19 +43,19 @@ class _SamplePageState extends State { Future _getImgFile() async { final infos = await ImagePickerWeb.getImageAsFile(); setState(() => _imageInfo = - 'Name: ${infos.name}\nRelative Path: ${infos.relativePath}'); + 'Name: ${infos?.name}\nRelative Path: ${infos?.relativePath}'); } Future _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 @@ -81,10 +81,8 @@ class _SamplePageState extends State { 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( diff --git a/example/pubspec.yaml b/example/pubspec.yaml index d82f7cc..bf808b6 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -3,7 +3,7 @@ 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