Skip to content

Commit 3919218

Browse files
committed
Add support for html string loading
1 parent 640c5b8 commit 3919218

6 files changed

Lines changed: 55 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.2.0
2+
* Added support for HTML string loading
3+
14
## 1.1.2
25
* Removed redundant isolate uses
36
* Removed flutter version constraint in pubspec.yaml

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,22 @@ It's better to warm up the controller before displaying the webview. It's possib
4646
Supported params of [AdBlockerWebview](https://pub.dev/documentation/adblocker_webview/latest/adblocker_webview/AdBlockerWebview-class.html]) are:
4747
```dart
4848
const AdBlockerWebview({
49-
super.key,
50-
required this.url,
51-
required this.adBlockerWebviewController,
52-
required this.shouldBlockAds,
53-
this.onLoadStart,
54-
this.onLoadFinished,
55-
this.onProgress,
56-
this.onLoadError,
57-
this.onTitleChanged,
58-
this.options,
59-
this.additionalHostsToBlock = const [],
60-
});
49+
required this.adBlockerWebviewController,
50+
required this.shouldBlockAds,
51+
this.url,
52+
this.initialHtmlData,
53+
this.onLoadStart,
54+
this.onLoadFinished,
55+
this.onProgress,
56+
this.onLoadError,
57+
this.onTitleChanged,
58+
this.options,
59+
this.additionalHostsToBlock = const [],
60+
super.key,
61+
}) : assert(
62+
(url == null && initialHtmlData != null) ||
63+
(url != null && initialHtmlData == null),
64+
'Both url and initialHtmlData can not be non null');
6165
```
6266
#### Caching
6367
- API response for Ad hosts is cached automatically and no config is required!

lib/src/adblocker_webview.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,27 @@ import 'package:flutter_inappwebview/flutter_inappwebview.dart';
88
/// appear inside of the webpages.
99
class AdBlockerWebview extends StatefulWidget {
1010
const AdBlockerWebview({
11-
required this.url,
1211
required this.adBlockerWebviewController,
1312
required this.shouldBlockAds,
14-
super.key,
13+
this.url,
14+
this.initialHtmlData,
1515
this.onLoadStart,
1616
this.onLoadFinished,
1717
this.onProgress,
1818
this.onLoadError,
1919
this.onTitleChanged,
2020
this.options,
2121
this.additionalHostsToBlock = const [],
22-
});
22+
super.key,
23+
}) : assert(
24+
(url == null && initialHtmlData != null) ||
25+
(url != null && initialHtmlData == null),
26+
'Both url and initialHtmlData can not be non null');
2327

2428
/// Required: The initial [Uri] url that will be displayed in webview.
25-
final Uri url;
29+
final Uri? url;
30+
31+
final String? initialHtmlData;
2632

2733
/// Required: The controller for [AdBlockerWebview].
2834
/// See more at [AdBlockerWebviewController].
@@ -110,6 +116,9 @@ class _AdBlockerWebviewState extends State<AdBlockerWebview> {
110116
onLoadStop: widget.onLoadFinished,
111117
onLoadError: widget.onLoadError,
112118
onTitleChanged: widget.onTitleChanged,
119+
initialData: widget.initialHtmlData == null
120+
? null
121+
: InAppWebViewInitialData(data: widget.initialHtmlData!),
113122
);
114123
}
115124
}

lib/src/adblocker_webview_controller.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ abstract class AdBlockerWebviewController implements InternalWebviewController {
5555
// Loads the given url
5656
Future<void> loadUrl(String url);
5757

58+
Future<void> loadData(
59+
String data, {
60+
String mimeType = 'text/html',
61+
String encoding = 'utf8',
62+
});
63+
5864
/// Navigates webview to previous page
5965
Future<void> goBack();
6066

lib/src/adblocker_webview_controller_impl.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ class AdBlockerWebviewControllerImpl implements AdBlockerWebviewController {
9797
.loadUrl(urlRequest: URLRequest(url: Uri.parse(url)));
9898
}
9999

100+
@override
101+
Future<void> loadData(
102+
String data, {
103+
String mimeType = 'text/html',
104+
String encoding = 'utf8',
105+
}) async {
106+
if (_inAppWebViewController == null) {
107+
return;
108+
}
109+
return _inAppWebViewController!.loadData(
110+
data: data,
111+
mimeType: mimeType,
112+
encoding: encoding,
113+
);
114+
}
115+
100116
@override
101117
Future<void> reload() async {
102118
if (_inAppWebViewController == null) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: adblocker_webview
22
description: A webview for flutter with adblocking capability. It's currently based on Flutter InAppWebview Plugin.
33
repository: https://github.com/islamdidarmd/flutter_adblocker_webview
44
issue_tracker: https://github.com/islamdidarmd/flutter_adblocker_webview/issues
5-
version: 1.1.2
5+
version: 1.2.0
66
homepage: https://github.com/islamdidarmd/flutter_adblocker_webview
77

88
environment:

0 commit comments

Comments
 (0)