Last active
May 14, 2022 14:29
-
-
Save rutvik110/816b53e7459bbee6185549c73a71a731 to your computer and use it in GitHub Desktop.
Twitter embed with WebView
This file contains hidden or 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 TwitterEmbed extends StatefulWidget { | |
const TwitterEmbed({Key? key}) : super(key: key); | |
@override | |
State<TwitterEmbed> createState() => _TwitterEmbedState(); | |
} | |
class _TwitterEmbedState extends State<TwitterEmbed> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("Twitter Embed"), | |
), | |
body: WebView( | |
initialUrl: Uri.dataFromString( | |
getHtmlString("1524510341165576192"), | |
mimeType: 'text/html', | |
encoding: Encoding.getByName('utf-8'), | |
).toString(), | |
javascriptMode: JavascriptMode.unrestricted, | |
), | |
); | |
} | |
} | |
String getHtmlString(String tweetId) { | |
return """ | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body> | |
<div id="container"></div> | |
</body> | |
<script id="twitter-wjs" type="text/javascript" async defer src="https://platform.twitter.com/widgets.js" onload="createMyTweet()"></script> | |
<script> | |
function createMyTweet() { | |
var twtter = window.twttr; | |
twttr.widgets.createTweet( | |
'$tweetId', | |
document.getElementById('container'), | |
) | |
} | |
</script> | |
</html> | |
"""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment