圆形进度条颤动

时间:2020-06-28 07:57:23

标签: firebase flutter dart google-cloud-firestore

Future uploadImage(BuildContext context) async {
  final picker = ImagePicker();
  final pickedFile = await picker.getImage(source: ImageSource.gallery);
  setState(() {
    _image = File(pickedFile.path);
  });
  StorageReference firebaseStorageRef = FirebaseStorage.instance
      .ref()
      .child('event_profile/${Path.basename(_image.path)}}');
  StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
  var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();
  setState(() {
    _imageURL = dowurl.toString();
  });
  showAlertDialog(context);
  print(_imageURL);
 }

这是我用于将图像上传到Firebase存储的代码。 如何使用圆形进度条向用户指示图像仍在上传。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以创建一个布尔实例变量isLoading并相应地对其进行更新。示例:

bool isLoading = false;


Future uploadImage(BuildContext context) async {
  final picker = ImagePicker();
  final pickedFile = await picker.getImage(source: ImageSource.gallery);
  setState(() {
    _image = File(pickedFile.path);
    isLoading = true; //add this line
  });
  StorageReference firebaseStorageRef = FirebaseStorage.instance
      .ref()
      .child('event_profile/${Path.basename(_image.path)}}');
  StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
  var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();
  setState(() {
    _imageURL = dowurl.toString();
    isLoading = false;
  });
  showAlertDialog(context);
  print(_imageURL);
 }

然后在build方法中,您可以执行以下操作:

            isLoading
                ? CircularProgressIndicator()
                : Visibility(visible: false, child: Text("test")),

因此,以这种方式,当isLoading等于false时,CircularProgressIndicator()将不会出现,当其更改为truesetState()将调用build()方法和CircularProgressIndicator()将会出现。