Created
May 9, 2023 13:43
-
-
Save followthemoney1/66d1a97fa3963dd1b6b879d8193b56df to your computer and use it in GitHub Desktop.
dart, iframe using
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 'dart:html' as html; | |
import 'dart:ui' as ui; | |
import 'package:flutter/material.dart'; | |
import 'dart:math' as math; | |
// class IframeView extends StatefulWidget { | |
// final String html; | |
// const IframeView({ | |
// Key? key, | |
// required this.html, | |
// }) : super(key: key); | |
// @override | |
// _IframeViewState createState() => _IframeViewState(); | |
// } | |
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%', | |
); | |
html.window.addEventListener('message', listen, true); | |
} | |
void listen(html.Event event) { | |
final data = (event as html.MessageEvent).data; | |
if(data!=null){ | |
} | |
} | |
@override | |
bool get wantKeepAlive => true; | |
@override | |
Widget build(BuildContext context) { | |
return HtmlElementView( | |
// key: ValueKey(id), | |
viewType: id, | |
// onPlatformViewCreated: , | |
); | |
} | |
} |
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
class YoutubePlayer extends StatelessWidget { | |
final divId = math.Random().nextInt(300); | |
String url; | |
YoutubePlayer({ | |
required this.url, | |
}); | |
var showedViewDelay = false; | |
late String? tempUrl; | |
WebViewXController? controller; | |
@override | |
Widget build(BuildContext context) { | |
// if (!showedViewDelay || tempUrl == null) | |
// return Card( | |
// color: Colors.white.withOpacity(0.1), | |
// ); | |
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> | |
"""; | |
if (controller != null) { | |
controller!.reload(); | |
controller!.loadContent(loadData, SourceType.html); | |
} | |
// child: WebViewX( | |
// onWebViewCreated: (controller) { | |
// log("loaded web view"); | |
// this.controller = controller; | |
// }, | |
// ), | |
// ); | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment