main.dart 7.2 KB

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