18
18
19
19
// Documentation web views.
20
20
21
- import 'package:flutter/foundation.dart' ;
22
21
import 'package:flutter/material.dart' ;
23
- import 'package:flutter/services.dart' show rootBundle;
24
22
25
- // import 'package:flutter_inappwebview/flutter_inappwebview.dart';
26
- import 'package:url_launcher/url_launcher.dart' show launchUrl, LaunchMode;
23
+ import 'package:webview_flutter/webview_flutter.dart' ;
27
24
28
25
import './theme.dart' show standardAppBar;
29
- import './common.dart' show dlog;
30
26
31
27
const String kDocsDir = 'docs' ;
32
28
const String kLoadingHTMLFilePath = "$kDocsDir /loading.html" ;
33
29
const String kLoadingDarkHTMLFilePath = "$kDocsDir /loading_dark.html" ;
34
30
35
- // late InAppWebViewInitialData loadingHTMLData;
36
- // late InAppWebViewInitialData loadingDarkHTMLData;
37
-
38
- // /// Preloads the "loading" HTML documents to prevent any initial
39
- // /// lag when showing loading indicator for documentation pages.
40
- // Future<void> preloadHTMLDocuments() async {
41
- // dlog("Preloading HTML loading documents");
42
- // loadingHTMLData =
43
- // InAppWebViewInitialData(data: await rootBundle.loadString(kLoadingHTMLFilePath));
44
- // loadingHTMLData.baseUrl = Uri.parse("file:///");
45
- // loadingDarkHTMLData =
46
- // InAppWebViewInitialData(data: await rootBundle.loadString(kLoadingDarkHTMLFilePath));
47
- // loadingDarkHTMLData.baseUrl = Uri.parse("file:///");
48
- // }
49
-
50
31
/// Standard web view route used for displaying HTML documentation files.
51
32
class WebViewRoute extends StatefulWidget {
52
33
final String initialURL;
@@ -58,96 +39,34 @@ class WebViewRoute extends StatefulWidget {
58
39
}
59
40
60
41
class WebViewRouteState extends State <WebViewRoute > {
61
- /// Path to local asset with same filename as remote HTML document.
62
- String _fallbackAssetForURL (String url) {
63
- final Uri uri = Uri .parse (url);
64
- return "$kDocsDir /${uri .pathSegments .last }" ;
65
- }
66
-
67
42
/// Add dark=1 query parameter to URL.
68
43
/// This param is used to style the HTML document for dark mode via JS.
69
44
String _darkURLForURL (String url) {
70
45
return "$url ?dark=1" ;
71
46
}
72
47
73
- // /// Fall back to local HTML document if error comes
74
- // /// up when fetching document from remote server.
75
- // void errHandler(InAppWebViewController controller, Uri? url, int errCode, String desc) async {
76
- // dlog("Page load error for $url: $errCode, $desc");
77
- // final String path = _fallbackAssetForURL(url.toString());
78
- // dlog("Falling back to local asset $path");
79
- // setState(() {
80
- // controller.loadFile(assetFilePath: path);
81
- // });
82
- // }
83
-
84
- // /// Handle clicks on links in HTML documentation.
85
- // /// These links should be opened in an external browser to
86
- // /// avoid screwing with the navigation stack of the app.
87
- // Future<NavigationActionPolicy> urlClickHandler(
88
- // InAppWebViewController controller, NavigationAction action) async {
89
- // final URLRequest req = action.request;
90
- // final String urlStr = req.url.toString();
91
- // final String fallbackFilename = _fallbackAssetForURL(urlStr);
92
-
93
- // if (urlStr.startsWith(widget.initialURL) == false &&
94
- // urlStr.endsWith(fallbackFilename) == false) {
95
- // // It's not a local URL, so open it in an external browser
96
- // dlog("Opening external URL: ${req.url}");
97
- // await launchUrl(req.url!, mode: LaunchMode.externalApplication);
98
- // return NavigationActionPolicy.CANCEL;
99
- // }
100
-
101
- // return NavigationActionPolicy.ALLOW;
102
- // }
103
-
104
- // /// Create web view that initially presents a "loading" document with
105
- // /// a progress indicator. Then immediately fetch the actual remote document.
106
- // /// Falls back to loading a local bundled HTML document with the same name
107
- // /// on network error. This ensures that at least *some* version of the
108
- // /// document can be viewed even when the device is offline.
109
- // InAppWebView _buildWebView(BuildContext context) {
110
- // final darkMode = (MediaQuery.platformBrightnessOf(context) == Brightness.dark);
111
- // final loadingURL = darkMode ? kLoadingDarkHTMLFilePath : kLoadingHTMLFilePath;
112
- // final finalURL = darkMode ? _darkURLForURL(widget.initialURL) : widget.initialURL;
113
- // final initialData = darkMode ? loadingDarkHTMLData : loadingHTMLData;
114
-
115
- // final webViewOpts = InAppWebViewGroupOptions(
116
- // crossPlatform: InAppWebViewOptions(
117
- // clearCache: kDebugMode,
118
- // useShouldOverrideUrlLoading: true,
119
- // transparentBackground: true,
120
- // ));
121
-
122
- // // Create and configure web view
123
- // return InAppWebView(
124
- // initialData: initialData,
125
- // initialUrlRequest: URLRequest(url: Uri.parse(finalURL)),
126
- // initialOptions: webViewOpts,
127
- // onLoadStart: (InAppWebViewController controller, Uri? uri) {
128
- // dlog("Loading URL ${uri.toString()}");
129
- // },
130
- // onLoadStop: (InAppWebViewController controller, Uri? uri) async {
131
- // final String urlStr = uri.toString();
132
- // if (urlStr.endsWith(loadingURL) || urlStr == 'about:blank' || urlStr == 'file:///') {
133
- // // Loading of initial "loading" document is complete.
134
- // // Now load the actual remote document.
135
- // setState(() {
136
- // controller.loadUrl(urlRequest: URLRequest(url: Uri.parse(finalURL)));
137
- // });
138
- // }
139
- // },
140
- // onLoadError: errHandler,
141
- // onLoadHttpError: errHandler,
142
- // shouldOverrideUrlLoading: urlClickHandler,
143
- // onConsoleMessage: (InAppWebViewController controller, ConsoleMessage msg) {
144
- // dlog("Web View Console message: ${msg.message}");
145
- // });
146
- // }
147
-
148
48
@override
149
49
Widget build (BuildContext context) {
150
- return Scaffold (appBar: standardAppBar, body: standardAppBar //buildWebView(context),
151
- );
50
+ final darkMode = (MediaQuery .platformBrightnessOf (context) == Brightness .dark);
51
+ final finalURL = darkMode ? _darkURLForURL (widget.initialURL) : widget.initialURL;
52
+
53
+ var controller = WebViewController ()
54
+ ..setJavaScriptMode (JavaScriptMode .unrestricted)
55
+ ..setBackgroundColor (const Color (0x00000000 ))
56
+ ..setNavigationDelegate (
57
+ NavigationDelegate (
58
+ onProgress: (int progress) {},
59
+ onPageStarted: (String url) {},
60
+ onPageFinished: (String url) {},
61
+ onHttpError: (HttpResponseError error) {},
62
+ onWebResourceError: (WebResourceError error) {},
63
+ onNavigationRequest: (NavigationRequest request) {
64
+ return NavigationDecision .navigate;
65
+ },
66
+ ),
67
+ )
68
+ ..loadRequest (Uri .parse (finalURL));
69
+
70
+ return Scaffold (appBar: standardAppBar, body: WebViewWidget (controller: controller));
152
71
}
153
72
}
0 commit comments