webview_scaffold.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'base.dart';
  5. class WebviewScaffold extends StatefulWidget {
  6. final PreferredSizeWidget appBar;
  7. final String url;
  8. final bool withJavascript;
  9. final bool clearCache;
  10. final bool clearCookies;
  11. final bool enableAppScheme;
  12. final String userAgent;
  13. final bool primary;
  14. final List<Widget> persistentFooterButtons;
  15. final Widget bottomNavigationBar;
  16. final bool withZoom;
  17. final bool withLocalStorage;
  18. WebviewScaffold(
  19. {Key key,
  20. this.appBar,
  21. @required this.url,
  22. this.withJavascript,
  23. this.clearCache,
  24. this.clearCookies,
  25. this.enableAppScheme,
  26. this.userAgent,
  27. this.primary: true,
  28. this.persistentFooterButtons,
  29. this.bottomNavigationBar,
  30. this.withZoom,
  31. this.withLocalStorage})
  32. : super(key: key);
  33. @override
  34. _WebviewScaffoldState createState() => new _WebviewScaffoldState();
  35. }
  36. class _WebviewScaffoldState extends State<WebviewScaffold> {
  37. final webviewReference = new FlutterWebviewPlugin();
  38. Rect _rect;
  39. Timer _resizeTimer;
  40. void initState() {
  41. super.initState();
  42. webviewReference.close();
  43. }
  44. void dispose() {
  45. super.dispose();
  46. webviewReference.close();
  47. webviewReference.dispose();
  48. }
  49. @override
  50. Widget build(BuildContext context) {
  51. if (_rect == null) {
  52. _rect = _buildRect(context);
  53. webviewReference.launch(widget.url,
  54. withJavascript: widget.withJavascript,
  55. clearCache: widget.clearCache,
  56. clearCookies: widget.clearCookies,
  57. enableAppScheme: widget.enableAppScheme,
  58. userAgent: widget.userAgent,
  59. rect: _rect,
  60. withZoom: widget.withZoom,
  61. withLocalStorage: widget.withLocalStorage);
  62. } else {
  63. Rect rect = _buildRect(context);
  64. if (_rect != rect) {
  65. _rect = rect;
  66. _resizeTimer?.cancel();
  67. _resizeTimer = new Timer(new Duration(milliseconds: 300), () {
  68. // avoid resizing to fast when build is called multiple time
  69. webviewReference.resize(_rect);
  70. });
  71. }
  72. }
  73. return new Scaffold(
  74. appBar: widget.appBar,
  75. persistentFooterButtons: widget.persistentFooterButtons,
  76. bottomNavigationBar: widget.bottomNavigationBar,
  77. body: new Center(child: new CircularProgressIndicator()));
  78. }
  79. Rect _buildRect(BuildContext context) {
  80. bool fullscreen = widget.appBar == null;
  81. final mediaQuery = MediaQuery.of(context);
  82. final topPadding = widget.primary ? mediaQuery.padding.top : 0.0;
  83. num top =
  84. fullscreen ? 0.0 : widget.appBar.preferredSize.height + topPadding;
  85. num height = mediaQuery.size.height - top;
  86. if (widget.bottomNavigationBar != null) {
  87. height -=
  88. 56.0 + mediaQuery.padding.bottom; // todo(lejard_h) find a way to determine bottomNavigationBar programmatically
  89. }
  90. if (widget.persistentFooterButtons != null) {
  91. height -=
  92. 53.0; // todo(lejard_h) find a way to determine persistentFooterButtons programmatically
  93. if (widget.bottomNavigationBar == null){
  94. height -= mediaQuery.padding.bottom;
  95. }
  96. }
  97. return new Rect.fromLTWH(0.0, top, mediaQuery.size.width, height);
  98. }
  99. }