flutter 局部view更新,dialog更新进度,dialog更新

局部更新有好几种方法,本次使用的是 StatefulBuilder 定义 customState去更新对话框内容

Dart 复制代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ProgressDialog {
  final BuildContext context;
  BuildContext? dialogContext;
  double _progress = 0.0;
  bool _isShowing = false;
  StateSetter? mCustomState;

  ProgressDialog(this.context, this._progress);

  void show() {
    _isShowing = true;
    showDialog<void>(
      context: context,
      builder: (BuildContext context) {
        return StatefulBuilder(builder: (mDialogContext, customState) {
          mCustomState =customState;
          dialogContext = mDialogContext;
          return AlertDialog(
            title: const Text('下载中...'),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                LinearProgressIndicator(value: _progress),
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 20.0),
                  child: Text('${(_progress * 100).toStringAsFixed(0)}%'),
                ),
              ],
            ),
          );
        });
      },
    );
  }

  void updateProgress(double progress) {
    if (_isShowing && null!=mCustomState) {
      (mCustomState!)(() {
        _progress = progress;
      });
    }
  }

  void hide() {
    if (dialogContext != null && _isShowing) {
      Navigator.of(dialogContext!).pop();
    }
    _isShowing = false;
  }

}

定义 StateSetter? mCustomState; 去set更新

使用

Dart 复制代码
   final ProgressDialog progressDialog = ProgressDialog(context, 0);
    progressDialog.show();


 更新进度
progressDialog.updateProgress(progress);

关闭对话框
progressDialog.hide();
相关推荐
蓝倾7 分钟前
淘宝批量获取商品SKU实战案例
前端·后端·api
comelong11 分钟前
Docker容器启动postgres端口映射失败问题
前端
花海如潮淹13 分钟前
硬件产品研发管理工具实战指南
前端·python
用户38022585982413 分钟前
vue3源码解析:依赖收集
前端·vue.js
用户75794199497013 分钟前
基于JavaScript的简易Git
javascript
WaiterL14 分钟前
一文读懂 MCP 与 Agent
前端·人工智能·cursor
gzzeason16 分钟前
使用Vite创建React初始化项目
前端·javascript·react.js
又双叒叕77817 分钟前
React19 新增Hooks:useOptimistic
前端·javascript·react.js
归于尽36 分钟前
V8 引擎是如何给 JS"打扫房间"的 ?
前端·javascript
小old弟36 分钟前
让对象保持定义的顺序来排列
前端