Last active
May 31, 2020 04:03
-
-
Save khatthaphone/491242575aa81b25e525e86cd97d1bd5 to your computer and use it in GitHub Desktop.
Flutter Webview from HTML String
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:async'; | |
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:webview_flutter/webview_flutter.dart'; | |
class WebViewHtmlString extends StatefulWidget { | |
final String html; | |
WebViewFromString(this.html); | |
@override | |
_WebViewFromStringState createState() => _WebViewFromStringState(); | |
} | |
class _WebViewFromStringState extends State<WebViewFromString> { | |
Completer<WebViewController> _controller = Completer(); | |
@override | |
Widget build(BuildContext context) { | |
_loadHtmlFromString(); | |
return WebView(onWebViewCreated: (controller) { | |
_controller.complete(controller); | |
}); | |
} | |
_loadHtmlFromString() { | |
_controller.future.then((controller) { | |
controller.loadUrl(Uri.dataFromString(widget.html, | |
mimeType: 'text/html', encoding: Encoding.getByName('utf-8')) | |
.toString()); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment