main.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
  5. void main() {
  6. runApp(new MyApp());
  7. }
  8. class MyApp extends StatelessWidget {
  9. // This widget is the root of your application.
  10. @override
  11. Widget build(BuildContext context) {
  12. return new MaterialApp(
  13. title: 'Flutter Demo',
  14. theme: new ThemeData(
  15. // This is the theme of your application.
  16. //
  17. // Try running your application with "flutter run". You'll see
  18. // the application has a blue toolbar. Then, without quitting
  19. // the app, try changing the primarySwatch below to Colors.green
  20. // and then invoke "hot reload" (press "r" in the console where
  21. // you ran "flutter run", or press Run > Hot Reload App in IntelliJ).
  22. // Notice that the counter didn't reset back to zero -- the application
  23. // is not restarted.
  24. primarySwatch: Colors.blue,
  25. ),
  26. home: new MyHomePage(title: 'Flutter Demo Home Page'),
  27. );
  28. }
  29. }
  30. class MyHomePage extends StatefulWidget {
  31. MyHomePage({Key key, this.title}) : super(key: key);
  32. // This widget is the home page of your application. It is stateful,
  33. // meaning that it has a State object (defined below) that contains
  34. // fields that affect how it looks.
  35. // This class is the configuration for the state. It holds the
  36. // values (in this case the title) provided by the parent (in this
  37. // case the App widget) and used by the build method of the State.
  38. // Fields in a Widget subclass are always marked "final".
  39. final String title;
  40. @override
  41. _MyHomePageState createState() => new _MyHomePageState();
  42. }
  43. class _MyHomePageState extends State<MyHomePage> {
  44. TextEditingController _ctrl = new TextEditingController(text: "https://flutter.io");
  45. StreamSubscription _onDestroy;
  46. GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
  47. @override
  48. initState() {
  49. super.initState();
  50. FlutterWebviewPlugin.init();
  51. _onDestroy = FlutterWebviewPlugin.onDestroy.listen((_) {
  52. if (mounted) {
  53. _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
  54. }
  55. });
  56. }
  57. @override
  58. void dispose() {
  59. super.dispose();
  60. _onDestroy?.cancel();
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. return new Scaffold(
  65. key: _scaffoldKey,
  66. appBar: new AppBar(
  67. title: new Text('Plugin example app'),
  68. ),
  69. body: new Column(children: [
  70. new Container(padding: const EdgeInsets.all(24.0), child: new TextField(controller: _ctrl)),
  71. new RaisedButton(onPressed: _onPressed, child: new Text("Open Webview"))
  72. ], mainAxisAlignment: MainAxisAlignment.center),
  73. );
  74. }
  75. void _onPressed() {
  76. try {
  77. FlutterWebviewPlugin.launch(_ctrl.text);
  78. new Timer(const Duration(seconds: 3), () {
  79. FlutterWebviewPlugin.close();
  80. });
  81. } catch (e) {
  82. print(e);
  83. }
  84. }
  85. }