Skip to content

Instantly share code, notes, and snippets.

@khatthaphone
Last active May 31, 2020 04:03
Show Gist options
  • Save khatthaphone/491242575aa81b25e525e86cd97d1bd5 to your computer and use it in GitHub Desktop.
Save khatthaphone/491242575aa81b25e525e86cd97d1bd5 to your computer and use it in GitHub Desktop.
Flutter Webview from HTML String
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