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();
相关推荐
毛骗导演几秒前
万字解析 OpenClaw 源码架构-架构概览
前端·架构
天才熊猫君9 分钟前
Flex布局深度解析:为什么我的Flex项目不按预期收缩?
前端
风雪心9 分钟前
Antd组件库Form的onValuesChange和getFieldsValue的调用时机分析
前端
Moment10 分钟前
2026 趋势预测:Vibe Coding 之后,人人都会拥有专属 Agent 吗?
前端·javascript·后端
前端Hardy25 分钟前
Bun 1.0 正式发布:JavaScript 运行时的新王者?启动快 5 倍,打包小 90%!
前端·javascript·面试
Bigger36 分钟前
从 Grunt 到 Vite:前端构建工具十几年的演化
前端·vite·前端工程化
IT_陈寒37 分钟前
Python 性能提升50%的5个魔法技巧,90%的人还不知道!
前端·人工智能·后端
前端Hardy37 分钟前
别再乱写正则了!一行 regex 可能让你的网站瘫痪 10 分钟
前端·javascript·面试
gyx_这个杀手不太冷静1 小时前
OpenCode 进阶使用指南(第二章:Skills 系统)
前端·ai编程
牛奶1 小时前
浏览器到底在偷偷帮你做什么?——HTTP缓存与刷新机制
前端·http·浏览器