Flutter进度指示插件percent_indicator_premium的使用

发布于 1周前 作者 zlyuanteng 来自 Flutter

Flutter进度指示插件percent_indicator_premium的使用

这是一个简单且高度可定制的包,适用于各种用户。

特性

  • 🛠️ 圆形进度指示器:创建具有可自定义颜色、大小和百分比的视觉上吸引人的圆形进度指示器。
  • 🔢 垂直进度指示器:构建简洁现代的垂直进度指示器以用于各种场景。
  • 🔢 水平进度指示器:构建简洁现代的水平进度指示器以用于各种场景。
  • 🔢 方形进度指示器:构建简洁现代的方形进度指示器以用于各种场景。
  • ⌨️ 自定义小部件:轻松将图标、文本或其他小部件嵌入到您的指示器中心。
  • 🔐 轻量级且快速:优化性能,确保在应用程序中无缝使用。

安装

pubspec.yaml文件中添加以下行:

dependencies:
  percent_indicator_premium: ^0.0.1  // 运行 `flutter pub get` 来安装包。

使用

这里有一个快速入门的例子:

水平进度指示器

HorizontalPercentIndicator(
    this.height = 30,
    this.width,
    this.borderRadius = 8,
    required this.loadingPercent,
    this.inactiveTrackColor = MyColor.inActiveColor,
    this.child,
    this.activeTrackColor = const [MyColor.skyPrimary, MyColor.skySecondary],
),

垂直进度指示器

VerticalPercentIndicator(
    this.height = 120,
    this.width = 30,
    this.borderRadius = 8,
    required this.loadingPercent,
    this.inactiveTrackColor = MyColor.inActiveColor,
    this.child,
    this.activeTrackColor = const [MyColor.skyPrimary, MyColor.skySecondary],
),

圆形进度指示器

CircularPercentIndicator(
    this.height = 150,
    this.width = 150,
    required this.loadingPercent,
    this.inActiveTrackColor = MyColor.inActiveColor,
    this.child,
    this.activeTrackColor = const [
      Colors.deepOrangeAccent,
      Colors.greenAccent,
      Color(0xFF913A84),
      Colors.deepOrangeAccent
    ],
),

方形进度指示器

SquarePercentIndicator(
    this.height = 150,
    this.width = 150,
    required this.loadingPercent,
    this.borderRadius = 12,
    this.indicatorColor = MyColor.skyPrimary,
    this.backColor = MyColor.inActiveColor,
    this.child
),

参数

// 对于水平进度指示器
HorizontalPercentIndicator{
    final double height;
    final double? width;
    final double borderRadius;
    final double loadingPercent;
    final Color inactiveTrackColor;
    // 你想放在进度指示器前面的小部件,默认显示百分比文本
    final Widget? child;
    // 如果你想要可以设置多种颜色
    final List<Color> activeTrackColor;
}
// 对于垂直进度指示器
VerticalPercentIndicator{
    final double height;
    final double width;
    final double borderRadius;
    final double loadingPercent;
    final Color inactiveTrackColor;
    // 你想放在进度指示器前面的小部件,默认显示百分比文本
    final Widget? child;
    // 如果你想要可以设置多种颜色
    final List<Color> activeTrackColor;
}
// 对于圆形进度指示器
CircularPercentIndicator{
   final double height;
   final double width;
   final double loadingPercent;
   final Color inActiveTrackColor;
   // 你想放在进度指示器前面的小部件,默认显示百分比文本
   final Widget? child;
   // 如果你想要可以设置多种颜色
   final List<Color> activeTrackColor;
}
// 对于方形进度指示器
SquarePercentIndicator{
    final double height;
    final double width;
    final double loadingPercent;
    final double borderRadius;
    final Color indicatorColor;
    final Color backColor;
    // 你想放在进度指示器前面的小部件,默认显示百分比文本
    final Widget? child;
}

示例

查看完整的示例代码:

import 'package:flutter/material.dart';
import 'package:percent_indicator_premium/config/theme/theme.dart';
import 'package:percent_indicator_premium/percent_indicator_premium.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Percent Indicator Premium',
      debugShowCheckedModeBanner: false,
      theme: AppTheme.light(),
      home: const Home(),
    );
  }
}

class Home extends StatefulWidget {
  const Home({super.key});
  [@override](/user/override)
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  [@override](/user/override)
  void initState() {
    super.initState();
    _controller = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 3000));
    if (mounted) _controller.repeat();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("加载百分比"),
        centerTitle: true,
      ),
      body: AnimatedBuilder(
        animation: _controller,
        builder: (context, _) => SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(12),
            child: SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const SizedBox(height: 20),
                  HorizontalPercentIndicator(loadingPercent: _controller.value),
                  const SizedBox(height: 20),
                  VerticalPercentIndicator(loadingPercent: _controller.value),
                  const SizedBox(height: 20),
                  CircularPercentIndicator(loadingPercent: _controller.value),
                  const SizedBox(height: 20),
                  SquarePercentIndicator(loadingPercent: _controller.value),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

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

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

1 回复

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


当然,下面是一个关于如何在Flutter应用中使用percent_indicator_premium插件的示例代码。这个插件提供了一个高级的进度指示器,可以灵活定制以满足各种需求。

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

dependencies:
  flutter:
    sdk: flutter
  percent_indicator_premium: ^latest_version  # 请替换为最新版本号

然后运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter应用中使用PercentIndicator组件。以下是一个完整的示例,展示如何在一个简单的Flutter应用中集成和使用percent_indicator_premium插件:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Percent Indicator Premium Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Percent Indicator Premium Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                height: 200,
                child: LinearPercentIndicator(
                  width: 0.8,
                  lineHeight: 20.0,
                  percent: 0.75, // 进度值,范围从0到1
                  linearStrokeCap: LinearStrokeCap.roundAll,
                  backgroundColor: Colors.grey[200]!,
                  progressColor: Colors.blue,
                  animation: true,
                  animationDuration: 2000,
                ),
              ),
              SizedBox(height: 20),
              CircularPercentIndicator(
                radius: 120.0,
                lineWidth: 12.0,
                percent: 0.5, // 进度值,范围从0到1
                center: Text(
                  '50%',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
                ),
                progressColor: Colors.green,
                backgroundColor: Colors.grey[300]!,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们展示了两种类型的进度指示器:

  1. LinearPercentIndicator:一个线性进度条。你可以通过percent属性设置进度值,progressColorbackgroundColor属性分别设置进度条的颜色和背景颜色。animationanimationDuration属性用于控制进度动画。

  2. CircularPercentIndicator:一个圆形进度条。同样,你可以通过percent属性设置进度值,progressColorbackgroundColor属性分别设置进度条的颜色和背景颜色。center属性允许你在进度条的中央放置自定义内容,如文本。

你可以根据需要进一步自定义这些进度指示器,比如调整大小、颜色、动画效果等。percent_indicator_premium插件提供了丰富的选项来满足不同的设计需求。

回到顶部