Skip to content

Commit bda9306

Browse files
authored
Merge pull request #431 from muffinjello/master
Update README.md with injection example
2 parents ec6516d + 77e8a30 commit bda9306

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Plugin that allows Flutter to communicate with a native WebView.
1010

1111
**_Warning:_**
1212
The webview is not integrated in the widget tree, it is a native view on top of the flutter view.
13-
you won't be able to use snackbars, dialogs ...
13+
You won't be able see snackbars, dialogs, or other flutter widgets that would overlap with the region of the screen taken up by the webview.
1414

1515
## Getting Started
1616

@@ -142,6 +142,25 @@ flutterWebviewPlugin.launch(url,
142142
);
143143
```
144144

145+
#### Injecting custom code into the webview
146+
Use `flutterWebviewPlugin.evalJavaScript(String code)`. This function must be run after the page has finished loading (i.e. listen to `onStateChanged` for events where state is `finishLoad`).
147+
148+
If you have a large amount of JavaScript to embed, use an asset file. Add the asset file to `pubspec.yaml`, then call the function like:
149+
150+
```dart
151+
Future<String> loadJS(String name) async {
152+
var givenJS = rootBundle.loadString('assets/$name.js');
153+
return givenJS.then((String js) {
154+
flutterWebViewPlugin.onStateChanged.listen((viewState) async {
155+
if (viewState.type == WebViewState.finishLoad) {
156+
flutterWebViewPlugin.evalJavascript(js);
157+
}
158+
});
159+
});
160+
}
161+
```
162+
163+
145164
### Webview Events
146165

147166
- `Stream<Null>` onDestroy

0 commit comments

Comments
 (0)