我在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,但没有加载。
答案 0 :(得分:0)
您需要使用像这样的网络视图控制器类方法
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
中找到