WebView 点击 TextFormField 后重新加载

问题描述 投票:0回答:1

我目前正在开发一个在 SDK 2.10.4 上运行的 flutter 应用程序。我已经实现了一个 webView,其中包含网页中的一些表单字段。当我单击 TextFormField 时,我的 WebView 会重新加载。并且这个过程是可重复的。 并说

WebView.destroy() called while WebView is still attached to window.
尝试了很多解决方案,例如更改 WebView 包、升级 SDK,但没有解决我的问题。 当我将项目的 SDK 版本更新到 2.18.0 时,我在整个项目中创建的每个文件中都出现了 Null Safety 错误。
那么有什么解决办法吗?我不需要升级我的 sdk 并且我的 webview 可以正常工作?

这是我的 Flutter Doctor 控制台。

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.3.9, on Microsoft Windows [Version 10.0.22621.1702], locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
[√] Chrome - develop for the web
[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2021.3)
[√] Connected device (4 available)
[√] HTTP Host Availability

! Doctor found issues in 2 categories.
flutter dart android-webview flutter-dependencies flutterwebviewplugin
1个回答
0
投票

是的,我已经找到解决方案,请使用此代码

import 'package:dragon_ltc/src/constant/pallete.dart';
import 'package:flutter/cupertino.dart';
import 'package:webview_flutter/webview_flutter.dart';

  class kWebView extends StatefulWidget {
    const kWebView({super.key});

      @override
     State<kWebView> createState() => _kWebViewState();
   }

  class _kWebViewState extends State<kWebView> {
     Widget view = const Center(
       child: CupertinoActivityIndicator(
       color: ConstColor.black,
        radius: 15,
     ),
    );

    @override
    void initState() {
       view = WebViewWidget(
          controller: WebViewController()
          ..setJavaScriptMode(JavaScriptMode.unrestricted)
          ..setBackgroundColor(
         const Color(0x00000000),
          )
        ..setNavigationDelegate(
         NavigationDelegate(
         onPageFinished: (String url) async {},
         onWebResourceError: (error) {
          print('Web resource error: $error');
        },
      ),
    )
    ..loadRequest(
      Uri.parse(
        'https://www.google.com/',
      ),
    ),
);
super.initState();
  }

   @override
  Widget build(BuildContext context) {
  return view;
   }
 }

将整个 webview 小部件存储在 initState 中的一个变量中,因为当我们从网页打开 textformfield 时,它将重新生成整个构建方法

© www.soinside.com 2019 - 2024. All rights reserved.