Skip to content

Commit

Permalink
finalized example
Browse files Browse the repository at this point in the history
  • Loading branch information
TesteurManiak committed Mar 22, 2022
1 parent 1e68a29 commit 807673b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 54 deletions.
65 changes: 25 additions & 40 deletions example/lib/big_video_upload/big_video_upload_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<BigVideoUploadView> createState() => _BigVideoUploadViewState();
}

class _BigVideoUploadViewState extends State<BigVideoUploadView> {
VideoPlayerController _controller;
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();
await _controller?.initialize();
setState(() {});
}

Expand All @@ -32,20 +32,11 @@ class _BigVideoUploadViewState extends State<BigVideoUploadView> {
}

Future<void> _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<void> _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
Expand All @@ -56,43 +47,37 @@ class _BigVideoUploadViewState extends State<BigVideoUploadView> {

@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'),
),
],
),
),
Expand Down
24 changes: 11 additions & 13 deletions example/lib/sample/sample_page.dart
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
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 807673b

Please sign in to comment.