flutter inappwebview url 未更新

flutter inappwebview url not updated

我在底部导航中有 4 个菜单,其中两个是使用 inappwebview 的 webview。

第一个网络视图是:https://google.com

第二个网络视图是:https://flutter.dev/

如果我点击第一个 webview 然后我点击第二个 webview,webview 不会重定向到新的 url,反之亦然。

但是如果在点击第一个 webview 之后我点击其他不是 webview 的菜单,然后点击第二个 webview 它是正常重定向到第二个 webview

那么如何在 webviewpage 仍处于活动状态时重定向到新的 url(不使用单击其他菜单关闭)

这是我的 inappwebview

InAppWebView(
                initialUrlRequest: URLRequest(url: Uri.parse(widget.url)),
                onWebViewCreated: (
                    InAppWebViewController controller) async {
                  _webViewController = controller;
                },
                androidOnPermissionRequest: (controller, origin, resources) async {
                  return PermissionRequestResponse(
                      resources: resources,
                      action: PermissionRequestResponseAction.GRANT);
                },
                onProgressChanged: (InAppWebViewController controller,
                    int progress) {
                  setState(() {
                    this.progress = progress / 100;
                  });
                },
              )

这是底部导航

_children = [
        HomePage(),
        ProfilePage(),
        MyWebview(url: web_inbox, statusAppbar: false, webMenu: web_menu_inbox,),
        MyWebview(url: web_feedback, statusAppbar: false, webMenu: web_menu_feedback,),
      ]

BottomNavigationBar(
          onTap: onTappedBar,
          currentIndex: _current,
          backgroundColor: Color(0xFF003c85),
          selectedFontSize: 12,
          unselectedFontSize: 12,
          selectedItemColor: Color(0xFFf4931f),
          unselectedItemColor: Color(0xFFc0bfbf),
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
                icon: Image.asset('asset/images/abba-home-pasif.png', height: 25, width: 25, fit: BoxFit.fill,),
                activeIcon: Image.asset('asset/images/abba-home-aktif.png', height: 25, width: 25, fit: BoxFit.fill,),
                label: "Home"
            ),
            BottomNavigationBarItem(
                icon: Image.asset('asset/images/profile-pasif.png', height: 25, width: 25, fit: BoxFit.fill,),
                activeIcon: Image.asset('asset/images/profile-aktif.png', height: 25, width: 25, fit: BoxFit.fill,),
                label: "Profile"
            ),
            BottomNavigationBarItem(
                icon: Image.asset('asset/images/inbox-pasif.png', height: 25, width: 32, fit: BoxFit.fill,),
                activeIcon: Image.asset('asset/images/inbox-aktif.png', height: 25, width: 32, fit: BoxFit.fill,),
                label: "Inbox"
            ),
            BottomNavigationBarItem(
                icon: Image.asset('asset/images/comment-pasif.png', height: 27, width: 30, fit: BoxFit.fill,),
                activeIcon: Image.asset('asset/images/comment-aktif.png', height: 27, width: 30, fit: BoxFit.fill,),
                label: "Feedback"
            )
          ],
        )

void onTappedBar(int index){
    setState((){
      _current = index;
    });
  }

终于找到了解决办法

添加ValueNotifier监听变量url是否改变

ValueNotifier<String> newurl = ValueNotifier<String>('');作为全局变量添加到webviewPage

newurl.addListener(changeUrl); 添加到 webviewpage 中的 initstate

webviewpage会喜欢这个

ValueNotifier<String> newurl = ValueNotifier<String>('');

class MyWebview extends StatefulWidget {
  late String url = "";
  late bool statusAppbar = true;
  late String webMenu = "";

  MyWebview({required this.url, required this.statusAppbar, required this.webMenu});

  @override
  _MyWebviewState createState() => new _MyWebviewState();
}

class _MyWebviewState extends State<MyWebview> {

  late InAppWebViewController _webViewController;
  double progress = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    newurl.addListener(changeUrl);
  }

  void changeUrl(){
    _webViewController.loadUrl(urlRequest: URLRequest(url: Uri.parse(newurl.value)));
  }
}

并在点击菜单底部导航时添加 valuelistener 的值

void onTappedBar(int index){
    setState((){
      if(index==2) // inbox
        newurl.value=web_inbox;
      else if(index==3) // feedback
        newurl.value=web_feedback;
      else
        newurl.value="";

      _current = index;
    });
  }