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();
相关推荐
乐观主义现代人9 分钟前
go 面试
java·前端·javascript
1***Q78431 分钟前
前端在移动端中的离线功能
前端
星环处相逢34 分钟前
Nginx 优化与防盗链及扩展配置指南
服务器·前端·nginx
2501_9418868643 分钟前
多语言微服务架构下的微服务熔断与限流优化实践
javascript
tsumikistep43 分钟前
【前后端】Vue 脚手架与前端工程结构入门指南
前端·javascript·vue.js
在繁华处1 小时前
JAVA实战:文件管理系统1.0
java·开发语言·前端
GISer_Jing1 小时前
SSE Conf大会分享支付宝xUI引擎:AI时代的多模态交互革命
前端·人工智能·交互
Aerelin1 小时前
爬虫playwright中的资源监听
前端·爬虫·js·playwright
WordPress学习笔记1 小时前
专业建外贸网站公司推荐
大数据·前端·人工智能
fruge2 小时前
前端简历优化:如何突出项目亮点与技术深度(附示例)
前端