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

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

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更新

使用

   final ProgressDialog progressDialog = ProgressDialog(context, 0);
    progressDialog.show();


 更新进度
progressDialog.updateProgress(progress);

关闭对话框
progressDialog.hide();

相关推荐

  1. Flutter APP下载更新

    2024-03-21 23:10:03       36 阅读
  2. Flutter中自定义Dialog

    2024-03-21 23:10:03       40 阅读
  3. flutter 版本自动更新调研

    2024-03-21 23:10:03       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-21 23:10:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-21 23:10:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-21 23:10:03       87 阅读
  4. Python语言-面向对象

    2024-03-21 23:10:03       96 阅读

热门阅读

  1. QT5信号函数的绑定方式

    2024-03-21 23:10:03       46 阅读
  2. SpringWeb

    SpringWeb

    2024-03-21 23:10:03      35 阅读
  3. 笔记:Mysql 主从搭建

    2024-03-21 23:10:03       42 阅读
  4. 椋鸟数据结构笔记#1:数据结构、顺序表

    2024-03-21 23:10:03       45 阅读
  5. SpringBoot注解

    2024-03-21 23:10:03       50 阅读
  6. 蓝桥杯算法基础(28)11道关于字符串的小题

    2024-03-21 23:10:03       36 阅读
  7. rocketmq 4.9.6安装

    2024-03-21 23:10:03       48 阅读
  8. 手势追踪技术在HTC VIVE中的应用与实现

    2024-03-21 23:10:03       46 阅读
  9. 7 Internet基础(2)

    2024-03-21 23:10:03       44 阅读