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();
相关推荐
打瞌睡的朱尤4 小时前
Vue day10 完整购物网页(登录页,首页,搜索)
前端·javascript·vue.js
扶苏10025 小时前
深入理解 Vue 3 的 watchEffect
前端·javascript·vue.js
松叶似针7 小时前
Flutter三方库适配OpenHarmony【secure_application】— OpenHarmony 插件工程搭建
flutter·harmonyos
未来龙皇小蓝7 小时前
RBAC前端架构-05:引入Element-UI及相关逻辑
前端·ui
yanlele8 小时前
AI Coding 时代下, 关于你会写代码这件事儿, 还重要吗?
前端·javascript·ai编程
打瞌睡的朱尤8 小时前
Vue day9 购物车,项目,vant组件库,vw,路由
前端·javascript·vue.js
lqj_本人9 小时前
Flutter三方库适配OpenHarmony【apple_product_name】华为nova系列设备映射表
flutter·华为
星火开发设计10 小时前
模板参数:类型参数与非类型参数的区别
java·开发语言·前端·数据库·c++·算法
空白诗11 小时前
基础入门 Flutter for OpenHarmony:ClipRRect 圆角裁剪组件详解
flutter
键盘鼓手苏苏11 小时前
Flutter for OpenHarmony 实战:just_audio 音乐播放器深度适配与进阶
flutter