Skip to content

Commit fecb370

Browse files
author
Adam
committed
Update README.md with injection example
Plus, clarified widget warning
1 parent 9c05312 commit fecb370

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

@@ -125,6 +125,25 @@ flutterWebviewPlugin.launch(url,
125125
);
126126
```
127127

128+
#### Injecting custom code into the webview
129+
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`).
130+
131+
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:
132+
133+
```dart
134+
Future<String> loadJS(String name) async {
135+
var givenJS = rootBundle.loadString('assets/$name.js');
136+
return givenJS.then((String js) {
137+
flutterWebViewPlugin.onStateChanged.listen((viewState) async {
138+
if (viewState.type == WebViewState.finishLoad) {
139+
flutterWebViewPlugin.evalJavascript(js);
140+
}
141+
});
142+
});
143+
}
144+
```
145+
146+
128147
### Webview Events
129148

130149
- `Stream<Null>` onDestroy

0 commit comments

Comments
 (0)