main.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 _ctrl =
  34. new TextEditingController(text: "https://flutter.io");
  35. GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
  36. final _history = [];
  37. @override
  38. initState() {
  39. super.initState();
  40. _onStateChanged = flutterWebviewPlugin.stateChanged.listen((String state) {
  41. if (mounted) {
  42. setState(() {
  43. _history.add(state);
  44. });
  45. }
  46. });
  47. // Add a listener to on destroy WebView, so you can make came actions.
  48. _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
  49. if (mounted) {
  50. // Actions like show a info toast.
  51. _scaffoldKey.currentState
  52. .showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
  53. }
  54. });
  55. // Add a listener to on url changed
  56. _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
  57. if (mounted) {
  58. setState(() {
  59. _history.add(url);
  60. });
  61. }
  62. });
  63. }
  64. @override
  65. void dispose() {
  66. // Every listener should be canceled, the same should be done with this stream.
  67. _onDestroy?.cancel();
  68. _onUrlChanged?.cancel();
  69. super.dispose();
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. return new Scaffold(
  74. key: _scaffoldKey,
  75. appBar: new AppBar(
  76. title: new Text('Plugin example app'),
  77. ),
  78. body: new Column(
  79. mainAxisAlignment: MainAxisAlignment.center,
  80. children: [
  81. new Container(
  82. padding: const EdgeInsets.all(24.0),
  83. child: new TextField(controller: _ctrl),
  84. ),
  85. new RaisedButton(
  86. onPressed: () {
  87. flutterWebviewPlugin.launch(_ctrl.text,
  88. fullScreen: false,
  89. rect: new Rect.fromLTWH(
  90. 0.0, 0.0, MediaQuery.of(context).size.width, 300.0));
  91. },
  92. child: new Text("Open Webview"),
  93. ),
  94. new RaisedButton(
  95. onPressed: () {
  96. flutterWebviewPlugin.launch(_ctrl.text, fullScreen: true);
  97. },
  98. child: new Text("Open Fullscreen Webview"),
  99. ),
  100. new Text(_history.join(", "))
  101. ],
  102. ),
  103. );
  104. }
  105. }