Flutter - ボタンに円形の読み込みインジケーターを追加するにはどうすればいいですか? 質問する

Flutter - ボタンに円形の読み込みインジケーターを追加するにはどうすればいいですか? 質問する

Flutter コードがあります。送信ボタンがクリックされたときに何も表示されないのではなく、ボタンがクリックされたときに円形の読み込みインジケーターを表示して、ユーザーを忙しくさせたいのですが、それを実行するチュートリアルを自分のコードに変換するのが困難です。

チュートリアルは次のとおりです:

...
 children: <Widget>[
            new Padding(
              padding: const EdgeInsets.all(16.0),
              child: new MaterialButton(
                child: setUpButtonChild(),
                onPressed: () {
                  setState(() {
                    if (_state == 0) {
                      animateButton();
                    }
                  });
                },
                elevation: 4.0,
                minWidth: double.infinity,
                height: 48.0,
                color: Colors.lightGreen,
              ),
            )
          ],
 Widget setUpButtonChild() {
    if (_state == 0) {
      return new Text(
        "Click Here",
        style: const TextStyle(
          color: Colors.white,
          fontSize: 16.0,
        ),
      );
    } else if (_state == 1) {
      return CircularProgressIndicator(
        valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
      );
    } else {
      return Icon(Icons.check, color: Colors.white);
    }
  }

  void animateButton() {
    setState(() {
      _state = 1;
    });

    Timer(Duration(milliseconds: 1000), () {
      setState(() {
        _state = 2;
      });
    });

    Timer(Duration(milliseconds: 3300), () {
       Navigator.of(context).push(
        MaterialPageRoute(
          builder: (context) => AnchorsPage(),
        ),
      );
    });
  }

これが私のコードです。私がやりたいのは、CircularProgressIndicatorシステムが HTTP リクエストを実行しているときにそれを表示することだけです。

そして、私が使用したいコードは次のとおりですCircularProgressIndicator:

                           Center(

                            child: 
                            RaisedButton(
                              padding: EdgeInsets.fromLTRB(80, 10, 80, 10),
                              color: Colors.green,
                             
                              child: setUpButtonChild(),
                             
                              onPressed: ()  {

                                setState(()async {
                                _state = 1;
                                var toSubmit = {
                                  "oid": EopOid,
                                  "modifiedBy": user['UserName'].toString(),
                                  "modifiedOn": DateTime.now().toString(),
                                  "submitted": true,
                                  "submittedOn": DateTime.now().toString(),
                                  "submittedBy": user['UserName'].toString()
                                };
                                for (EopLine i in selectedEops) {
                                  var item = {
                                    "oid": i.oid.toString(),
                                    "quantityCollected": i.quantityCollected,
                                    "modifiedBy": user['UserName'].toString(),
                                    "modifiedOn": DateTime.now().toString(),
                                  };
                                  await http
                                      .put(
                                          "http://api.ergagro.com:112/UpdateEopLine",
                                          headers: {
                                            'Content-Type': 'application/json'
                                          },
                                          body: jsonEncode(item))
                                      .then((value) async {
                                    if (selectedEops.indexOf(i) ==
                                        selectedEops.length - 1) {
                                      await http
                                          .put(
                                              "http://api.ergagro.com:112/SubmitEop",
                                              headers: {
                                                'Content-Type':
                                                    'application/json'
                                              },
                                              body: jsonEncode(toSubmit))
                                          .then((value) {
                                        print('${value.statusCode} submitted');
                                        Navigator.pop(context);
                                      });
                                    }
                                  });
                                }
                               _state = 2;
                                });
                              //Navigator.of(context).push(MaterialPageRoute(
                              //builder: (context) =>
                              //StartScanPage(widget.dc_result)));
                              },
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(50),
                              ),
                            ),
                          ),

ベストアンサー1

ボタンを使用している場合はicon()コンストラクター (アイコン + テキスト) を使用すると、ボタンの状態が変わったときにアイコンを交換できますCircularProgressIndicator。アイコンとインジケーターの両方がウィジェットであるため、これは機能します。

return ElevatedButton.icon(
  onPressed: _isLoading ? null : _onSubmit,
  style: ElevatedButton.styleFrom(padding: const EdgeInsets.all(16.0)),
  icon: _isLoading
      ? Container(
          width: 24,
          height: 24,
          padding: const EdgeInsets.all(2.0),
          child: const CircularProgressIndicator(
            color: Colors.white,
            strokeWidth: 3,
          ),
        )
      : const Icon(Icons.feedback),
  label: const Text('SUBMIT'),
);

ライブデモ

おすすめ記事