main.dart 7.5 KB

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