main.dart 6.5 KB

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