Flutter:webview_flutter更新同一webview小部件中的URL [英] Flutter: webview_flutter update url in same webview widget

查看:987
本文介绍了Flutter:webview_flutter更新同一webview小部件中的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试创建一个屏幕,该屏幕显示带有bottomappbar的Web视图. 因此,您加载了Webview,然后点击底部应用栏中的某个项目时,另一个网站应加载到相同的Webview中...

Hey I am trying to create a screen which shows a webview with a bottomappbar. So you load the webview and when tapping on a item in the bottomappbar a other website should loaded in the same webview...

除了最初解析的内容外,我无法弄清楚如何打开其他网站. 我尝试使用《 setState》更新网址,但它仅更新应用程序栏标题,并且Webview仍显示原始网站

I cant figure out how to open another website other than I originaly parsed. I tried to update the url by using «setState» but it only updates the appbar title and the webview still shows the original website

这是我当前的代码:

class _WebviewContainer extends State<WebviewContainer> {
var url;
final key = UniqueKey();

_WebviewContainer(this.url); 

@override
Widget build(BuildContext context) {    
return Scaffold(
  appBar: AppBar(
    title: Text(url),
    actions: <Widget>[
      IconButton(
        icon: Icon(
          Icons.home,
          size: 40.0,
          color: Colors.white,
        ),
        onPressed: () => {
          //-> Here I set the new url but the webview always shows the origin website

          setState(() {
            url = 'https://stackoverflow.com/';
          })
        },
      )
    ],
  ),
  body: Column(
    children: <Widget>[
      Expanded(
        child: WebView(
          key: key,
          javascriptMode: JavascriptMode.unrestricted,
          initialUrl: url,              
        ),
      ),          
    ],
  ),
);
}
}

我从YouTube上的一个教程中获取了代码,创建者还表示,如果url的状态发生更改,则Webview将重新加载,但不幸的是他没有显示如何操作

I took the code from a tutorial on YouTube and the creator also stated that the webview will reload if the state of the url changes, but unfortunately he did not show how to do it

有人可以帮助我吗?

预先感谢 杜比

推荐答案

所以我遇到了类似的情况,只是当我从下拉菜单中选择一个项目时,我希望在Web视图中加载一个新的URL. 我如何做到的是创建WebViewController的实例

So I had a similar situation except i wanted a new url to be loaded in my webview when i selected an item from a dropdownmenu. How I did this was create an instance of WebViewController

WebViewController controller;

然后在使用onWebViewCreated参数创建Web视图时设置此控制器

Then set this controller when the webview is created with the onWebViewCreated parameter

child: WebView(
      key: _key,
      javascriptMode: JavascriptMode.unrestricted,
      initialUrl: url,
    onWebViewCreated: (WebViewController webViewController) {
    controller = webViewController;}

现在已经设置了控制器,可以在setState()中使用其loadUrl方法来更改显示的网址

Now you have the controller set, you can use its loadUrl method in setState() to change the displayed Url

setState(() {                
            controller.loadUrl(newUrl_here);

我刚开始使用Flutter,所以当然可以是更好的方法,但这对我有用

I just started with Flutter so could be a better way of course but this worked for me

这篇关于Flutter:webview_flutter更新同一webview小部件中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆