webview_scaffold.dart 3.4 KB

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