main.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 =
  45. flutterWebviewPlugin.onStateChanged.listen((dynamic state) {
  46. if (mounted) {
  47. setState(() {
  48. _history.add("state: $state");
  49. });
  50. }
  51. });
  52. // Add a listener to on destroy WebView, so you can make came actions.
  53. _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
  54. if (mounted) {
  55. // Actions like show a info toast.
  56. _scaffoldKey.currentState
  57. .showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
  58. }
  59. });
  60. // Add a listener to on url changed
  61. _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
  62. if (mounted) {
  63. setState(() {
  64. _history.add("onUrlChanged: $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. super.dispose();
  75. }
  76. @override
  77. Widget build(BuildContext context) {
  78. return new Scaffold(
  79. key: _scaffoldKey,
  80. appBar: new AppBar(
  81. title: new Text('Plugin example app'),
  82. ),
  83. body: new Column(
  84. mainAxisAlignment: MainAxisAlignment.center,
  85. children: [
  86. new Container(
  87. padding: const EdgeInsets.all(24.0),
  88. child: new TextField(controller: _urlCtrl),
  89. ),
  90. new RaisedButton(
  91. onPressed: () {
  92. flutterWebviewPlugin.launch(_urlCtrl.text,
  93. fullScreen: false,
  94. rect: new Rect.fromLTWH(
  95. 0.0, 0.0, MediaQuery.of(context).size.width, 300.0),
  96. userAgent: kAndroidUserAgent);
  97. },
  98. child: new Text("Open Webview (rect)"),
  99. ),
  100. new RaisedButton(
  101. onPressed: () {
  102. flutterWebviewPlugin.launch(_urlCtrl.text, hidden: true);
  103. },
  104. child: new Text("Open 'hidden' Webview"),
  105. ),
  106. new RaisedButton(
  107. onPressed: () {
  108. flutterWebviewPlugin.launch(_urlCtrl.text, fullScreen: true);
  109. },
  110. child: new Text("Open Fullscreen Webview"),
  111. ),
  112. new Container(
  113. padding: const EdgeInsets.all(24.0),
  114. child: new TextField(controller: _codeCtrl),
  115. ),
  116. new RaisedButton(
  117. onPressed: () {
  118. Future<String> future =
  119. flutterWebviewPlugin.evalJavascript(_codeCtrl.text);
  120. future.then((String result) {
  121. setState(() {
  122. _history.add("eval: $result");
  123. });
  124. });
  125. },
  126. child: new Text("Eval some javascript"),
  127. ),
  128. new RaisedButton(
  129. onPressed: () {
  130. setState(() {
  131. _history.clear();
  132. });
  133. flutterWebviewPlugin.close();
  134. },
  135. child: new Text("Close"),
  136. ),
  137. new Text(_history.join("\n"))
  138. ],
  139. ),
  140. );
  141. }
  142. }