TF-1886 Support RTL mode for HTML display

(cherry picked from commit 9ac393489e746b49073b1a2d98063481f4c894c6)
This commit is contained in:
dab246
2023-06-12 23:41:45 +07:00
committed by Dat H. Pham
parent 18a17a9ebc
commit 2fb2ab18e8
6 changed files with 50 additions and 29 deletions
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
const nameClassToolTip = 'tmail-tooltip';
const tooltipLinkCss = '''
@@ -27,6 +29,7 @@ String generateHtml(String content, {
String? styleCSS,
String? javaScripts,
bool hideScrollBar = true,
TextDirection? direction
}) {
return '''
<!DOCTYPE html>
@@ -53,7 +56,7 @@ String generateHtml(String content, {
</style>
${javaScripts ?? ''}
</head>
<body style = "overflow-x: hidden">
<body ${direction == TextDirection.rtl ? 'dir="rtl"' : ''} style = "overflow-x: hidden">
<div class="tmail-content">$content</div>
</body>
</html>
@@ -16,6 +16,7 @@ class HtmlContentViewerOnWeb extends StatefulWidget {
final double widthContent;
final double heightContent;
final HtmlViewerControllerForWeb controller;
final TextDirection? direction;
/// Handler for mailto: links
final Function(Uri?)? mailtoDelegate;
@@ -31,6 +32,7 @@ class HtmlContentViewerOnWeb extends StatefulWidget {
required this.controller,
this.allowResizeToDocumentSize = true,
this.mailtoDelegate,
this.direction,
}) : super(key: key);
@override
@@ -65,7 +67,9 @@ class _HtmlContentViewerOnWebState extends State<HtmlContentViewerOnWeb> {
@override
void didUpdateWidget(covariant HtmlContentViewerOnWeb oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.contentHtml != oldWidget.contentHtml) {
log('_HtmlContentViewerOnWebState::didUpdateWidget():Old-Direction: ${oldWidget.direction} | Current-Direction: ${widget.direction}');
if (widget.contentHtml != oldWidget.contentHtml ||
widget.direction != oldWidget.direction) {
createdViewId = _getRandString(10);
widget.controller.viewId = createdViewId;
_setUpWeb();
@@ -167,7 +171,8 @@ class _HtmlContentViewerOnWebState extends State<HtmlContentViewerOnWeb> {
minHeight: minHeight,
minWidth: minWidth,
styleCSS: tooltipLinkCss,
javaScripts: webViewActionScripts + scriptsDisableZoom);
javaScripts: webViewActionScripts + scriptsDisableZoom,
direction: widget.direction);
return htmlTemplate;
}
@@ -276,21 +281,18 @@ class _HtmlContentViewerOnWebState extends State<HtmlContentViewerOnWeb> {
return Container();
}
return Directionality(
textDirection: TextDirection.ltr,
child: FutureBuilder<bool>(
future: webInit,
builder: (context, snapshot) {
if (snapshot.hasData) {
return HtmlElementView(
key: ValueKey(htmlData),
viewType: createdViewId,
);
} else {
return Container();
}
return FutureBuilder<bool>(
future: webInit,
builder: (context, snapshot) {
if (snapshot.hasData) {
return HtmlElementView(
key: ValueKey(htmlData),
viewType: createdViewId,
);
} else {
return Container();
}
)
}
);
}
}
@@ -22,6 +22,7 @@ class HtmlContentViewer extends StatefulWidget {
final double heightContent;
final OnScrollHorizontalEnd? onScrollHorizontalEnd;
final OnWebViewLoaded? onWebViewLoaded;
final TextDirection? direction;
/// Register this callback if you want a reference to the [InAppWebViewController].
final void Function(InAppWebViewController controller)? onCreated;
@@ -43,6 +44,7 @@ class HtmlContentViewer extends StatefulWidget {
this.onScrollHorizontalEnd,
this.urlLauncherDelegate,
this.mailtoDelegate,
this.direction,
}) : super(key: key);
@override
@@ -63,14 +65,19 @@ class _HtmlContentViewState extends State<HtmlContentViewer> {
void initState() {
super.initState();
actualHeight = widget.heightContent;
_htmlData = generateHtml(widget.contentHtml);
_htmlData = generateHtml(widget.contentHtml, direction: widget.direction);
}
@override
void didUpdateWidget(covariant HtmlContentViewer oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.contentHtml != oldWidget.contentHtml) {
_htmlData = generateHtml(widget.contentHtml);
log('_HtmlContentViewState::didUpdateWidget():Old-Direction: ${oldWidget.direction} | Current-Direction: ${widget.direction}');
if (widget.contentHtml != oldWidget.contentHtml ||
widget.direction != oldWidget.direction) {
_htmlData = generateHtml(
widget.contentHtml,
direction: widget.direction
);
}
}