【问题标题】:WebView can not load two different urls in flutter?WebView不能在flutter中加载两个不同的url?
【发布时间】:2019-09-09 09:03:00
【问题描述】:

我在 build 方法的第一行打印 url 消息。而这里,url是对的,但是webView没有效果。 我用的是webview_flutter lib,版本是0.3.13;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() => runApp(WebPage());

class WebPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _WebPageState();
  }
}

class _WebPageState extends State<WebPage> {
  String url = "https://flutter.dev/";

  void _changeUrl() {
    setState(() {
      this.url = "https://github.com/flutter/flutter";
    });
  }

  @override
  Widget build(BuildContext context) {
    print("url:" + url);
    return MaterialApp(
        theme: ThemeData(primarySwatch: Colors.blue),
        home: Scaffold(
          appBar: AppBar(
            title: Text("WebPage"),
          ),
          body: WebView(
            initialUrl: url,
            javascriptMode: JavascriptMode.unrestricted,
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _changeUrl,
            child: Icon(Icons.replay),
          ),
        ));
  }
}

我排除了 webView 加载第二个 url,但它没有。

【问题讨论】:

    标签: flutter webview


    【解决方案1】:

    你需要像这样使用 web-view 控制器类方法

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:webview_flutter/webview_flutter.dart';
    
    void main() => runApp(WebPage());
    
    class WebPage extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return _WebPageState();
      }
    }
    
    class _WebPageState extends State<WebPage> {
      WebViewController controller;
      var url = "https://flutter.dev/";
    
      changeUrl() {
        setState(() {
        url = "https://github.com/flutter/flutter";
          controller.loadUrl(url);
        });
      }
    
      @override
      Widget build(BuildContext context) {
        print("url:" + url);
        return MaterialApp(
            theme: ThemeData(primarySwatch: Colors.blue),
            home: Scaffold(
              appBar: AppBar(
                title: Text("WebPage"),
              ),
        body: WebView(
              // key: _key,
              javascriptMode: JavascriptMode.unrestricted,
              initialUrl: url,
              onWebViewCreated: (WebViewController webViewController) {
                controller = webViewController;
              }),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              changeUrl();
            },
                child: Icon(Icons.replay),
              ),
            ));
      }
    }
    

    更多的控制器方法可以在这里找到flutter web-view class methods

    【讨论】:

    • 它有效。但我想找到更好的解决方案。我想知道为什么这段代码失败了?
    猜你喜欢
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2020-07-14
    相关资源
    最近更新 更多