main.dart 4.2 KB

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