@@ -18,10 +18,9 @@ import 'package:flutter/material.dart';
1818import 'package:flutter/services.dart' ;
1919import 'package:markdown/markdown.dart' as md;
2020import 'package:ouds_flutter_demo/main_app_bar.dart' ;
21- import 'package:path/path.dart' as path;
2221import 'package:webview_flutter/webview_flutter.dart' ;
2322
24- class AboutFileScreen extends StatelessWidget {
23+ class AboutFileScreen extends StatefulWidget {
2524 final String title;
2625 final String fileMenuItem;
2726 final bool darkModeEnabled;
@@ -33,56 +32,57 @@ class AboutFileScreen extends StatelessWidget {
3332 required this .darkModeEnabled});
3433
3534 @override
36- Widget build (BuildContext context) {
37- final colors = Theme .of (context).colorScheme;
38- const horizontalPadding = 13.0 ;
39- const verticalPadding = 13.0 ;
35+ AboutFileScreenState createState () => AboutFileScreenState ();
36+ }
4037
41- return Scaffold (
42- appBar: MainAppBar (title: title, showBackButton: true ),
43- body: SafeArea (
44- child: FutureBuilder (
45- future: _loadFileData (),
46- builder: (context, snapshot) {
47- if (snapshot.connectionState == ConnectionState .done) {
48- String markdownContent = snapshot.data as String ;
49-
50- /// Convert Markdown to HTML using the markdown package
51- String htmlContent = markdownToHtml (markdownContent);
52-
53- return WebView (
54- initialUrl: 'about:blank' ,
55- onWebViewCreated: (WebViewController webViewController) {
56- webViewController.loadUrl (Uri .dataFromString (
57- _wrapHtmlWithCss (
58- htmlContent,
59- darkModeEnabled,
60- colors,
61- horizontalPadding,
62- verticalPadding,
63- ),
64- mimeType: 'text/html' ,
65- encoding: Encoding .getByName ('utf-8' ),
66- ).toString ());
67- },
68- backgroundColor: Colors .transparent,
69- );
70- } else {
71- return const Center (child: CircularProgressIndicator ());
72- }
73- },
74- ),
75- ),
76- );
38+ class AboutFileScreenState extends State <AboutFileScreen > {
39+ late WebViewController _webViewController;
40+
41+ /// Padding
42+ final double horizontalPadding = 13.0 ;
43+ final double verticalPadding = 13.0 ;
44+
45+ @override
46+ void initState () {
47+ super .initState ();
48+
49+ /// Initialize WebViewController
50+ _webViewController = WebViewController ()
51+ ..setJavaScriptMode (JavaScriptMode .unrestricted)
52+ ..setBackgroundColor (Colors .transparent);
53+
54+ /// Initialize WebView and load content
55+ _initializeWebView ();
7756 }
7857
79- bool isHTMLFile (String filePath) {
80- String extension = path.extension (filePath);
81- return extension .toLowerCase () == '.html' ;
58+ Future <void > _initializeWebView () async {
59+ String markdownContent = await _loadFileData ();
60+ String htmlContent = markdownToHtml (markdownContent);
61+
62+ /// Check if the widget is still mounted before using the context
63+ if (! mounted) return ;
64+
65+ /// Wrap the HTML content with necessary CSS
66+ String wrappedHtml = _wrapHtmlWithCss (
67+ htmlContent,
68+ widget.darkModeEnabled,
69+ Theme .of (context).colorScheme,
70+ horizontalPadding,
71+ verticalPadding,
72+ );
73+
74+ /// Load HTML content into the WebViewController
75+ _webViewController.loadRequest (
76+ Uri .dataFromString (
77+ wrappedHtml,
78+ mimeType: 'text/html' ,
79+ encoding: Encoding .getByName ('utf-8' ),
80+ ),
81+ );
8282 }
8383
8484 Future <String > _loadFileData () async {
85- return await rootBundle.loadString (fileMenuItem);
85+ return await rootBundle.loadString (widget. fileMenuItem);
8686 }
8787
8888 String markdownToHtml (String markdownContent) {
@@ -150,7 +150,7 @@ a:link {
150150
151151 String themeStyle = darkModeEnabled ? lightStyle : darkStyle;
152152
153- String cssApple = '''<style type ="text/css">
153+ String cssApple = '''<style ="text/css">
154154 html {
155155 font: -apple-system-body;
156156 }
@@ -202,7 +202,7 @@ a:link {
202202 color: #527EDB;
203203 }
204204 </style>''' ;
205- String cssAndroid = '''<style type ="text/css">
205+ String cssAndroid = '''<style ="text/css">
206206body {
207207 padding: 0;
208208 -webkit-text-size-adjust: none;
@@ -293,40 +293,52 @@ a:link {
293293
294294 return convertToHtml (fileContent, cssApple, cssAndroidThemeStyle);
295295 }
296- }
297296
298- String convertToHtml (
299- String partialHTMLText, String cssApple, String cssAndroid) {
300- String head = """
297+ String convertToHtml (
298+ String partialHTMLText, String cssApple, String cssAndroid) {
299+ String head = """
301300 <!doctype html>
302301<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'>
303302""" ;
304303
305- if (partialHTMLText.contains ("<html" )) {
306- return partialHTMLText;
307- }
304+ if (partialHTMLText.contains ("<html" )) {
305+ return partialHTMLText;
306+ }
308307
309- var result = "<html>" ;
310- result += head;
308+ var result = "<html>" ;
309+ result += head;
311310
312- if (! kIsWeb) {
313- if (Platform .isIOS) {
314- result += cssApple;
315- } else {
316- if (Platform .isAndroid) {
317- result += cssAndroid;
311+ if (! kIsWeb) {
312+ if (Platform .isIOS) {
313+ result += cssApple;
314+ } else {
315+ if (Platform .isAndroid) {
316+ result += cssAndroid;
317+ }
318318 }
319319 }
320- }
321320
322- result += "</head>" ;
321+ result += "</head>" ;
322+
323+ if (partialHTMLText.contains ("<body" )) {
324+ result += partialHTMLText;
325+ } else {
326+ result += "<body><p dir=\" auto\" >$partialHTMLText </p></body>" ;
327+ }
323328
324- if (partialHTMLText.contains ("<body" )) {
325- result += partialHTMLText;
326- } else {
327- result += "<body><p dir=\" auto\" >$partialHTMLText </p></body>" ;
329+ result += "</html>" ;
330+ return result;
328331 }
329332
330- result += "</html>" ;
331- return result;
333+ @override
334+ Widget build (BuildContext context) {
335+ return Scaffold (
336+ appBar: MainAppBar (title: widget.title, showBackButton: true ),
337+ body: SafeArea (
338+ child: WebViewWidget (
339+ controller: _webViewController,
340+ ),
341+ ),
342+ );
343+ }
332344}
0 commit comments