main.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. )
  28. },
  29. );
  30. }
  31. }
  32. class MyHomePage extends StatefulWidget {
  33. const 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<WebViewHttpError> _onHttpError;
  48. final _urlCtrl = new TextEditingController(text: selectedUrl);
  49. final _codeCtrl =
  50. new TextEditingController(text: 'window.navigator.userAgent');
  51. final _scaffoldKey = new GlobalKey<ScaffoldState>();
  52. final _history = [];
  53. @override
  54. void 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.showSnackBar(
  65. const SnackBar(content: const 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 =
  77. flutterWebviewPlugin.onScrollYChanged.listen((double y) {
  78. if (mounted) {
  79. setState(() {
  80. _history.add("Scroll in Y Direction: $y");
  81. });
  82. }
  83. });
  84. _onScrollXChanged =
  85. flutterWebviewPlugin.onScrollXChanged.listen((double x) {
  86. if (mounted) {
  87. setState(() {
  88. _history.add("Scroll in X Direction: $x");
  89. });
  90. }
  91. });
  92. _onStateChanged =
  93. flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
  94. if (mounted) {
  95. setState(() {
  96. _history.add('onStateChanged: ${state.type} ${state.url}');
  97. });
  98. }
  99. });
  100. _onHttpError =
  101. flutterWebviewPlugin.onHttpError.listen((WebViewHttpError error) {
  102. if (mounted) {
  103. setState(() {
  104. _history.add('onHttpError: ${error.code} ${error.url}');
  105. });
  106. }
  107. });
  108. }
  109. @override
  110. void dispose() {
  111. // Every listener should be canceled, the same should be done with this stream.
  112. _onDestroy.cancel();
  113. _onUrlChanged.cancel();
  114. _onStateChanged.cancel();
  115. _onHttpError.cancel();
  116. flutterWebviewPlugin.dispose();
  117. super.dispose();
  118. }
  119. @override
  120. Widget build(BuildContext context) {
  121. return new Scaffold(
  122. key: _scaffoldKey,
  123. appBar: new AppBar(
  124. title: const Text('Plugin example app'),
  125. ),
  126. body: new Column(
  127. mainAxisAlignment: MainAxisAlignment.center,
  128. children: [
  129. new Container(
  130. padding: const EdgeInsets.all(24.0),
  131. child: new TextField(controller: _urlCtrl),
  132. ),
  133. new RaisedButton(
  134. onPressed: () {
  135. flutterWebviewPlugin.launch(selectedUrl,
  136. rect: new Rect.fromLTWH(
  137. 0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
  138. userAgent: kAndroidUserAgent);
  139. },
  140. child: const Text('Open Webview (rect)'),
  141. ),
  142. new RaisedButton(
  143. onPressed: () {
  144. flutterWebviewPlugin.launch(selectedUrl, hidden: true);
  145. },
  146. child: const Text('Open "hidden" Webview'),
  147. ),
  148. new RaisedButton(
  149. onPressed: () {
  150. flutterWebviewPlugin.launch(selectedUrl);
  151. },
  152. child: const Text('Open Fullscreen Webview'),
  153. ),
  154. new RaisedButton(
  155. onPressed: () {
  156. Navigator.of(context).pushNamed('/widget');
  157. },
  158. child: const Text('Open widget webview'),
  159. ),
  160. new Container(
  161. padding: const EdgeInsets.all(24.0),
  162. child: new TextField(controller: _codeCtrl),
  163. ),
  164. new RaisedButton(
  165. onPressed: () {
  166. final future =
  167. flutterWebviewPlugin.evalJavascript(_codeCtrl.text);
  168. future.then((String result) {
  169. setState(() {
  170. _history.add('eval: $result');
  171. });
  172. });
  173. },
  174. child: const Text('Eval some javascript'),
  175. ),
  176. new RaisedButton(
  177. onPressed: () {
  178. setState(() {
  179. _history.clear();
  180. });
  181. flutterWebviewPlugin.close();
  182. },
  183. child: const Text('Close'),
  184. ),
  185. new RaisedButton(
  186. onPressed: () {
  187. flutterWebviewPlugin.getCookies().then((m) {
  188. setState(() {
  189. _history.add('cookies: $m');
  190. });
  191. });
  192. },
  193. child: const Text('Cookies'),
  194. ),
  195. new Text(_history.join('\n'))
  196. ],
  197. ),
  198. );
  199. }
  200. }