Flutter进度显示插件jprogress的使用

Flutter进度显示插件jprogress的使用

JProgress 是一个包含基于时间的进度控件的包,并且允许用户根据选择设置值。

特性

  • 时间控制 – 开始 - 停止 - 持续时间等

入门指南

  • 圆形定时进度
  • 圆形自定义进度
  • 线性定时进度
  • 线性自定义进度

使用方法

要使用 ProgressController,必须初始化并释放资源。

late ProgressController controller;

[@override](/user/override)
void initState() {
  controller = ProgressController(
    duration: const Duration(minutes: 3), // 设置持续时间为3分钟
    timeFormat: DateFormatN.SecMin, // 设置时间格式为秒和分钟
    tickPeriod: const Duration(milliseconds: 1000), // 设置每次更新的时间间隔为1秒
  );
  super.initState();
}

// 释放资源
[@override](/user/override)
void dispose() {
  controller.dispose(); // 释放控制器资源
  super.dispose();
}

额外信息

Just Codes Developers 提供。 “JProgress”

完整示例代码

import 'package:flutter/material.dart';
import 'package:jprogress/jprogress.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // 这是应用的根组件
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'JProgress 示例',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late ProgressController controller;
  String b = "";

  [@override](/user/override)
  void initState() {
    controller = ProgressController(
      duration: const Duration(minutes: 3),
      timeFormat: DateFormatN.SecMin,
      tickPeriod: const Duration(milliseconds: 1000),
    );
    super.initState();
  }

  [@override](/user/override)
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          const SizedBox(
            height: 10,
          ),
          Expanded(
            child: ListView(
              children: [
                progress(),
                button("定时进度圆圈", "1"),
                button("定时进度线性", "2"),
                button("仅定时", "3"),
                button("自定义进度圆圈", "4"),
                button("自定义进度线性", "5"),
              ],
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.stop),
        onPressed: () {
          controller.stop();
        },
      ),
    );
  }

  Widget progress() {
    switch (b) {
      case "1":
        return CountdownProgress(
          controller: controller,
          valueColor: Colors.blue,
          backgroundColor: Colors.white,
          textStyle: Theme.of(context).textTheme.overline,
          strokeWidth: 3.0,
          progressWidget: ProgressWidget.Indicator,
          progressType: ProgressType.Circular,
          size: 150,
          onTimeout: () => controller.stop(), // 超时后停止或执行其他操作
          onUpdate: () {
            setState(() {}); // 更新UI
          },
        );
      case "2":
        return CountdownProgress(
          controller: controller,
          valueColor: Colors.red,
          backgroundColor: Colors.white,
          textStyle: Theme.of(context).textTheme.overline,
          strokeWidth: 3.0,
          progressWidget: ProgressWidget.Indicator,
          progressType: ProgressType.Linear,
          size: 90,
          onTimeout: () => controller.stop(), // 超时后停止或执行其他操作
          onUpdate: () {
            setState(() {}); // 更新UI
          },
        );
      case "3":
        return CountdownProgress(
          controller: controller,
          size: 90,
          onTimeout: () => controller.stop(), // 超时后停止或执行其他操作
          onUpdate: () {
            setState(() {}); // 更新UI
          },
        );
      case "4":
        return CustomProgress(
          valueColor: Colors.red,
          backgroundColor: Colors.white,
          textStyle: Theme.of(context).textTheme.overline,
          strokeWidth: 3.0,
          progressType: ProgressType.Circular,
          size: 90,
          title: "你好",
          value: controller.valuePercent,
        );
      case "5":
        return CustomProgress(
          valueColor: Colors.red,
          backgroundColor: Colors.white,
          textStyle: Theme.of(context).textTheme.overline,
          strokeWidth: 3.0,
          size: 90,
          title: "你好",
          progressType: ProgressType.Linear,
          value: controller.valuePercent,
        );
      default:
        return const SizedBox(
          height: 1,
        );
    }
  }

  Widget button(String title, String p) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: InkWell(
        onTap: () {
          controller.start(); // 开始计数
          setState(() => b = p); // 更新当前选中的进度类型
        },
        child: Container(
          height: 50,
          width: 80,
          decoration: const BoxDecoration(
              color: Colors.grey,
              borderRadius: BorderRadius.all(Radius.circular(20))),
          child: Center(
            child: Text(
              title,
              style: const TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                  fontSize: 14),
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter进度显示插件jprogress的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter进度显示插件jprogress的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


jprogress 是一个用于在 Flutter 应用中显示进度条或加载指示器的插件。它提供了多种样式和配置选项,帮助开发者轻松地集成进度指示器到应用中。以下是如何在 Flutter 项目中使用 jprogress 插件的基本指南。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 jprogress 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  jprogress: ^1.0.0  # 请检查是否有更新的版本

然后运行 flutter pub get 以获取依赖。

2. 基本使用

2.1 导入包

在需要使用 jprogress 的 Dart 文件中导入包:

import 'package:jprogress/jprogress.dart';

2.2 使用 JProgress 组件

JProgress 提供了多种进度条样式,你可以通过设置不同的属性来定制进度条的外观和行为。

简单使用示例:

import 'package:flutter/material.dart';
import 'package:jprogress/jprogress.dart';

class ProgressExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('JProgress Example'),
      ),
      body: Center(
        child: JProgress(
          type: JProgressType.bar, // 使用条形进度条
          value: 0.5, // 设置进度值(0.0 - 1.0)
          backgroundColor: Colors.grey[300],
          valueColor: Colors.blue,
        ),
      ),
    );
  }
}

圆形进度指示器示例:

Center(
  child: JProgress(
    type: JProgressType.circular, // 使用圆形进度指示器
    value: 0.7, // 设置进度值(0.0 - 1.0)
    backgroundColor: Colors.grey[300],
    valueColor: Colors.green,
    strokeWidth: 5.0, // 设置圆形进度条的宽度
  ),
),

3. 自定义属性

JProgress 提供了多个属性来自定义进度条的外观和行为:

  • type: 进度条的类型,可以是 JProgressType.bar(条形进度条)或 JProgressType.circular(圆形进度指示器)。
  • value: 进度值,范围从 0.01.0
  • backgroundColor: 进度条的背景颜色。
  • valueColor: 进度条的前景颜色。
  • strokeWidth: 圆形进度条的宽度(仅适用于 JProgressType.circular)。
  • minHeight: 条形进度条的最小高度(仅适用于 JProgressType.bar)。
  • borderRadius: 条形进度条的圆角半径(仅适用于 JProgressType.bar)。

4. 完整示例

以下是一个完整的示例,展示了如何在 Flutter 应用中使用 jprogress 插件:

import 'package:flutter/material.dart';
import 'package:jprogress/jprogress.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ProgressExample(),
    );
  }
}

class ProgressExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('JProgress Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            JProgress(
              type: JProgressType.bar,
              value: 0.5,
              backgroundColor: Colors.grey[300],
              valueColor: Colors.blue,
              minHeight: 10.0,
              borderRadius: 5.0,
            ),
            SizedBox(height: 20),
            JProgress(
              type: JProgressType.circular,
              value: 0.7,
              backgroundColor: Colors.grey[300],
              valueColor: Colors.green,
              strokeWidth: 5.0,
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部