From ddf5e54089d41032c0c9a96d628b21d5e87d8fd5 Mon Sep 17 00:00:00 2001 From: Shawn Date: Wed, 10 May 2023 14:28:04 -0600 Subject: [PATCH] Add play/pause keyboard handler to FullscreenVideoView --- .../modals/fullscreen_video_viewer.dart | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/ui/common/modals/fullscreen_video_viewer.dart b/lib/ui/common/modals/fullscreen_video_viewer.dart index 863482bc..4e056d8b 100644 --- a/lib/ui/common/modals/fullscreen_video_viewer.dart +++ b/lib/ui/common/modals/fullscreen_video_viewer.dart @@ -1,4 +1,6 @@ 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'; class FullscreenVideoViewer extends StatefulWidget { @@ -15,19 +17,40 @@ class _FullscreenVideoViewerState extends State { params: const YoutubePlayerParams(), ); + bool get _enableVideo => PlatformInfo.isMobile; + @override void initState() { super.initState(); appLogic.supportedOrientationsOverride = [Axis.horizontal, Axis.vertical]; + RawKeyboard.instance.addListener(_handleKeyDown); } @override void dispose() { // when view closes, remove the override appLogic.supportedOrientationsOverride = null; + RawKeyboard.instance.removeListener(_handleKeyDown); super.dispose(); } + Future _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 Widget build(BuildContext context) { double aspect = context.isLandscape ? MediaQuery.of(context).size.aspectRatio : 9 / 9; @@ -36,10 +59,12 @@ class _FullscreenVideoViewerState extends State { body: Stack( children: [ Center( - child: YoutubePlayer( - controller: _controller, - aspectRatio: aspect, - ), + child: (PlatformInfo.isMobile) + ? YoutubePlayer( + controller: _controller, + aspectRatio: aspect, + ) + : Placeholder(), ), SafeArea( child: Padding(