From 89a7b12da486ef493826efee55a0a0c653dd8e4e Mon Sep 17 00:00:00 2001 From: dab246 Date: Mon, 9 Oct 2023 10:24:22 +0700 Subject: [PATCH] Fix email view height greater then content height (cherry picked from commit 595e37d675fbd45d5a3fdec09226237a9e5cdee1) --- .../html_content_viewer_widget.dart | 60 +++++++++++-------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/core/lib/presentation/views/html_viewer/html_content_viewer_widget.dart b/core/lib/presentation/views/html_viewer/html_content_viewer_widget.dart index 3a3b1dd75..cb8fa2caa 100644 --- a/core/lib/presentation/views/html_viewer/html_content_viewer_widget.dart +++ b/core/lib/presentation/views/html_viewer/html_content_viewer_widget.dart @@ -20,7 +20,7 @@ typedef OnWebViewLoaded = Function(bool isScrollPageViewActivated); class HtmlContentViewer extends StatefulWidget { final String contentHtml; - final double heightContent; + final double? heightContent; final OnScrollHorizontalEnd? onScrollHorizontalEnd; final OnWebViewLoaded? onWebViewLoaded; final TextDirection? direction; @@ -39,7 +39,7 @@ class HtmlContentViewer extends StatefulWidget { const HtmlContentViewer({ Key? key, required this.contentHtml, - required this.heightContent, + this.heightContent, this.onCreated, this.onWebViewLoaded, this.onScrollHorizontalEnd, @@ -54,17 +54,19 @@ class HtmlContentViewer extends StatefulWidget { class _HtmlContentViewState extends State { - late double actualHeight; - double minWidth = 300; + static const double _minHeight = 100.0; + static const double _offsetHeight = 30.0; + + late double _actualHeight; String? _htmlData; late InAppWebViewController _webViewController; bool _isLoading = true; - bool horizontalGestureActivated = false; + bool _horizontalGestureActivated = false; @override void initState() { super.initState(); - actualHeight = widget.heightContent; + _actualHeight = widget.heightContent ?? _minHeight; _htmlData = generateHtml( widget.contentHtml, direction: widget.direction, @@ -89,11 +91,11 @@ class _HtmlContentViewState extends State { return LayoutBuilder(builder: (context, constraints) { return Stack( children: [ - if (_htmlData?.isNotEmpty == false) + if (_htmlData == null || _htmlData?.isEmpty == true) const SizedBox.shrink() else SizedBox( - height: actualHeight, + height: _actualHeight, width: constraints.maxWidth, child: InAppWebView( key: ValueKey(_htmlData), @@ -110,7 +112,7 @@ class _HtmlContentViewState extends State { shouldOverrideUrlLoading: _shouldOverrideUrlLoading, gestureRecognizers: { Factory(() => LongPressGestureRecognizer()), - if (Platform.isIOS && horizontalGestureActivated) + if (Platform.isIOS && _horizontalGestureActivated) Factory(() => HorizontalDragGestureRecognizer()), if (Platform.isAndroid) Factory(() => ScaleGestureRecognizer()), @@ -158,18 +160,17 @@ class _HtmlContentViewState extends State { ) async { log('_HtmlContentViewState::_onContentSizeChanged:oldContentSize: $oldContentSize | newContentSize: $newContentSize'); final maxContentHeight = max(oldContentSize.height, newContentSize.height); - log('_HtmlContentViewState::_onContentSizeChanged:maxContentHeight: $maxContentHeight'); - if (maxContentHeight > actualHeight) { + if (!_isLoading && maxContentHeight > _actualHeight) { + log('_HtmlContentViewState::_onContentSizeChanged:HEIGHT_UPDATED: $maxContentHeight'); setState(() { - actualHeight = maxContentHeight; + _actualHeight = maxContentHeight; }); } } void _onHandleScrollEvent(List parameters) { - log('_HtmlContentViewState::_onHandleScrollRightEvent():parameters: $parameters'); + log('_HtmlContentViewState::_onHandleScrollEvent():parameters: $parameters'); final message = parameters.first; - log('_HtmlContentViewState::_onHandleScrollRightEvent():message: $message'); if (message == HtmlEventAction.scrollLeftEndAction) { widget.onScrollHorizontalEnd?.call(true); } else if (message == HtmlEventAction.scrollRightEndAction) { @@ -179,14 +180,16 @@ class _HtmlContentViewState extends State { Future _setActualHeightView() async { final scrollHeight = await _webViewController.evaluateJavascript(source: 'document.body.scrollHeight'); - if (scrollHeight != null && mounted) { - final scrollHeightWithBuffer = scrollHeight + 30.0; - if (scrollHeightWithBuffer > actualHeight) { - setState(() { - actualHeight = scrollHeightWithBuffer; - _isLoading = false; - }); - } + log('_HtmlContentViewState::_setActualHeightView():scrollHeight: $scrollHeight | type: ${scrollHeight.runtimeType}'); + if (mounted && + scrollHeight != null && + scrollHeight is double && + scrollHeight > 0 + ) { + setState(() { + _actualHeight = scrollHeight + _offsetHeight; + _isLoading = false; + }); } } @@ -195,19 +198,24 @@ class _HtmlContentViewState extends State { _webViewController.evaluateJavascript(source: 'document.getElementsByClassName("tmail-content")[0].scrollWidth'), _webViewController.evaluateJavascript(source: 'document.getElementsByClassName("tmail-content")[0].offsetWidth') ]); - + log('_HtmlContentViewState::_setActualWidthView():result: $result'); if (result.length == 2) { final scrollWidth = result[0]; final offsetWidth = result[1]; - if (scrollWidth != null && offsetWidth != null && mounted) { + if (mounted && + scrollWidth != null && + offsetWidth != null && + scrollWidth is double && + offsetWidth is double + ) { final isScrollActivated = scrollWidth.round() == offsetWidth.round(); if (isScrollActivated) { setState(() { - horizontalGestureActivated = false; + _horizontalGestureActivated = false; }); } else { setState(() { - horizontalGestureActivated = true; + _horizontalGestureActivated = true; }); await _webViewController.evaluateJavascript(source: HtmlUtils.runScriptsHandleScrollEvent);