|
| 1 | +import 'dart:convert'; |
| 2 | + |
1 | 3 | import 'package:flutter/cupertino.dart'; |
2 | 4 | import 'package:flutter/material.dart'; |
3 | 5 | import 'package:flutter_webview_plugin/flutter_webview_plugin.dart'; |
@@ -27,26 +29,72 @@ class GSYWebView extends StatelessWidget { |
27 | 29 | ]); |
28 | 30 | } |
29 | 31 |
|
| 32 | + final FocusNode focusNode = new FocusNode(); |
30 | 33 | @override |
31 | 34 | Widget build(BuildContext context) { |
32 | | - |
33 | 35 | /*return Scaffold( |
34 | 36 | appBar: new AppBar( |
35 | 37 | title: _renderTitle(), |
36 | 38 | ), |
37 | | - body: WebView( |
38 | | - initialUrl: url, |
39 | | - javascriptMode: JavascriptMode.unrestricted, |
| 39 | + body: new Stack( |
| 40 | + children: <Widget>[ |
| 41 | + TextField( |
| 42 | + focusNode: focusNode, |
| 43 | + ), |
| 44 | + WebView( |
| 45 | + initialUrl: new Uri.dataFromString(html, mimeType: 'text/html', encoding: Encoding.getByName("utf-8")).toString(), |
| 46 | + javascriptMode: JavascriptMode.unrestricted, |
| 47 | + javascriptChannels: Set.from([ |
| 48 | + JavascriptChannel( |
| 49 | + name: 'Print', |
| 50 | + onMessageReceived: (JavascriptMessage message) { |
| 51 | + print("FFFFFF"); |
| 52 | + print(message.message); |
| 53 | + FocusScope.of(context).requestFocus(focusNode); |
| 54 | + }) |
| 55 | + ])) |
| 56 | + ], |
40 | 57 | ), |
41 | 58 | );*/ |
42 | 59 | return new WebviewScaffold( |
43 | 60 | withJavascript: true, |
44 | 61 | url: url, |
45 | | - scrollBar:true, |
| 62 | + scrollBar: true, |
46 | 63 | withLocalUrl: true, |
47 | 64 | appBar: new AppBar( |
48 | 65 | title: _renderTitle(), |
49 | 66 | ), |
50 | 67 | ); |
51 | 68 | } |
52 | 69 | } |
| 70 | + |
| 71 | + |
| 72 | +///测试 html 代码,不管 |
| 73 | +final testhtml = "<!DOCTYPE html>" |
| 74 | + "<html>" |
| 75 | + "<head>" |
| 76 | + "<meta charset=\"utf-8\">" |
| 77 | + "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no\" />" |
| 78 | + "<title>Local Title</title>" |
| 79 | + "<script>" |
| 80 | + "function callJS(){" |
| 81 | + "alert(\"Android调用了web js\");" |
| 82 | + "}" |
| 83 | + "function callInterface(){" |
| 84 | + "JSCallBackInterface.callback(\"我是web的js哟\");" |
| 85 | + "}" |
| 86 | + "function callInterface2(){" |
| 87 | + "document.location = \"js://Authority?pra1=111&pra2=222\";" |
| 88 | + "}" |
| 89 | + "function clickPrompt(){" |
| 90 | + "Print.postMessage('Hello');" |
| 91 | + "}" |
| 92 | + "</script>" |
| 93 | + "</head>" |
| 94 | + "<body>" |
| 95 | + "<button type=\"button\" id=\"buttonxx\" onclick=\"callInterface()\">点我调用原生android方法</button>" |
| 96 | + "<button type=\"button\" id=\"buttonxx2\" onclick=\"callInterface2()\">点我调用原生android方法2</button>" |
| 97 | + "<button type=\"button\" id=\"buttonxx3\" onclick=\"clickPrompt()\">点我调用原生android方法3</button>" |
| 98 | + "<input></input>" |
| 99 | + "</body>" |
| 100 | + "</html>"; |
0 commit comments