custom_progress_bar.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import 'package:flutter/material.dart';
  2. class CustomProgressBar extends StatelessWidget {
  3. final double progress; // 进度值,范围 [0, 1]
  4. final Color color; // 进度条的颜色
  5. final double radius; // 进度条的圆角
  6. const CustomProgressBar({
  7. Key? key,
  8. required this.progress,
  9. required this.color,
  10. this.radius = 0,
  11. }) : assert(progress >= 0 && progress <= 1, '进度值必须在 0 到 1 之间'),
  12. super(key: key);
  13. @override
  14. Widget build(BuildContext context) {
  15. return Container(
  16. width: double.infinity, // 进度条宽度
  17. height: 16, // 进度条高度
  18. decoration: BoxDecoration(
  19. border: Border.all(color: color, width: 0.5), // 边框颜色和宽度
  20. borderRadius: BorderRadius.circular(radius), // 边框圆角
  21. ),
  22. child: ClipRRect(
  23. borderRadius: BorderRadius.circular(radius), // 内部进度条也有相同的圆角
  24. child: LinearProgressIndicator(
  25. value: progress, // 进度值
  26. backgroundColor: Colors.transparent, // 背景透明
  27. valueColor: AlwaysStoppedAnimation<Color>(color), // 进度条颜色
  28. ),
  29. ),
  30. );
  31. }
  32. }