-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
packages/djangoflow_video_player/lib/src/video_player_widget_builder.dart
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,69 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:video_player/video_player.dart'; | ||
|
||
typedef VideoPlayerWidgetBuilderFunction = Widget Function( | ||
BuildContext context, | ||
VideoPlayerController? controller, | ||
bool isInitialized, | ||
); | ||
|
||
class VideoPlayerWidgetBuilder extends StatelessWidget { | ||
const VideoPlayerWidgetBuilder({ | ||
required this.builder, | ||
super.key, | ||
this.url, | ||
}); | ||
final String? url; | ||
final VideoPlayerWidgetBuilderFunction builder; | ||
|
||
@override | ||
Widget build(BuildContext context) => _VideoPlayerWidgetInternal( | ||
url: url, | ||
builder: builder, | ||
); | ||
} | ||
|
||
class _VideoPlayerWidgetInternal extends StatefulWidget { | ||
const _VideoPlayerWidgetInternal({ | ||
required this.builder, | ||
this.url, | ||
}); | ||
final String? url; | ||
final VideoPlayerWidgetBuilderFunction builder; | ||
|
||
@override | ||
__VideoPlayerWidgetInternalState createState() => | ||
__VideoPlayerWidgetInternalState(); | ||
} | ||
|
||
class __VideoPlayerWidgetInternalState | ||
extends State<_VideoPlayerWidgetInternal> { | ||
VideoPlayerController? _controller; | ||
bool _isInitialized = false; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
if (widget.url != null) { | ||
final uri = Uri.tryParse(widget.url!); | ||
if (uri != null) { | ||
_controller = VideoPlayerController.networkUrl(uri) | ||
..initialize().then((_) { | ||
setState(() { | ||
_isInitialized = true; | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_controller?.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) => | ||
widget.builder(context, _controller, _isInitialized); | ||
} |
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