You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
967 B
34 lines
967 B
import 'dart:convert'; |
|
import 'dart:io' show Platform; |
|
|
|
import 'package:flutter/foundation.dart'; |
|
import 'package:webview_flutter/webview_flutter.dart'; |
|
|
|
/// Evaluates a JavaScript function on the given WebView. |
|
/// |
|
/// The JavaScript function must return a String. |
|
/// |
|
/// On Android, the `String` resulted from the evaluation |
|
/// is JSON parsed. On iOS, the `String` is returned as is. |
|
/// |
|
/// Other platforms are not supported. The returned value |
|
/// in this case will be `null`. |
|
Future<String> wInvoke({ |
|
@required WebViewController webViewController, |
|
@required String jsFunctionContent, |
|
bool isFunctionAlready = false |
|
}) async { |
|
final actualJS = isFunctionAlready ? |
|
jsFunctionContent : |
|
""" |
|
(() => { |
|
$jsFunctionContent |
|
})() |
|
"""; |
|
|
|
final res = await webViewController.evaluateJavascript(actualJS); |
|
|
|
if (Platform.isAndroid) return JsonDecoder().convert(res) as String; |
|
else if (Platform.isIOS) return res; |
|
else return null; |
|
} |