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();
相关推荐
晓得迷路了2 分钟前
栗子前端技术周刊第 105 期 - npm 安全性加强、Storybook 10、htmx 4.0 Alpha 1...
前端·javascript·npm
七号练习生.c19 分钟前
CSS入门
前端·css·tensorflow
程序员爱钓鱼20 分钟前
Python编程实战——Python实用工具与库:Matplotlib数据可视化
前端·后端·python
程序员爱钓鱼23 分钟前
Python编程实战 - Python实用工具与库 - requests 与 BeautifulSoup
前端·后端·python
wyzqhhhh24 分钟前
前端跨页面通信
前端
G018_star sky♬30 分钟前
原生JavaScript实现输入验证的界面
javascript·css·css3
火龙谷33 分钟前
DrissionPage遇到iframe
开发语言·前端·javascript
千里马-horse1 小时前
搭建 React Native 库
javascript·react native·react.js·native library
艾小码1 小时前
从零到一:这篇JavaScript指南让你成为独立开发者
前端·javascript