Flutter圆形进度条插件radial_progress的使用
Flutter圆形进度条插件radial_progress的使用
radial_progress
是一个用于在Flutter应用中显示动态百分比进度条的插件。它支持动画效果,可以应用于各种需要展示进度的应用场景。
安装包
首先,在你的 pubspec.yaml
文件中添加以下依赖:
dependencies:
radial_progress: ^0.1.0
然后运行以下命令来获取依赖包:
flutter pub get
使用方法
导入包
在你的Dart代码中导入该包:
import 'package:radial_progress/radial_progress.dart';
基本用法
percent
: 进度值,应该在0.0到1.0之间。diameter
: 圆形进度条的直径,默认为80。progressLineWidth
: 进度线条宽度,默认为10,最大不能超过直径的三分之一。startAngle
: 进度条开始绘制的角度,是一个枚举类型。progressLineColors
: 进度线条颜色列表,根据进度值选择不同颜色。centerChild
: 中心显示的内容。
示例1:简单进度条
RadialProgressWidget(
percent: 0.35,
diameter: 180,
bgLineColor: Colors.cyan.withOpacity(0.2),
progressLineWidth: 16,
startAngle: StartAngle.top,
centerChild: const Text(
'\$ 547.52 - \$ 800.0',
maxLines: 1,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
);
示例2:带颜色渐变的进度条
RadialProgressWidget(
percent: 0.7,
diameter: 180,
bgLineColor: Colors.grey.withOpacity(0.2),
progressLineWidth: 24,
startAngle: StartAngle.top,
progressLineColors: const [
Colors.green,
],
centerChild: const Text(
'70 %',
maxLines: 1,
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
);
动态示例
以下是一个完整的Flutter应用程序示例,展示了如何使用 RadialProgressWidget
来创建动态进度条:
import 'package:flutter/material.dart';
import 'package:radial_progress/radial_progress.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Radial Progress Demo',
theme: ThemeData(
primarySwatch: Colors.cyan,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Radial Progress Demo'),
centerTitle: true,
),
body: SafeArea(
child: Column(
children: [
const SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const RadialSample1(),
RadialProgressWidget(
percent: 0.73,
diameter: 85,
progressLineWidth: 40,
startAngle: StartAngle.bottom,
enableAnimation: true,
animationDuration: const Duration(seconds: 2),
centerChild: const Text(
'-',
maxLines: 1,
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
),
),
),
],
),
const SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RadialProgressWidget(
percent: 0.23,
diameter: 100,
progressLineWidth: 24,
startAngle: StartAngle.start,
enableAnimation: true,
animationDuration: const Duration(seconds: 2),
progressLineColors: const [
Colors.lightBlueAccent,
Colors.lightBlue,
Colors.blue,
],
centerChild: const Text(
'23 %',
maxLines: 1,
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
),
RadialProgressWidget(
percent: 0.73,
diameter: 95,
progressLineWidth: 12,
startAngle: StartAngle.end,
enableAnimation: true,
animationDuration: const Duration(seconds: 2),
centerChild: Container(),
),
],
),
const SizedBox(height: 24),
const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RadialSample2(),
],
),
],
),
),
);
}
}
// Sample widget 1
class RadialSample1 extends StatefulWidget {
const RadialSample1({super.key});
[@override](/user/override)
State<RadialSample1> createState() => _RadialSample1State();
}
class _RadialSample1State extends State<RadialSample1> {
double _value = 0.0;
[@override](/user/override)
Widget build(BuildContext context) {
return RadialProgressWidget(
percent: 0.92,
diameter: 185,
progressLineWidth: 32,
startAngle: StartAngle.top,
enableAnimation: true,
animationDuration: const Duration(seconds: 4),
progressLineColors: const [
Colors.purple,
Colors.purpleAccent,
Colors.red,
Colors.orange,
Colors.yellow,
Colors.lightGreenAccent,
Colors.lightGreen,
Colors.green,
],
centerChild: Text(
'${(_value * 100).toInt()} %',
maxLines: 1,
style: const TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
),
),
callback: (value) {
setState(() {
_value = value;
});
},
);
}
}
// Sample widget 2
class RadialSample2 extends StatefulWidget {
const RadialSample2({super.key});
[@override](/user/override)
State<RadialSample2> createState() => _RadialSample2State();
}
class _RadialSample2State extends State<RadialSample2> {
double _value = 0.0;
[@override](/user/override)
Widget build(BuildContext context) {
return RadialProgressWidget(
percent: 0.57,
diameter: 300,
progressLineWidth: 24,
startAngle: StartAngle.top,
enableAnimation: true,
animationDuration: const Duration(seconds: 5),
progressLineColors: [
Colors.grey.shade600,
],
centerChild: Text(
(_value < 0.2)
? 'line 1\nsome description in line 2\nor each widget\n${(_value * 100).toInt()} %'
: (_value < 0.3)
? '🙂'
: (_value < 0.3)
? '🙃'
: (_value < 0.4)
? '😉'
: (_value < 0.5)
? '😌'
: '🤩',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: (_value < 0.2) ? 20 : 100,
fontWeight: FontWeight.bold,
),
),
callback: (value) {
setState(() {
_value = value;
});
},
);
}
}
更多关于Flutter圆形进度条插件radial_progress的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter圆形进度条插件radial_progress的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter中使用radial_progress
插件来创建一个圆形进度条的示例代码。radial_progress
是一个流行的Flutter插件,用于显示圆形的进度条。
首先,确保在你的pubspec.yaml
文件中添加radial_progress
依赖:
dependencies:
flutter:
sdk: flutter
radial_progress: ^x.y.z # 请将x.y.z替换为最新的版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Dart文件中,你可以使用RadialProgress
小部件来创建圆形进度条。以下是一个完整的示例:
import 'package:flutter/material.dart';
import 'package:radial_progress/radial_progress.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Radial Progress Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Radial Progress Demo'),
),
body: Center(
child: RadialProgressDemo(),
),
),
);
}
}
class RadialProgressDemo extends StatefulWidget {
@override
_RadialProgressDemoState createState() => _RadialProgressDemoState();
}
class _RadialProgressDemoState extends State<RadialProgressDemo> with SingleTickerProviderStateMixin {
double _progress = 0.0;
@override
void initState() {
super.initState();
// 模拟进度更新
Future.delayed(Duration(seconds: 2), () {
setState(() {
_progress = 0.5;
});
});
Future.delayed(Duration(seconds: 4), () {
setState(() {
_progress = 1.0;
});
});
}
@override
Widget build(BuildContext context) {
return RadialProgress(
radius: 100.0,
lineWidth: 10.0,
percentage: _progress,
child: Center(
child: Text(
'$_progress * 100%',
style: TextStyle(fontSize: 24, color: Colors.white),
),
),
backgroundColor: Colors.grey[300]!,
progressColor: Colors.blue,
center: true,
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个RadialProgress
小部件。以下是一些关键点的解释:
- Radius:定义进度条的半径。
- LineWidth:定义进度条的宽度。
- Percentage:定义当前的进度值(0.0到1.0之间)。
- Child:在进度条的中心显示的子小部件,这里我们显示当前的进度百分比。
- BackgroundColor:进度条的背景颜色。
- ProgressColor:进度条的颜色。
- Center:是否将子小部件居中显示。
运行这个应用,你会看到一个圆形进度条,它从0%开始,在2秒后更新到50%,然后在4秒后更新到100%。
希望这个示例能帮你更好地理解如何在Flutter中使用radial_progress
插件。如果你有其他问题或需要进一步的帮助,请随时告诉我!