main.dart 8.4 KB

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