Flutter, jak mogę ukryć lub odrzucić okrągły wskaźnik postępu [duplikat]
Dec 18 2020
Po prostu pobieram dane z API, potrzebuję tylko pokazać program ładujący, aż API zostanie załadowane. Problem polega na tym, że program ładujący działa poprawnie, ale nie wiem, jak mogę go zamknąć.
Mój kod
Future<http.Response> _trySubmit3() async {
final isValid2 = _formKey3.currentState.validate();
FocusScope.of(context).unfocus();
if (isValid2) {
showDialog(
context: context,
builder: (BuildContext context) {
return Center(child: CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(Color(0xff00abb5)),
),);
});
print(smsOTP.text);
print(userConfirmPassword.text);
var url = '123/set_password.php?email=${userEmail.text}&password=${userConfirmPassword.text}';
print(url);
http.Response res = await http.get(url,
headers: <String, String>{
'token': 'my token'
},
);
var data = json.decode(res.body.toString());
print(data);
print(data['status']);
if(data['status'].toString() == "success"){
// I need to close the loader here
}
}
Odpowiedzi
UmaizKhan Dec 18 2020 at 19:02
Witamy w Stackoverflow :)
Możesz to zrobić za pomocą tej sztuczki Navigator.pop (kontekst). Myślę, że musisz się ukryć, kiedy jego sukces jest pod warunkiem, że możesz to zrobić
if(data['status'].toString() == "success"){
Navigator.pop(context)
}
StefanoA. Dec 18 2020 at 19:00
Jeśli szukasz odrzucenia Dialog
zawierającego CircularProgressIndicator
, natychmiast po zakończeniu wszystkich operacji, możesz użyć następującego kodu:
Navigator.pop(context);
Który zaimplementowany w Twoim przykładzie wyglądałby następująco:
Future<http.Response> _trySubmit3() async {
final isValid2 = _formKey3.currentState.validate();
FocusScope.of(context).unfocus();
if (isValid2) {
showDialog(
context: context,
builder: (BuildContext context) {
return Center(child: CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(Color(0xff00abb5)),
),);
});
print(smsOTP.text);
print(userConfirmPassword.text);
var url = '123/set_password.php?email=${userEmail.text}&password=${userConfirmPassword.text}';
print(url);
http.Response res = await http.get(url,
headers: <String, String>{
'token': 'my token'
},
);
var data = json.decode(res.body.toString());
print(data);
print(data['status']);
if(data['status'].toString() == "success"){
Navigator.pop(context); // closing the Dialog with the CircularProgressIndicator
}
}
}