更新对话框中的值
Update value in dialog
我想在从服务器获取数据时在 Dialog
中显示进度值。取到第一个应该显示5%,取到第二个显示10%直到100%(共20个)
showSyncDialog(
BuildContext context,
Repository repo,
BehaviorSubject<double> percentage) {
double percent = 0.0;
int count = 0;
return showDialog(
barrierDismissible: false,
context: context,
builder: (_) {
return StatefulBuilder(builder: (context, StateSetter setState) {
return Dialog(
elevation: 0.0,
child: FutureBuilder(
future: repo.getAdminList(urls),
builder: (context, snapshot) {
if (snapshot.hasData) {
return FutureBuilder<List<ABC>>(
future: repo.getList(),
builder: (context, snapshotDatas) {
if (snapshotDatas.hasData) {
for (var i in snapshotDatas.data!) {
repo.getItems(i.id!)
.then((value) {
percent = ((count++) / 20 * 100);
print(percent.toString() + "efewfeeff");
percentage.sink.add(percent);
print(percentage.stream.value.toDouble());
});
}
return Padding(
padding: EdgeInsets.all(15.0),
child: LinearPercentIndicator(
animation: true,
lineHeight: 20.0,
animationDuration: 2000,
percent: percentage.stream.value.toDouble(),
center:
Text(percentage.stream.value.toString()),
linearStrokeCap: LinearStrokeCap.roundAll,
progressColor: Colors.greenAccent,
),
);
} else {
return Text("No item");
}
});
} else {
return Text("No item");
}
}),
);
});
});
}
输出
I/flutter (16036): 0.0efewfeeff
I/flutter (16036): 0.0
I/flutter (16036): 5.0efewfeeff
I/flutter (16036): 5.0
I/flutter (16036): 10.0efewfeeff
I/flutter (16036): 10.0
I/flutter (16036): 15.0efewfeeff
I/flutter (16036): 15.0
I/flutter (16036): 20.0efewfeeff
I/flutter (16036): 20.0
...
我的问题是对话框中的百分比值始终显示为 0.0,尽管控制台中显示的值在变化。如何更新对话框中的值?
使用 StatefulBuilder 在 Dialog 内部使用 setState 并仅在其中更新 Widgets。这是一个例子。
showDialog(
context: context,
builder: (context) {
String contentText = "Content of Dialog";
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("Title of Dialog"),
content: Text(contentText),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text("Cancel"),
),
TextButton(
onPressed: () {
setState(() {
contentText = "Changed Content of Dialog";
});
},
child: Text("Change"),
),
],
);
},
);
},
);
我想在从服务器获取数据时在 Dialog
中显示进度值。取到第一个应该显示5%,取到第二个显示10%直到100%(共20个)
showSyncDialog(
BuildContext context,
Repository repo,
BehaviorSubject<double> percentage) {
double percent = 0.0;
int count = 0;
return showDialog(
barrierDismissible: false,
context: context,
builder: (_) {
return StatefulBuilder(builder: (context, StateSetter setState) {
return Dialog(
elevation: 0.0,
child: FutureBuilder(
future: repo.getAdminList(urls),
builder: (context, snapshot) {
if (snapshot.hasData) {
return FutureBuilder<List<ABC>>(
future: repo.getList(),
builder: (context, snapshotDatas) {
if (snapshotDatas.hasData) {
for (var i in snapshotDatas.data!) {
repo.getItems(i.id!)
.then((value) {
percent = ((count++) / 20 * 100);
print(percent.toString() + "efewfeeff");
percentage.sink.add(percent);
print(percentage.stream.value.toDouble());
});
}
return Padding(
padding: EdgeInsets.all(15.0),
child: LinearPercentIndicator(
animation: true,
lineHeight: 20.0,
animationDuration: 2000,
percent: percentage.stream.value.toDouble(),
center:
Text(percentage.stream.value.toString()),
linearStrokeCap: LinearStrokeCap.roundAll,
progressColor: Colors.greenAccent,
),
);
} else {
return Text("No item");
}
});
} else {
return Text("No item");
}
}),
);
});
});
}
输出
I/flutter (16036): 0.0efewfeeff
I/flutter (16036): 0.0
I/flutter (16036): 5.0efewfeeff
I/flutter (16036): 5.0
I/flutter (16036): 10.0efewfeeff
I/flutter (16036): 10.0
I/flutter (16036): 15.0efewfeeff
I/flutter (16036): 15.0
I/flutter (16036): 20.0efewfeeff
I/flutter (16036): 20.0
...
我的问题是对话框中的百分比值始终显示为 0.0,尽管控制台中显示的值在变化。如何更新对话框中的值?
使用 StatefulBuilder 在 Dialog 内部使用 setState 并仅在其中更新 Widgets。这是一个例子。
showDialog(
context: context,
builder: (context) {
String contentText = "Content of Dialog";
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("Title of Dialog"),
content: Text(contentText),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text("Cancel"),
),
TextButton(
onPressed: () {
setState(() {
contentText = "Changed Content of Dialog";
});
},
child: Text("Change"),
),
],
);
},
);
},
);