Fix email view height greater then content height

(cherry picked from commit 595e37d675fbd45d5a3fdec09226237a9e5cdee1)
This commit is contained in:
dab246
2023-10-09 10:24:22 +07:00
committed by Dat H. Pham
parent 794776425c
commit 89a7b12da4
@@ -20,7 +20,7 @@ typedef OnWebViewLoaded = Function(bool isScrollPageViewActivated);
class HtmlContentViewer extends StatefulWidget { class HtmlContentViewer extends StatefulWidget {
final String contentHtml; final String contentHtml;
final double heightContent; final double? heightContent;
final OnScrollHorizontalEnd? onScrollHorizontalEnd; final OnScrollHorizontalEnd? onScrollHorizontalEnd;
final OnWebViewLoaded? onWebViewLoaded; final OnWebViewLoaded? onWebViewLoaded;
final TextDirection? direction; final TextDirection? direction;
@@ -39,7 +39,7 @@ class HtmlContentViewer extends StatefulWidget {
const HtmlContentViewer({ const HtmlContentViewer({
Key? key, Key? key,
required this.contentHtml, required this.contentHtml,
required this.heightContent, this.heightContent,
this.onCreated, this.onCreated,
this.onWebViewLoaded, this.onWebViewLoaded,
this.onScrollHorizontalEnd, this.onScrollHorizontalEnd,
@@ -54,17 +54,19 @@ class HtmlContentViewer extends StatefulWidget {
class _HtmlContentViewState extends State<HtmlContentViewer> { class _HtmlContentViewState extends State<HtmlContentViewer> {
late double actualHeight; static const double _minHeight = 100.0;
double minWidth = 300; static const double _offsetHeight = 30.0;
late double _actualHeight;
String? _htmlData; String? _htmlData;
late InAppWebViewController _webViewController; late InAppWebViewController _webViewController;
bool _isLoading = true; bool _isLoading = true;
bool horizontalGestureActivated = false; bool _horizontalGestureActivated = false;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
actualHeight = widget.heightContent; _actualHeight = widget.heightContent ?? _minHeight;
_htmlData = generateHtml( _htmlData = generateHtml(
widget.contentHtml, widget.contentHtml,
direction: widget.direction, direction: widget.direction,
@@ -89,11 +91,11 @@ class _HtmlContentViewState extends State<HtmlContentViewer> {
return LayoutBuilder(builder: (context, constraints) { return LayoutBuilder(builder: (context, constraints) {
return Stack( return Stack(
children: [ children: [
if (_htmlData?.isNotEmpty == false) if (_htmlData == null || _htmlData?.isEmpty == true)
const SizedBox.shrink() const SizedBox.shrink()
else else
SizedBox( SizedBox(
height: actualHeight, height: _actualHeight,
width: constraints.maxWidth, width: constraints.maxWidth,
child: InAppWebView( child: InAppWebView(
key: ValueKey(_htmlData), key: ValueKey(_htmlData),
@@ -110,7 +112,7 @@ class _HtmlContentViewState extends State<HtmlContentViewer> {
shouldOverrideUrlLoading: _shouldOverrideUrlLoading, shouldOverrideUrlLoading: _shouldOverrideUrlLoading,
gestureRecognizers: { gestureRecognizers: {
Factory<LongPressGestureRecognizer>(() => LongPressGestureRecognizer()), Factory<LongPressGestureRecognizer>(() => LongPressGestureRecognizer()),
if (Platform.isIOS && horizontalGestureActivated) if (Platform.isIOS && _horizontalGestureActivated)
Factory<HorizontalDragGestureRecognizer>(() => HorizontalDragGestureRecognizer()), Factory<HorizontalDragGestureRecognizer>(() => HorizontalDragGestureRecognizer()),
if (Platform.isAndroid) if (Platform.isAndroid)
Factory<ScaleGestureRecognizer>(() => ScaleGestureRecognizer()), Factory<ScaleGestureRecognizer>(() => ScaleGestureRecognizer()),
@@ -158,18 +160,17 @@ class _HtmlContentViewState extends State<HtmlContentViewer> {
) async { ) async {
log('_HtmlContentViewState::_onContentSizeChanged:oldContentSize: $oldContentSize | newContentSize: $newContentSize'); log('_HtmlContentViewState::_onContentSizeChanged:oldContentSize: $oldContentSize | newContentSize: $newContentSize');
final maxContentHeight = max(oldContentSize.height, newContentSize.height); final maxContentHeight = max(oldContentSize.height, newContentSize.height);
log('_HtmlContentViewState::_onContentSizeChanged:maxContentHeight: $maxContentHeight'); if (!_isLoading && maxContentHeight > _actualHeight) {
if (maxContentHeight > actualHeight) { log('_HtmlContentViewState::_onContentSizeChanged:HEIGHT_UPDATED: $maxContentHeight');
setState(() { setState(() {
actualHeight = maxContentHeight; _actualHeight = maxContentHeight;
}); });
} }
} }
void _onHandleScrollEvent(List<dynamic> parameters) { void _onHandleScrollEvent(List<dynamic> parameters) {
log('_HtmlContentViewState::_onHandleScrollRightEvent():parameters: $parameters'); log('_HtmlContentViewState::_onHandleScrollEvent():parameters: $parameters');
final message = parameters.first; final message = parameters.first;
log('_HtmlContentViewState::_onHandleScrollRightEvent():message: $message');
if (message == HtmlEventAction.scrollLeftEndAction) { if (message == HtmlEventAction.scrollLeftEndAction) {
widget.onScrollHorizontalEnd?.call(true); widget.onScrollHorizontalEnd?.call(true);
} else if (message == HtmlEventAction.scrollRightEndAction) { } else if (message == HtmlEventAction.scrollRightEndAction) {
@@ -179,14 +180,16 @@ class _HtmlContentViewState extends State<HtmlContentViewer> {
Future<void> _setActualHeightView() async { Future<void> _setActualHeightView() async {
final scrollHeight = await _webViewController.evaluateJavascript(source: 'document.body.scrollHeight'); final scrollHeight = await _webViewController.evaluateJavascript(source: 'document.body.scrollHeight');
if (scrollHeight != null && mounted) { log('_HtmlContentViewState::_setActualHeightView():scrollHeight: $scrollHeight | type: ${scrollHeight.runtimeType}');
final scrollHeightWithBuffer = scrollHeight + 30.0; if (mounted &&
if (scrollHeightWithBuffer > actualHeight) { scrollHeight != null &&
setState(() { scrollHeight is double &&
actualHeight = scrollHeightWithBuffer; scrollHeight > 0
_isLoading = false; ) {
}); setState(() {
} _actualHeight = scrollHeight + _offsetHeight;
_isLoading = false;
});
} }
} }
@@ -195,19 +198,24 @@ class _HtmlContentViewState extends State<HtmlContentViewer> {
_webViewController.evaluateJavascript(source: 'document.getElementsByClassName("tmail-content")[0].scrollWidth'), _webViewController.evaluateJavascript(source: 'document.getElementsByClassName("tmail-content")[0].scrollWidth'),
_webViewController.evaluateJavascript(source: 'document.getElementsByClassName("tmail-content")[0].offsetWidth') _webViewController.evaluateJavascript(source: 'document.getElementsByClassName("tmail-content")[0].offsetWidth')
]); ]);
log('_HtmlContentViewState::_setActualWidthView():result: $result');
if (result.length == 2) { if (result.length == 2) {
final scrollWidth = result[0]; final scrollWidth = result[0];
final offsetWidth = result[1]; 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(); final isScrollActivated = scrollWidth.round() == offsetWidth.round();
if (isScrollActivated) { if (isScrollActivated) {
setState(() { setState(() {
horizontalGestureActivated = false; _horizontalGestureActivated = false;
}); });
} else { } else {
setState(() { setState(() {
horizontalGestureActivated = true; _horizontalGestureActivated = true;
}); });
await _webViewController.evaluateJavascript(source: HtmlUtils.runScriptsHandleScrollEvent); await _webViewController.evaluateJavascript(source: HtmlUtils.runScriptsHandleScrollEvent);