main.dart 4.0 KB

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