wonders/lib/ui/common/modals/fullscreen_video_viewer.dart

80 lines
2.3 KiB
Dart
Raw Normal View History

2022-08-29 20:38:28 -06:00
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';
2022-08-29 20:38:28 -06:00
class FullscreenVideoViewer extends StatefulWidget {
const FullscreenVideoViewer({Key? key, required this.id}) : super(key: key);
2022-08-29 20:38:28 -06:00
final String id;
@override
State<FullscreenVideoViewer> createState() => _FullscreenVideoViewerState();
2022-08-29 20:38:28 -06:00
}
class _FullscreenVideoViewerState extends State<FullscreenVideoViewer> {
late final _controller = YoutubePlayerController.fromVideoId(
videoId: widget.id,
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<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();
}
}
}
}
}
2022-08-29 20:38:28 -06:00
@override
Widget build(BuildContext context) {
double aspect = context.isLandscape ? MediaQuery.of(context).size.aspectRatio : 9 / 9;
return Scaffold(
backgroundColor: Colors.black,
body: Stack(
children: [
Center(
child: (PlatformInfo.isMobile)
? YoutubePlayer(
controller: _controller,
aspectRatio: aspect,
)
: Placeholder(),
),
SafeArea(
child: Padding(
padding: EdgeInsets.all($styles.insets.md),
child: const BackBtn(),
),
),
],
),
);
}
2022-08-29 20:38:28 -06:00
}