webview_scaffold.dart 3.3 KB

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