main.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 =
  45. new TextEditingController(text: "https://flutter.io");
  46. StreamSubscription _onDestroy;
  47. GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
  48. @override
  49. initState() {
  50. super.initState();
  51. FlutterWebviewPlugin.init();
  52. _onDestroy = FlutterWebviewPlugin.onDestroy.listen((_) {
  53. if (mounted) {
  54. _scaffoldKey.currentState
  55. .showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
  56. }
  57. });
  58. }
  59. @override
  60. void dispose() {
  61. super.dispose();
  62. _onDestroy?.cancel();
  63. }
  64. @override
  65. Widget build(BuildContext context) {
  66. return new Scaffold(
  67. key: _scaffoldKey,
  68. appBar: new AppBar(
  69. title: new Text('Plugin example app'),
  70. ),
  71. body: new Column(children: [
  72. new Container(
  73. padding: const EdgeInsets.all(24.0),
  74. child: new TextField(controller: _ctrl)),
  75. new RaisedButton(onPressed: _onPressed, child: new Text("Open Webview"))
  76. ], mainAxisAlignment: MainAxisAlignment.center),
  77. );
  78. }
  79. void _onPressed() {
  80. try {
  81. FlutterWebviewPlugin.launch(_ctrl.text);
  82. new Timer(const Duration(seconds: 3), () {
  83. FlutterWebviewPlugin.close();
  84. });
  85. } catch (e) {
  86. print(e);
  87. }
  88. }
  89. }