flutter之进度条

flutter中进度条中的线性进度条,实现其3秒走完的功能搞了一段时间,主要是要使用await方法等待一段时间才去改变其参数.

for(int i = 1; i <= 30; i++) {
        // ignore: close_sinks
        await Socket.connect("1.1.1.1", 8888, timeout: new Duration(milliseconds: 100)).catchError(
                (e) { isShowProgressBar = true; });
        String pText = "正在更新$version版本,请等待";
        switch(i%4) {
          case 1:
            pText = "正在更新$version版本,请等待.";
            break;
          case 2:
            pText = "正在更新$version版本,请等待..";
            break;
          case 3:
            pText = "正在更新$version版本,请等待...";
            break;
        }
        setState(() {
          progressBar = Center(
            child: Container(
              margin: const EdgeInsets.fromLTRB(36.0, 0.0, 36.0, 60.0),
              child: new Column(
                verticalDirection: VerticalDirection.up,
                children: <Widget> [
                  _getNameText(context, pText),
                  new LinearProgressIndicator(value: i/30),
                ]
              ),
            ),
          );
        });
      }

其中1.1.1.1是随便填的一个有效的ip,这样就必须等待await的结果才改变,之前一直不能原因是异步的机制导致逻辑虽然正确但是却不能实现想要的效果。我这使用socket连接设置timeout时间,还可以自己写返回Future的方法去写。

发布了83 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42317011/article/details/101199727