【问题标题】:I'm having trouble drawing the progress indicator我在绘制进度指示器时遇到问题
【发布时间】:2021-05-07 22:03:57
【问题描述】:

我正在实现文件上传。我想使用 CircularProgressIndicator 显示上传进度。

FileUploadController 类的uploadFile() 函数将文件上传到服务器并告知上传进度的百分比。并且每帧都会调用'NotifyListeners()'来通知上传进度。

我尝试使用此信息绘制 CircularProgressIndicator,但出现错误。 "setstate() 或 markneedsbuild() 在构建期间调用。"

  @override
  Widget build(BuildContext context) {
    var uploadController = Provider.of<FileUploadController>(context);
    uploadController.uploadFile(userFiles, context);//upload files and update uploadPercentage
    return ListTile(
      leading: Stack(
        children: [
          CircularProgressIndicator(
            value: userFiles.uploadPercentage,
            strokeWidth: userFiles.uploadPercentage == 1 ? 0 : 1,
          ),
          Positioned(
            right: 3,
            left: 3,
            top: 3,
            bottom: 3,
            child: CircleAvatar(
              backgroundColor: Colors.white,
              backgroundImage:
                  AssetImage('assets/images/fileTypeLogos/pdf.png'),
            ),
          ),
        ],
      ),
      title: Text(widget.fileName),
      trailing: IconButton(
        icon: Icon(Icons.close),
        onPressed: () => widget.removeWhenCloseButtonTap(widget.fileName),
      ),
    );
  }

我认为问题在于在 build() 中调用 notifyListeners()(uploadFile 函数调用它)。我该如何解决?

【问题讨论】:

    标签: flutter flutter-provider flutter-circularprogressindicator


    【解决方案1】:

    是的,这可能就是问题所在。您可能想在initState() 内拨打uploadFile() 的电话:

    FileUploadController uploadController(BuildContext context) => Provider.of<FileUploadController>(context);
    
    @override
    void initState() {
      super.initState();
      uploadController(context).uploadFile(userFiles, context);
    }
    

    如果您在无状态小部件中,请将其转换为有状态小部件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2013-12-31
      • 1970-01-01
      • 2022-06-17
      • 2020-05-31
      • 2021-12-03
      • 2011-12-17
      相关资源
      最近更新 更多