main.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. '/': (_) => const MyHomePage(title: 'Flutter WebView Demo'),
  20. '/widget': (_) => new WebviewScaffold(
  21. url: selectedUrl,
  22. appBar: new AppBar(
  23. title: const Text('Widget webview'),
  24. ),
  25. withZoom: true,
  26. withLocalStorage: true,
  27. hidden: true,
  28. initialChild: Container(
  29. color: Colors.redAccent,
  30. child: const Center(
  31. child: Text('Waiting.....'),
  32. ),
  33. ),
  34. )
  35. },
  36. );
  37. }
  38. }
  39. class MyHomePage extends StatefulWidget {
  40. const MyHomePage({Key key, this.title}) : super(key: key);
  41. final String title;
  42. @override
  43. _MyHomePageState createState() => new _MyHomePageState();
  44. }
  45. class _MyHomePageState extends State<MyHomePage> {
  46. // Instance of WebView plugin
  47. final flutterWebviewPlugin = new FlutterWebviewPlugin();
  48. // On destroy stream
  49. StreamSubscription _onDestroy;
  50. // On urlChanged stream
  51. StreamSubscription<String> _onUrlChanged;
  52. // On urlChanged stream
  53. StreamSubscription<WebViewStateChanged> _onStateChanged;
  54. StreamSubscription<WebViewHttpError> _onHttpError;
  55. StreamSubscription<double> _onScrollYChanged;
  56. StreamSubscription<double> _onScrollXChanged;
  57. final _urlCtrl = new TextEditingController(text: selectedUrl);
  58. final _codeCtrl =
  59. new TextEditingController(text: 'window.navigator.userAgent');
  60. final _scaffoldKey = new GlobalKey<ScaffoldState>();
  61. final _history = [];
  62. @override
  63. void initState() {
  64. super.initState();
  65. flutterWebviewPlugin.close();
  66. _urlCtrl.addListener(() {
  67. selectedUrl = _urlCtrl.text;
  68. });
  69. // Add a listener to on destroy WebView, so you can make came actions.
  70. _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
  71. if (mounted) {
  72. // Actions like show a info toast.
  73. _scaffoldKey.currentState.showSnackBar(
  74. const SnackBar(content: const Text('Webview Destroyed')));
  75. }
  76. });
  77. // Add a listener to on url changed
  78. _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
  79. if (mounted) {
  80. setState(() {
  81. _history.add('onUrlChanged: $url');
  82. });
  83. }
  84. });
  85. _onScrollYChanged =
  86. flutterWebviewPlugin.onScrollYChanged.listen((double y) {
  87. if (mounted) {
  88. setState(() {
  89. _history.add("Scroll in Y Direction: $y");
  90. });
  91. }
  92. });
  93. _onScrollXChanged =
  94. flutterWebviewPlugin.onScrollXChanged.listen((double x) {
  95. if (mounted) {
  96. setState(() {
  97. _history.add("Scroll in X Direction: $x");
  98. });
  99. }
  100. });
  101. _onStateChanged =
  102. flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
  103. if (mounted) {
  104. setState(() {
  105. _history.add('onStateChanged: ${state.type} ${state.url}');
  106. });
  107. }
  108. });
  109. _onHttpError =
  110. flutterWebviewPlugin.onHttpError.listen((WebViewHttpError error) {
  111. if (mounted) {
  112. setState(() {
  113. _history.add('onHttpError: ${error.code} ${error.url}');
  114. });
  115. }
  116. });
  117. }
  118. @override
  119. void dispose() {
  120. // Every listener should be canceled, the same should be done with this stream.
  121. _onDestroy.cancel();
  122. _onUrlChanged.cancel();
  123. _onStateChanged.cancel();
  124. _onHttpError.cancel();
  125. _onScrollXChanged.cancel();
  126. _onScrollYChanged.cancel();
  127. flutterWebviewPlugin.dispose();
  128. super.dispose();
  129. }
  130. @override
  131. Widget build(BuildContext context) {
  132. return new Scaffold(
  133. key: _scaffoldKey,
  134. appBar: new AppBar(
  135. title: const Text('Plugin example app'),
  136. ),
  137. body: new Column(
  138. mainAxisAlignment: MainAxisAlignment.center,
  139. children: [
  140. new Container(
  141. padding: const EdgeInsets.all(24.0),
  142. child: new TextField(controller: _urlCtrl),
  143. ),
  144. new RaisedButton(
  145. onPressed: () {
  146. flutterWebviewPlugin.launch(selectedUrl,
  147. rect: new Rect.fromLTWH(
  148. 0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
  149. userAgent: kAndroidUserAgent);
  150. },
  151. child: const Text('Open Webview (rect)'),
  152. ),
  153. new RaisedButton(
  154. onPressed: () {
  155. flutterWebviewPlugin.launch(selectedUrl, hidden: true);
  156. },
  157. child: const Text('Open "hidden" Webview'),
  158. ),
  159. new RaisedButton(
  160. onPressed: () {
  161. flutterWebviewPlugin.launch(selectedUrl);
  162. },
  163. child: const Text('Open Fullscreen Webview'),
  164. ),
  165. new RaisedButton(
  166. onPressed: () {
  167. Navigator.of(context).pushNamed('/widget');
  168. },
  169. child: const Text('Open widget webview'),
  170. ),
  171. new Container(
  172. padding: const EdgeInsets.all(24.0),
  173. child: new TextField(controller: _codeCtrl),
  174. ),
  175. new RaisedButton(
  176. onPressed: () {
  177. final future =
  178. flutterWebviewPlugin.evalJavascript(_codeCtrl.text);
  179. future.then((String result) {
  180. setState(() {
  181. _history.add('eval: $result');
  182. });
  183. });
  184. },
  185. child: const Text('Eval some javascript'),
  186. ),
  187. new RaisedButton(
  188. onPressed: () {
  189. setState(() {
  190. _history.clear();
  191. });
  192. flutterWebviewPlugin.close();
  193. },
  194. child: const Text('Close'),
  195. ),
  196. new RaisedButton(
  197. onPressed: () {
  198. flutterWebviewPlugin.getCookies().then((m) {
  199. setState(() {
  200. _history.add('cookies: $m');
  201. });
  202. });
  203. },
  204. child: const Text('Cookies'),
  205. ),
  206. new Text(_history.join('\n'))
  207. ],
  208. ),
  209. );
  210. }
  211. }