在Flutter中实现进度条动画,可以使用内置组件或自定义动画控制器。以下是两种常用方法:
1. 使用内置LinearProgressIndicator(简单动画)
LinearProgressIndicator(
  value: _progress, // 0.0 ~ 1.0
  backgroundColor: Colors.grey,
  valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
)
2. 自定义动画控制器(高级动画)
class AnimatedProgressBar extends StatefulWidget {
  @override
  _AnimatedProgressBarState createState() => _AnimatedProgressBarState();
}
class _AnimatedProgressBarState extends State<AnimatedProgressBar>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation<double> _animation;
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: Duration(seconds: 2),
      vsync: this,
    );
    _animation = Tween<double>(begin: 0, end: 1).animate(_controller)
      ..addListener(() => setState(() {}));
    _controller.forward();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 20,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey),
        borderRadius: BorderRadius.circular(10),
      ),
      child: Stack(
        children: [
          Container(
            width: MediaQuery.of(context).size.width * _animation.value,
            decoration: BoxDecoration(
              gradient: LinearGradient(colors: [Colors.blue, Colors.green]),
              borderRadius: BorderRadius.circular(10),
            ),
          )
        ],
      ),
    );
  }
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
关键点:
- 使用
AnimationController控制动画时长 
- 通过
Tween定义数值变化范围 
addListener触发重绘更新UI 
- 使用
forward()启动动画 
扩展建议:
- 添加动画曲线(Curves)
 
- 实现暂停/继续功能
 
- 支持自定义颜色和样式
 
- 添加进度文本显示
 
内置组件适合简单场景,自定义控制器可实现更复杂的动画效果。