main.dart 6.2 KB

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