isLoading without setState?Flutter(循环器压力指示器)



我试过

setState(() {isLoading = true})
//do something
setState(() {isLoading = false})

在onTab(({}中

但是使用setState,像pdfFile和pdfController这样的数据会被覆盖,并且应用程序不会推送到新的路由。

好吧,我的问题是:如何向用户显示加载动画,如果加载完成,将下载的数据推到屏幕上?也许我需要一些没有setState的东西?我们有什么可能性?

body: StreamBuilder(
stream: firebase.base
.collection('xxx')
.doc(firebase.auth.currentUser!.uid)
.collection("xxx")
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
final liste = snapshot.data!.docs;
return Column(
children: [
Container(
height: 400,
child: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: liste.length,
itemBuilder: (BuildContext context, int index) {
Map data = snapshot.data!.docs[index].data() as Map;
String fileName = data['fileName'];
String url = data['url'];
return GestureDetector(
onTap: () async {
http.Response pdfFile =
await http.get(Uri.parse(url));
final pdfController = PdfController(
document:
PdfDocument.openData(pdfFile.bodyBytes));
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
PdfViewScreen(pdfController, fileName)));
},

也许以下逻辑适用于您:

onTap仅将用户重定向到PdfViewScreen。在PdfViewScreen中,您使用FutureBuilder来解析http.get(Uri.parse(url)),之后FutureBuildr将返回您使用的PDFviewer。在FutureBuilder中,您可以显示进度指示符

您可以在花括号后使用else表达式。或者在if之后仅使用return Center(child: CircularProgressIndicator());



body: StreamBuilder(
stream: firebase.base
.collection('xxx')
.doc(firebase.auth.currentUser!.uid)
.collection("xxx")
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
final liste = snapshot.data!.docs;
return Column(
children: [
Container(
height: 400,
child: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: liste.length,
itemBuilder: (BuildContext context, int index) {
Map data = snapshot.data!.docs[index].data() as Map;
String fileName = data['fileName'];
String url = data['url'];
return GestureDetector(
onTap: () async {
Future<http.Response> pdfFile =
http.get(Uri.parse(url));
showDialog(
context: context,
builder: (context) {
return FutureBuilder<http.Response>(
future: http.get(Uri.parse(url)),
builder: (context, snapshot) {
if (snapshot.hasData) {
final pdfController = PdfController(
document: PdfDocument.openData(
snapshot.data!.bodyBytes));
return PdfViewScreen(
pdfController, fileName);
}
return const Center(
child: CircularProgressIndicator());
});
});
},

最新更新