当我使用 webview_flutter 包加载 iframe 并单击输入字段时,屏幕会刷新并且键盘永远不会出现。 Iframe 本身按预期正常运行,但单击调用键盘的字段时除外。
要复制的示例代码:
import 'package:webview_flutter/webview_flutter.dart';
class PaymentWebView extends StatefulWidget {
static const path = "payments-web-view";
final String url;
const PaymentWebView({super.key, required this.url});
@override
State<PaymentWebView> createState() => _PaymentWebViewState();
}
class _PaymentWebViewState extends State<PaymentWebView> {
final controller = WebViewController()
..setBackgroundColor(const Color(0x00000000));
// ..setNavigationDelegate(
// NavigationDelegate(
// onProgress: (int progress) {
// // Update loading bar.
// },
// onPageStarted: (String url) {},
// onPageFinished: (String url) {},
// onWebResourceError: (WebResourceError error) {},
// onNavigationRequest: (NavigationRequest request) {
// return NavigationDecision.navigate;
// },
// ),
// );
@override
void initState() {
Future.microtask(() {
final size = MediaQuery.of(context).size;
final height = (size.height * 0.7).toInt().toString();
final width = (size.width * 0.9).toInt().toString();
controller.loadRequest(Uri.dataFromString('''<html>
<head><meta name="viewport" content="width=device-width, initial-scale=1"></head><iframe src="${widget.url}" height=${height}px width=${width}px ></iframe></html>''',
mimeType: 'text/html'));
});
super.initState();
}
@override
Widget build(BuildContext context) {
print("Refreshed webview");
return Scaffold(
body: WebViewWidget(controller: controller),
);
}
}
'''
我通过将控制器初始化移至 initState 解决了这个问题:
class _YourScreen extends State<YourScreen> {
final String _url;
final WebViewController _controller = WebViewController();
_YourScreen(this._url);
@override
void initState() {
super.initState();
_controller
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(Colors.grey[100]!)
/////// other code ........
}