main.dart 7.4 KB

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