2023-04-03 21:11:25 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:relatica/controls/audio_video/media_kit_av_control.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
|
|
|
import '../../globals.dart';
|
|
|
|
import '../../services/setting_service.dart';
|
|
|
|
import '../../utils/snackbar_builder.dart';
|
|
|
|
import 'video_player_lib_av_control.dart';
|
|
|
|
|
|
|
|
final _shownVideos = <String>{};
|
|
|
|
|
2023-04-26 19:58:33 +00:00
|
|
|
final _useVideoPlayer = kIsWeb;
|
|
|
|
final _useMediaKit = Platform.isAndroid ||
|
|
|
|
Platform.isIOS ||
|
2023-04-04 17:50:52 +00:00
|
|
|
Platform.isWindows ||
|
|
|
|
Platform.isMacOS ||
|
2023-05-02 00:19:37 +00:00
|
|
|
(kReleaseMode && Platform.isLinux);
|
2023-04-03 21:11:25 +00:00
|
|
|
|
|
|
|
class AVControl extends StatefulWidget {
|
|
|
|
final String videoUrl;
|
|
|
|
final String description;
|
2023-04-13 14:30:09 +00:00
|
|
|
final double? width;
|
|
|
|
final double? height;
|
2023-04-26 19:58:33 +00:00
|
|
|
final bool alwaysShow;
|
|
|
|
final Function()? onGoFullScreen;
|
2023-04-03 21:11:25 +00:00
|
|
|
|
|
|
|
const AVControl({
|
|
|
|
super.key,
|
|
|
|
required this.videoUrl,
|
2023-04-13 14:30:09 +00:00
|
|
|
this.width,
|
|
|
|
this.height,
|
2023-04-26 19:58:33 +00:00
|
|
|
this.alwaysShow = false,
|
|
|
|
this.onGoFullScreen,
|
2023-04-03 21:11:25 +00:00
|
|
|
required this.description,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<AVControl> createState() => _AVControlState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AVControlState extends State<AVControl> {
|
|
|
|
void showVideo() {
|
|
|
|
_shownVideos.add(widget.videoUrl);
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-04-26 19:58:33 +00:00
|
|
|
final shown = widget.alwaysShow ||
|
|
|
|
!context.watch<SettingsService>().lowBandwidthMode ||
|
2023-04-03 21:11:25 +00:00
|
|
|
_shownVideos.contains(widget.videoUrl);
|
|
|
|
|
|
|
|
final placeHolderBox = SizedBox(
|
|
|
|
width: widget.width,
|
|
|
|
height: widget.height,
|
|
|
|
child: const Card(
|
|
|
|
color: Colors.black12,
|
|
|
|
shape: RoundedRectangleBorder(),
|
|
|
|
child: Icon(Icons.movie)),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!shown) {
|
|
|
|
return GestureDetector(
|
|
|
|
onTap: showVideo,
|
|
|
|
child: placeHolderBox,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-26 19:58:33 +00:00
|
|
|
_shownVideos.add(widget.videoUrl);
|
|
|
|
|
2023-04-03 21:11:25 +00:00
|
|
|
if (_useVideoPlayer) {
|
|
|
|
return VideoPlayerLibAvControl(
|
|
|
|
videoUrl: widget.videoUrl,
|
|
|
|
width: widget.width,
|
|
|
|
height: widget.height,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_useMediaKit) {
|
|
|
|
return MediaKitAvControl(
|
|
|
|
videoUrl: widget.videoUrl,
|
|
|
|
width: widget.width,
|
|
|
|
height: widget.height,
|
2023-04-26 19:58:33 +00:00
|
|
|
onGoFullScreen: widget.onGoFullScreen,
|
2023-04-03 21:11:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildStandinWidget(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget buildStandinWidget(BuildContext context) {
|
|
|
|
return GestureDetector(
|
|
|
|
onTap: () async {
|
|
|
|
final confirm = await showYesNoDialog(
|
|
|
|
context, 'Open Link in external app? ${widget.videoUrl}');
|
|
|
|
if (confirm != true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (await canLaunchUrl(Uri.parse(widget.videoUrl))) {
|
|
|
|
if (context.mounted) {
|
|
|
|
buildSnackbar(
|
|
|
|
context,
|
|
|
|
'Attempting to launch video: ${widget.videoUrl}',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await launchUrl(Uri.parse(widget.videoUrl));
|
|
|
|
} else {
|
|
|
|
if (context.mounted) {
|
|
|
|
buildSnackbar(
|
|
|
|
context, 'Unable to launch video: ${widget.videoUrl}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: SizedBox(
|
|
|
|
height: widget.height,
|
|
|
|
width: widget.width,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Card(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Text(
|
|
|
|
widget.description.isNotEmpty
|
|
|
|
? widget.description
|
|
|
|
: 'Video: ${widget.videoUrl}',
|
|
|
|
softWrap: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|