main.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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: SingleChildScrollView(
  155. child: new Column(
  156. mainAxisAlignment: MainAxisAlignment.center,
  157. children: [
  158. new Container(
  159. padding: const EdgeInsets.all(24.0),
  160. child: new TextField(controller: _urlCtrl),
  161. ),
  162. new RaisedButton(
  163. onPressed: () {
  164. flutterWebviewPlugin.launch(selectedUrl,
  165. rect: new Rect.fromLTWH(0.0, 0.0, MediaQuery.of(context).size.width, 300.0), userAgent: kAndroidUserAgent);
  166. },
  167. child: const Text('Open Webview (rect)'),
  168. ),
  169. new RaisedButton(
  170. onPressed: () {
  171. flutterWebviewPlugin.launch(selectedUrl, hidden: true);
  172. },
  173. child: const Text('Open "hidden" Webview'),
  174. ),
  175. new RaisedButton(
  176. onPressed: () {
  177. flutterWebviewPlugin.launch(selectedUrl);
  178. },
  179. child: const Text('Open Fullscreen Webview'),
  180. ),
  181. new RaisedButton(
  182. onPressed: () {
  183. Navigator.of(context).pushNamed('/widget');
  184. },
  185. child: const Text('Open widget webview'),
  186. ),
  187. new Container(
  188. padding: const EdgeInsets.all(24.0),
  189. child: new TextField(controller: _codeCtrl),
  190. ),
  191. new RaisedButton(
  192. onPressed: () {
  193. final future = flutterWebviewPlugin.evalJavascript(_codeCtrl.text);
  194. future.then((String result) {
  195. setState(() {
  196. _history.add('eval: $result');
  197. });
  198. });
  199. },
  200. child: const Text('Eval some javascript'),
  201. ),
  202. new RaisedButton(
  203. onPressed: () {
  204. setState(() {
  205. _history.clear();
  206. });
  207. flutterWebviewPlugin.close();
  208. },
  209. child: const Text('Close'),
  210. ),
  211. new RaisedButton(
  212. onPressed: () {
  213. flutterWebviewPlugin.getCookies().then((m) {
  214. setState(() {
  215. _history.add('cookies: $m');
  216. });
  217. });
  218. },
  219. child: const Text('Cookies'),
  220. ),
  221. new Text(_history.join('\n'))
  222. ],
  223. ),
  224. ),
  225. );
  226. }
  227. }