main.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
  4. const kAndroidUserAgent =
  5. "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36";
  6. String selectedUrl = "https://flutter.io";
  7. void main() {
  8. runApp(new MyApp());
  9. }
  10. class MyApp extends StatelessWidget {
  11. @override
  12. Widget build(BuildContext context) {
  13. return new MaterialApp(
  14. title: 'Flutter WebView Demo',
  15. theme: new ThemeData(
  16. primarySwatch: Colors.blue,
  17. ),
  18. routes: {
  19. "/": (_) => new MyHomePage(title: "Flutter WebView Demo"),
  20. "/widget": (_) => new WebviewScaffold(
  21. url: selectedUrl,
  22. appBar: new AppBar(
  23. title: new Text("Widget webview"),
  24. ),
  25. withZoom: true,
  26. withLocalStorage: true,
  27. )
  28. },
  29. );
  30. }
  31. }
  32. class MyHomePage extends StatefulWidget {
  33. MyHomePage({Key key, this.title}) : super(key: key);
  34. final String title;
  35. @override
  36. _MyHomePageState createState() => new _MyHomePageState();
  37. }
  38. class _MyHomePageState extends State<MyHomePage> {
  39. // Instance of WebView plugin
  40. final flutterWebviewPlugin = new FlutterWebviewPlugin();
  41. // On destroy stream
  42. StreamSubscription _onDestroy;
  43. // On urlChanged stream
  44. StreamSubscription<String> _onUrlChanged;
  45. // On urlChanged stream
  46. StreamSubscription<WebViewStateChanged> _onStateChanged;
  47. StreamSubscription<double> _onScrollYChanged,_onScrollXChanged;
  48. TextEditingController _urlCtrl = new TextEditingController(text: selectedUrl);
  49. TextEditingController _codeCtrl =
  50. new TextEditingController(text: "window.navigator.userAgent");
  51. GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
  52. final _history = [];
  53. @override
  54. initState() {
  55. super.initState();
  56. flutterWebviewPlugin.close();
  57. _urlCtrl.addListener(() {
  58. selectedUrl = _urlCtrl.text;
  59. });
  60. // Add a listener to on destroy WebView, so you can make came actions.
  61. _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
  62. if (mounted) {
  63. // Actions like show a info toast.
  64. _scaffoldKey.currentState
  65. .showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
  66. }
  67. });
  68. // Add a listener to on url changed
  69. _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
  70. if (mounted) {
  71. setState(() {
  72. _history.add("onUrlChanged: $url");
  73. });
  74. }
  75. });
  76. _onScrollYChanged = flutterWebviewPlugin.onScrollYChanged.listen((double y) {
  77. if (mounted) {
  78. setState(() {
  79. _history.add("Scroll in Y Direction: $y");
  80. });
  81. }
  82. });
  83. _onScrollXChanged = flutterWebviewPlugin.onScrollXChanged.listen((double x) {
  84. if (mounted) {
  85. setState(() {
  86. _history.add("Scroll in X Direction: $x");
  87. });
  88. }
  89. });
  90. _onStateChanged =
  91. flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
  92. if (mounted) {
  93. setState(() {
  94. _history.add("onStateChanged: ${state.type} ${state.url}");
  95. });
  96. }
  97. });
  98. }
  99. @override
  100. void dispose() {
  101. // Every listener should be canceled, the same should be done with this stream.
  102. _onDestroy.cancel();
  103. _onUrlChanged.cancel();
  104. _onStateChanged.cancel();
  105. _onScrollYChanged.cancel();
  106. _onScrollXChanged.cancel();
  107. flutterWebviewPlugin.dispose();
  108. super.dispose();
  109. }
  110. @override
  111. Widget build(BuildContext context) {
  112. return new Scaffold(
  113. key: _scaffoldKey,
  114. appBar: new AppBar(
  115. title: new Text('Plugin example app'),
  116. ),
  117. body: new Column(
  118. mainAxisAlignment: MainAxisAlignment.center,
  119. children: [
  120. new Container(
  121. padding: const EdgeInsets.all(24.0),
  122. child: new TextField(controller: _urlCtrl),
  123. ),
  124. new RaisedButton(
  125. onPressed: () {
  126. flutterWebviewPlugin.launch(selectedUrl,
  127. rect: new Rect.fromLTWH(
  128. 0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
  129. userAgent: kAndroidUserAgent);
  130. },
  131. child: new Text("Open Webview (rect)"),
  132. ),
  133. new RaisedButton(
  134. onPressed: () {
  135. flutterWebviewPlugin.launch(selectedUrl, hidden: true);
  136. },
  137. child: new Text("Open 'hidden' Webview"),
  138. ),
  139. new RaisedButton(
  140. onPressed: () {
  141. flutterWebviewPlugin.launch(selectedUrl);
  142. },
  143. child: new Text("Open Fullscreen Webview"),
  144. ),
  145. new RaisedButton(
  146. onPressed: () {
  147. Navigator.of(context).pushNamed("/widget");
  148. },
  149. child: new Text("Open widget webview"),
  150. ),
  151. new Container(
  152. padding: const EdgeInsets.all(24.0),
  153. child: new TextField(controller: _codeCtrl),
  154. ),
  155. new RaisedButton(
  156. onPressed: () {
  157. Future<String> future =
  158. flutterWebviewPlugin.evalJavascript(_codeCtrl.text);
  159. future.then((String result) {
  160. setState(() {
  161. _history.add("eval: $result");
  162. });
  163. });
  164. },
  165. child: new Text("Eval some javascript"),
  166. ),
  167. new RaisedButton(
  168. onPressed: () {
  169. setState(() {
  170. _history.clear();
  171. });
  172. flutterWebviewPlugin.close();
  173. },
  174. child: new Text("Close"),
  175. ),
  176. new RaisedButton(
  177. onPressed: () {
  178. flutterWebviewPlugin.getCookies().then((m) {
  179. setState(() {
  180. _history.add("cookies: $m");
  181. });
  182. });
  183. },
  184. child: new Text("Cookies"),
  185. ),
  186. new Text(_history.sublist(_history.length-10, _history.length ).join("\n")),
  187. ],
  188. ),
  189. );
  190. }
  191. }