forked from foss42/apidash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_previewer.dart
61 lines (52 loc) · 1.63 KB
/
html_previewer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import 'package:flutter/material.dart';
import 'package:webview_cef/webview_cef.dart';
class HtmlPreviewer extends StatefulWidget {
const HtmlPreviewer({super.key, required this.url});
final String url;
@override
_HtmlPreviewerState createState() => _HtmlPreviewerState();
}
class _HtmlPreviewerState extends State<HtmlPreviewer> {
final WebViewController _controller = WebViewController();
final _textController = TextEditingController();
String title = "";
Future<void> initPlatformState() async {
_textController.text = widget.url;
await _controller.initialize();
await _controller.loadUrl(widget.url);
_controller.setWebviewListener(WebviewEventsListener(
onTitleChanged: (t) {
setState(() {
title = t;
});
},
onUrlChanged: (url) {
_textController.text = url;
},
));
// ignore: prefer_collection_literals
final Set<JavascriptChannel> jsChannels = [
JavascriptChannel(
name: 'Print',
onMessageReceived: (JavascriptMessage message) {
_controller.sendJavaScriptChannelCallBack(
false,
"{'code':'200','message':'print succeed!'}",
message.callbackId,
message.frameId);
}),
].toSet();
await _controller.setJavaScriptChannels(jsChannels);
await _controller.executeJavaScript("function abc(e){console.log(e)}");
if (!mounted) return;
}
@override
void initState() {
super.initState();
initPlatformState();
}
@override
Widget build(BuildContext context) {
return Expanded(child: WebView(_controller));
}
}