フラッターCircularprogressインジケーターを非表示または非表示にするにはどうすればよいですか[重複]

Dec 18 2020

APIからデータを取得するだけで、APIが読み込まれるまでローダーを表示する必要があります。問題はローダーが正常に機能していることですが、どうすれば閉じることができますか。

私のコード

   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


        }
      }

回答

UmaizKhan Dec 18 2020 at 19:02

Stackoverflowへようこそ:)

このトリックNavigator.pop(context)によってこれを行うことができます。私はあなたがこのようにできる条件で成功したときに隠す必要があると思います

    if(data['status'].toString() == "success"){
       Navigator.pop(context)
    }
StefanoA. Dec 18 2020 at 19:00

Dialogを含むを却下する場合はCircularProgressIndicator、すべての操作が完了した直後に、次のコードを使用できます。

Navigator.pop(context);

これは、あなたの例で実装すると、次のようになります。

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
     }
   }
 }