Add play/pause keyboard handler to FullscreenVideoView

This commit is contained in:
Shawn 2023-05-10 14:28:04 -06:00
parent 38dc787e27
commit ddf5e54089

View File

@ -1,4 +1,6 @@
import 'package:wonders/common_libs.dart'; import 'package:wonders/common_libs.dart';
import 'package:wonders/logic/common/platform_info.dart';
import 'package:wonders/ui/common/modals/app_modals.dart';
import 'package:youtube_player_iframe/youtube_player_iframe.dart'; import 'package:youtube_player_iframe/youtube_player_iframe.dart';
class FullscreenVideoViewer extends StatefulWidget { class FullscreenVideoViewer extends StatefulWidget {
@ -15,19 +17,40 @@ class _FullscreenVideoViewerState extends State<FullscreenVideoViewer> {
params: const YoutubePlayerParams(), params: const YoutubePlayerParams(),
); );
bool get _enableVideo => PlatformInfo.isMobile;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
appLogic.supportedOrientationsOverride = [Axis.horizontal, Axis.vertical]; appLogic.supportedOrientationsOverride = [Axis.horizontal, Axis.vertical];
RawKeyboard.instance.addListener(_handleKeyDown);
} }
@override @override
void dispose() { void dispose() {
// when view closes, remove the override // when view closes, remove the override
appLogic.supportedOrientationsOverride = null; appLogic.supportedOrientationsOverride = null;
RawKeyboard.instance.removeListener(_handleKeyDown);
super.dispose(); super.dispose();
} }
Future<void> _handleKeyDown(RawKeyEvent value) async {
if (value.repeat) return;
if (value is RawKeyDownEvent) {
final k = value.logicalKey;
if (k == LogicalKeyboardKey.enter || k == LogicalKeyboardKey.space) {
if (_enableVideo) {
final state = await _controller.playerState;
if (state == PlayerState.playing) {
_controller.pauseVideo();
} else {
_controller.playVideo();
}
}
}
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
double aspect = context.isLandscape ? MediaQuery.of(context).size.aspectRatio : 9 / 9; double aspect = context.isLandscape ? MediaQuery.of(context).size.aspectRatio : 9 / 9;
@ -36,10 +59,12 @@ class _FullscreenVideoViewerState extends State<FullscreenVideoViewer> {
body: Stack( body: Stack(
children: [ children: [
Center( Center(
child: YoutubePlayer( child: (PlatformInfo.isMobile)
controller: _controller, ? YoutubePlayer(
aspectRatio: aspect, controller: _controller,
), aspectRatio: aspect,
)
: Placeholder(),
), ),
SafeArea( SafeArea(
child: Padding( child: Padding(