Mustafa Shaikh
Mustafa Shaikh

Reputation: 45

Show Animation Controller value to list-view builder item in Flutter

Hi I'm building file upload to server using https with Progress bar It's working fine to upload image with percentage in LinearProgressIndicator.

My Problem is I want to show with animation controller value for that index.

Here is my working code.

     Map<int, double> fileDownloadProgress = {};
    late AnimationController loadingController;

 @override
  void initState() {
    super.initState();
    var docProvider = Provider.of<DocProviderBLoC>(context, listen: false);
    docProvider.getDocumentList();
    loadingController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 10),
    )..addListener(() {
        setState(() {});
      });
  }

ListView.builder(
                    itemCount: doc.getDocument.length + 1,
                    physics: const BouncingScrollPhysics(),
                    shrinkWrap: true,
                    itemBuilder: (ctx, index) {
                      progress = downloadProgress[index];
                      if (index == 0) {
                        return InkWell(
                          onTap: () => _getUserSelfie(index),
                          child: DocumentsWidget(
                            fileName:
                                AppLocalizations.of(context)!.upload_selfie,
                            percentage: progress,
                          ),
                        );
                      } else {
                        fileProgress = fileDownloadProgress[index - 1];
                        return GestureDetector(
                          onTap: () => _showBottomFilePicker(index - 1, doc),
                          child: DocumentsWidget(
                            fileName: doc.docData[index - 1].name,
                            percentage: fileProgress,
                          ),
                        );
                      }
                    },
                  )

Method to upload file to server:

 _asyncFileUpload(String text, File file, int i, DocProviderBLoC doc) async {
  byteCount += data.length;

        // debugPrint(
        //     'Total ByteCount => $byteCount  and totalByteLength  =>  $totalByteLength');

        var percentage = byteCount / totalByteLength * 100;
        loadingController.forward();

  setState(() {
          valuePercentage = percentage / 100;
          fileDownloadProgress[i] = valuePercentage;
          debugPrint('downloadProgress => ${fileDownloadProgress[i]}');
        });
}

Upvotes: 0

Views: 379

Answers (1)

Mustafa Shaikh
Mustafa Shaikh

Reputation: 45

To fix the problem you need to assign the animation controller value to the fileProgress.

if (fileProgress != null) {
    fileProgress = loadingController.value;
}

Upvotes: 1

Related Questions