110 lines
3.4 KiB
Dart
110 lines
3.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:tablet_app/Components/video_viewer.dart';
|
|
import 'package:tablet_app/constants.dart';
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
|
import 'package:youtube_player_iframe/youtube_player_iframe.dart' as iframe;
|
|
|
|
enum _VideoSourceType { youtube, vimeo, direct }
|
|
|
|
_VideoSourceType _detectSourceType(String url) {
|
|
if (url.contains('youtube.com') || url.contains('youtu.be')) return _VideoSourceType.youtube;
|
|
if (url.contains('vimeo.com')) return _VideoSourceType.vimeo;
|
|
return _VideoSourceType.direct;
|
|
}
|
|
|
|
String? _extractVimeoId(String url) {
|
|
final match = RegExp(r'vimeo\.com/(?:.*?/)?(\d+)').firstMatch(url);
|
|
return match?.group(1);
|
|
}
|
|
|
|
class VideoView extends StatefulWidget {
|
|
final VideoDTO section;
|
|
VideoView({required this.section});
|
|
|
|
@override
|
|
_VideoView createState() => _VideoView();
|
|
}
|
|
|
|
class _VideoView extends State<VideoView> {
|
|
iframe.YoutubePlayerController? _youtubeController;
|
|
WebViewController? _vimeoController;
|
|
_VideoSourceType? _sourceType;
|
|
|
|
static const _browserUserAgent =
|
|
'Mozilla/5.0 (Linux; Android 10; Tablet) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final source = widget.section.source_;
|
|
if (source == null || source.isEmpty) return;
|
|
|
|
_sourceType = _detectSourceType(source);
|
|
|
|
if (_sourceType == _VideoSourceType.youtube) {
|
|
_youtubeController = iframe.YoutubePlayerController(
|
|
params: iframe.YoutubePlayerParams(
|
|
mute: false,
|
|
showControls: true,
|
|
showFullscreenButton: false,
|
|
loop: true,
|
|
showVideoAnnotations: false,
|
|
strictRelatedVideos: false,
|
|
enableKeyboard: false,
|
|
enableCaption: false,
|
|
pointerEvents: iframe.PointerEvents.auto,
|
|
userAgent: _browserUserAgent,
|
|
),
|
|
);
|
|
_youtubeController!.loadVideo(source);
|
|
} else if (_sourceType == _VideoSourceType.vimeo) {
|
|
final vimeoId = _extractVimeoId(source);
|
|
if (vimeoId != null) {
|
|
_vimeoController = WebViewController()
|
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
|
..setUserAgent(_browserUserAgent)
|
|
..loadRequest(Uri.parse('https://player.vimeo.com/video/$vimeoId'));
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_youtubeController?.close();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final source = widget.section.source_;
|
|
if (source == null || source.isEmpty) {
|
|
return Center(
|
|
child: Text(
|
|
"La vidéo ne peut pas être affichée, l'url est incorrecte",
|
|
style: TextStyle(fontSize: kNoneInfoOrIncorrect),
|
|
),
|
|
);
|
|
}
|
|
|
|
switch (_sourceType) {
|
|
case _VideoSourceType.youtube:
|
|
return iframe.YoutubePlayer(controller: _youtubeController!);
|
|
case _VideoSourceType.vimeo:
|
|
if (_vimeoController == null) {
|
|
return Center(
|
|
child: Text(
|
|
"Impossible d'extraire l'identifiant Vimeo depuis l'URL",
|
|
style: TextStyle(fontSize: kNoneInfoOrIncorrect),
|
|
),
|
|
);
|
|
}
|
|
return WebViewWidget(controller: _vimeoController!);
|
|
case _VideoSourceType.direct:
|
|
return VideoViewer(videoUrl: source, file: null);
|
|
default:
|
|
return const SizedBox();
|
|
}
|
|
}
|
|
}
|