Created
January 27, 2022 10:38
-
-
Save followthemoney1/8e6b73eba322cbba57ffd6d6836c6419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:developer'; | |
import 'dart:math' as math; | |
import 'dart:ui' as ui; | |
import 'dart:html' as html; | |
import 'package:flutter/material.dart'; | |
// import 'package:sport_news/ui/widgets/video_players/html_view.dart'; | |
// import 'package:webviewx/webviewx.dart'; | |
void main() { | |
runApp(MaterialApp(home:Container( | |
constraints: const BoxConstraints( | |
maxHeight: 100, | |
maxWidth: 100 | |
), | |
child:YoutubePlayer(url: "https://www.youtube.com/watch?v=HxflFhf5rRA"),),)); | |
} | |
class YoutubePlayer extends StatelessWidget { | |
final divId = math.Random().nextInt(300); | |
String url; | |
YoutubePlayer({ | |
required this.url, | |
}); | |
var showedViewDelay = false; | |
late String? tempUrl; | |
@override | |
Widget build(BuildContext context) { | |
log('get youtube: get link'); | |
tempUrl = _getYoutubeId(url); | |
return LayoutBuilder(builder: (BuildContext ctx, BoxConstraints constraints) { | |
log('get youtube: build youtube'); | |
Size playerSize = Size( | |
constraints.maxWidth, | |
constraints.maxHeight, | |
); | |
log('sizes; ${playerSize}'); | |
final resUrl = 'https://www.youtube.com/embed/' + tempUrl! + "?enablejsapi=1&origin=https://nemesis.wtf"; | |
var loadData = """ | |
<html> | |
<body> | |
<iframe | |
src="${resUrl}" | |
frameborder="0" | |
height="${playerSize.height.round() - 20}" | |
width="${playerSize.width.round() - 15}" | |
frameborder="0" | |
allowfullscreen="true"/> | |
</body> | |
</html> | |
"""; | |
//or | |
// child: WebViewX( | |
// onWebViewCreated: (controller) { | |
// log("loaded web view"); | |
// this.controller = controller; | |
// }, | |
// ), | |
// ); | |
//or | |
return Container( | |
constraints: BoxConstraints(maxHeight: playerSize.height.round() - 20), | |
child: IframeView( | |
htmlSource: loadData, | |
), | |
); | |
}); | |
} | |
String? _getYoutubeId(String link) { | |
RegExp regExp = new RegExp( | |
r'.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*', | |
caseSensitive: false, | |
multiLine: false, | |
); | |
final match = regExp.firstMatch(link)?.group(1); // <- This is the fix | |
// print('$link -> $match'); | |
return match; | |
} | |
} | |
class IframeView extends StatefulWidget { | |
final String htmlSource; | |
IframeView({ | |
Key? key, | |
required this.htmlSource, | |
}) : super(key: key); | |
@override | |
State<StatefulWidget> createState() { | |
// TODO: implement createState | |
return IframeViewState(); | |
} | |
} | |
class IframeViewState extends State<IframeView> with AutomaticKeepAliveClientMixin { | |
final id = math.Random().nextInt(300).toString() + '_twitchIframe'; | |
@override | |
void initState() { | |
super.initState(); | |
html.Element.iframe()..slot = id; | |
//ignore: undefined_prefixed_name | |
ui.platformViewRegistry.registerViewFactory( | |
id, | |
(int viewId) => html.Element.html(widget.htmlSource, | |
validator: new html.NodeValidatorBuilder() | |
..allowHtml5() | |
..allowElement('video', attributes: ['*']) | |
..allowElement('webview', attributes: ['*']) | |
..allowElement('iframe', attributes: ['*'])) | |
..style.width = '100%' | |
..style.height = '100%', | |
); | |
} | |
@override | |
bool get wantKeepAlive => true; | |
@override | |
Widget build(BuildContext context) { | |
return HtmlElementView( | |
// key: ValueKey(id), | |
viewType: id, | |
// onPlatformViewCreated: , | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment