main.dart 2.7 KB

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