main.dart 3.0 KB

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