main.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. void main() {
  7. runApp(new MyApp());
  8. }
  9. class MyApp extends StatelessWidget {
  10. @override
  11. Widget build(BuildContext context) {
  12. return new MaterialApp(
  13. title: 'Flutter WebView Demo',
  14. theme: new ThemeData(
  15. primarySwatch: Colors.blue,
  16. ),
  17. home: new MyHomePage(title: 'Flutter WebView Demo'),
  18. );
  19. }
  20. }
  21. class MyHomePage extends StatefulWidget {
  22. MyHomePage({Key key, this.title}) : super(key: key);
  23. final String title;
  24. @override
  25. _MyHomePageState createState() => new _MyHomePageState();
  26. }
  27. class _MyHomePageState extends State<MyHomePage> {
  28. // Instance of WebView plugin
  29. final flutterWebviewPlugin = new FlutterWebviewPlugin();
  30. // On destroy stream
  31. StreamSubscription _onDestroy;
  32. // On urlChanged stream
  33. StreamSubscription<String> _onUrlChanged;
  34. // On urlChanged stream
  35. StreamSubscription<WebViewStateChanged> _onStateChanged;
  36. TextEditingController _urlCtrl = new TextEditingController(
  37. text: "https://github.com/dart-flitter/flutter_webview_plugin");
  38. TextEditingController _codeCtrl =
  39. new TextEditingController(text: "window.navigator.userAgent");
  40. GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
  41. final _history = [];
  42. @override
  43. initState() {
  44. super.initState();
  45. // Add a listener to on destroy WebView, so you can make came actions.
  46. _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
  47. if (mounted) {
  48. // Actions like show a info toast.
  49. _scaffoldKey.currentState
  50. .showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
  51. }
  52. });
  53. // Add a listener to on url changed
  54. _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
  55. if (mounted) {
  56. setState(() {
  57. _history.add("onUrlChanged: $url");
  58. });
  59. }
  60. });
  61. _onStateChanged =
  62. flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
  63. if (mounted) {
  64. setState(() {
  65. _history.add("onStateChanged: ${state.type} ${state.url}");
  66. });
  67. }
  68. });
  69. }
  70. @override
  71. void dispose() {
  72. // Every listener should be canceled, the same should be done with this stream.
  73. _onDestroy.cancel();
  74. _onUrlChanged.cancel();
  75. _onStateChanged.cancel();
  76. flutterWebviewPlugin.dispose();
  77. super.dispose();
  78. }
  79. @override
  80. Widget build(BuildContext context) {
  81. return new Scaffold(
  82. key: _scaffoldKey,
  83. appBar: new AppBar(
  84. title: new Text('Plugin example app'),
  85. ),
  86. body: new Column(
  87. mainAxisAlignment: MainAxisAlignment.center,
  88. children: [
  89. new Container(
  90. padding: const EdgeInsets.all(24.0),
  91. child: new TextField(controller: _urlCtrl),
  92. ),
  93. new RaisedButton(
  94. onPressed: () {
  95. flutterWebviewPlugin.launch(_urlCtrl.text,
  96. fullScreen: false,
  97. rect: new Rect.fromLTWH(
  98. 0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
  99. userAgent: kAndroidUserAgent);
  100. },
  101. child: new Text("Open Webview (rect)"),
  102. ),
  103. new RaisedButton(
  104. onPressed: () {
  105. flutterWebviewPlugin.launch(_urlCtrl.text, hidden: true);
  106. },
  107. child: new Text("Open 'hidden' Webview"),
  108. ),
  109. new RaisedButton(
  110. onPressed: () {
  111. flutterWebviewPlugin.launch(_urlCtrl.text, fullScreen: true);
  112. },
  113. child: new Text("Open Fullscreen Webview"),
  114. ),
  115. new Container(
  116. padding: const EdgeInsets.all(24.0),
  117. child: new TextField(controller: _codeCtrl),
  118. ),
  119. new RaisedButton(
  120. onPressed: () {
  121. Future<String> future =
  122. flutterWebviewPlugin.evalJavascript(_codeCtrl.text);
  123. future.then((String result) {
  124. setState(() {
  125. _history.add("eval: $result");
  126. });
  127. });
  128. },
  129. child: new Text("Eval some javascript"),
  130. ),
  131. new RaisedButton(
  132. onPressed: () {
  133. setState(() {
  134. _history.clear();
  135. });
  136. flutterWebviewPlugin.close();
  137. },
  138. child: new Text("Close"),
  139. ),
  140. new RaisedButton(
  141. onPressed: () {
  142. flutterWebviewPlugin.getCookies().then((m) {
  143. setState(() {
  144. _history.add("cookies: $m");
  145. });
  146. });
  147. },
  148. child: new Text("Cookies"),
  149. ),
  150. new Text(_history.join("\n"))
  151. ],
  152. ),
  153. );
  154. }
  155. }