main.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 flutterWebviewPlugin = new FlutterWebViewPlugin();
  30. // On destroy stream
  31. StreamSubscription _onDestroy;
  32. // On urlChanged stream
  33. StreamSubscription<String> _onUrlChanged;
  34. StreamSubscription<String> _onStateChanged;
  35. TextEditingController _urlCtrl =
  36. new TextEditingController(text: "http://github.com");
  37. TextEditingController _codeCtrl =
  38. new TextEditingController(text: "window.navigator.userAgent");
  39. GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
  40. final _history = [];
  41. @override
  42. initState() {
  43. super.initState();
  44. _onStateChanged = flutterWebviewPlugin.stateChanged.listen((dynamic state) {
  45. if (mounted) {
  46. setState(() {
  47. _history.add("stateChanged: $state");
  48. });
  49. }
  50. });
  51. // Add a listener to on destroy WebView, so you can make came actions.
  52. _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
  53. if (mounted) {
  54. // Actions like show a info toast.
  55. _scaffoldKey.currentState
  56. .showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
  57. }
  58. });
  59. // Add a listener to on url changed
  60. _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
  61. if (mounted) {
  62. setState(() {
  63. _history.add("onUrlChanged: $url");
  64. });
  65. }
  66. });
  67. }
  68. @override
  69. void dispose() {
  70. // Every listener should be canceled, the same should be done with this stream.
  71. _onDestroy?.cancel();
  72. _onUrlChanged?.cancel();
  73. super.dispose();
  74. }
  75. @override
  76. Widget build(BuildContext context) {
  77. return new Scaffold(
  78. key: _scaffoldKey,
  79. appBar: new AppBar(
  80. title: new Text('Plugin example app'),
  81. ),
  82. body: new Column(
  83. mainAxisAlignment: MainAxisAlignment.center,
  84. children: [
  85. new Container(
  86. padding: const EdgeInsets.all(24.0),
  87. child: new TextField(controller: _urlCtrl),
  88. ),
  89. new RaisedButton(
  90. onPressed: () {
  91. flutterWebviewPlugin.launch(_urlCtrl.text,
  92. fullScreen: false,
  93. rect: new Rect.fromLTWH(
  94. 0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
  95. userAgent: kAndroidUserAgent);
  96. },
  97. child: new Text("Open Webview (rect)"),
  98. ),
  99. new RaisedButton(
  100. onPressed: () {
  101. flutterWebviewPlugin.launch(_urlCtrl.text, hidden: true);
  102. },
  103. child: new Text("Open 'hidden' Webview"),
  104. ),
  105. new RaisedButton(
  106. onPressed: () {
  107. flutterWebviewPlugin.launch(_urlCtrl.text, fullScreen: true);
  108. },
  109. child: new Text("Open Fullscreen Webview"),
  110. ),
  111. new Container(
  112. padding: const EdgeInsets.all(24.0),
  113. child: new TextField(controller: _codeCtrl),
  114. ),
  115. new RaisedButton(
  116. onPressed: () {
  117. Future<String> future =
  118. flutterWebviewPlugin.evalJavascript(_codeCtrl.text);
  119. future.then((String result) {
  120. setState(() {
  121. _history.add("eval: $result");
  122. });
  123. });
  124. },
  125. child: new Text("Eval some javascript"),
  126. ),
  127. new RaisedButton(
  128. onPressed: () {
  129. _history.clear();
  130. flutterWebviewPlugin.close();
  131. },
  132. child: new Text("Close"),
  133. ),
  134. new Text(_history.join("\n"))
  135. ],
  136. ),
  137. );
  138. }
  139. }