我正在尝试在Flutter应用中实现webView。它与CircularProgressIndicator配合良好,但我无法隐藏加载栏。这是我的webview dart文件:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class MyWebView extends StatelessWidget {
final String title;
final String selectedUrl;
final Completer<WebViewController> _controller =
Completer<WebViewController>();
MyWebView({
@required this.title,
@required this.selectedUrl,
});
bool isLoading = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Stack(
children: <Widget>[
new WebView(
initialUrl: selectedUrl,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
onPageFinished: (finish) {
setState(() {
isLoading = false;
});
},
),
isLoading ? Center( child: CircularProgressIndicator()) : Container(),
],
),
);
}
它仅显示错误:未为类“ MyWebView”定义方法“ setState”。有人可以建议我如何解决这个问题吗?
答案 0 :(得分:0)
@Enayet,您正在setState
中使用StatelessWidget
。您只能在StatefulWidget
中进行操作。如果要使用setState
,则需要将窗口小部件类更改为以下类别,
class MyWebView extends StatefulWidget {
final String title;
final String selectedUrl;
const MyWebView({Key key, @required this.title, @required this.selectedUrl}) : super(key: key);
@override
MyWebViewState createState() => MyWebViewState();
}
class MyWebViewState extends State<MyWebView> {
....
在此处了解有关StatefulWidget
的更多信息-https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html