main.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 =
  37. new TextEditingController(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 = flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
  62. if (mounted) {
  63. setState(() {
  64. _history.add("onStateChanged: ${state.type} ${state.url}");
  65. });
  66. }
  67. });
  68. }
  69. @override
  70. void dispose() {
  71. // Every listener should be canceled, the same should be done with this stream.
  72. _onDestroy.cancel();
  73. _onUrlChanged.cancel();
  74. _onStateChanged.cancel();
  75. flutterWebviewPlugin.dispose();
  76. super.dispose();
  77. }
  78. @override
  79. Widget build(BuildContext context) {
  80. return new Scaffold(
  81. key: _scaffoldKey,
  82. appBar: new AppBar(
  83. title: new Text('Plugin example app'),
  84. ),
  85. body: new Column(
  86. mainAxisAlignment: MainAxisAlignment.center,
  87. children: [
  88. new Container(
  89. padding: const EdgeInsets.all(24.0),
  90. child: new TextField(controller: _urlCtrl),
  91. ),
  92. new RaisedButton(
  93. onPressed: () {
  94. flutterWebviewPlugin.launch(_urlCtrl.text,
  95. fullScreen: false,
  96. rect: new Rect.fromLTWH(
  97. 0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
  98. userAgent: kAndroidUserAgent);
  99. },
  100. child: new Text("Open Webview (rect)"),
  101. ),
  102. new RaisedButton(
  103. onPressed: () {
  104. flutterWebviewPlugin.launch(_urlCtrl.text, hidden: true);
  105. },
  106. child: new Text("Open 'hidden' Webview"),
  107. ),
  108. new RaisedButton(
  109. onPressed: () {
  110. flutterWebviewPlugin.launch(_urlCtrl.text, fullScreen: true);
  111. },
  112. child: new Text("Open Fullscreen Webview"),
  113. ),
  114. new Container(
  115. padding: const EdgeInsets.all(24.0),
  116. child: new TextField(controller: _codeCtrl),
  117. ),
  118. new RaisedButton(
  119. onPressed: () {
  120. Future<String> future =
  121. flutterWebviewPlugin.evalJavascript(_codeCtrl.text);
  122. future.then((String result) {
  123. setState(() {
  124. _history.add("eval: $result");
  125. });
  126. });
  127. },
  128. child: new Text("Eval some javascript"),
  129. ),
  130. new RaisedButton(
  131. onPressed: () {
  132. setState(() {
  133. _history.clear();
  134. });
  135. flutterWebviewPlugin.close();
  136. },
  137. child: new Text("Close"),
  138. ),
  139. new Text(_history.join("\n"))
  140. ],
  141. ),
  142. );
  143. }
  144. }