3 changed files with 258 additions and 180 deletions
@ -0,0 +1,65 @@ |
|||
import 'package:flutter/material.dart'; |
|||
|
|||
class AnimatedGradientBackground extends StatefulWidget { |
|||
const AnimatedGradientBackground({super.key}); |
|||
|
|||
@override |
|||
State<AnimatedGradientBackground> createState() => |
|||
_AnimatedGradientBackgroundState(); |
|||
} |
|||
|
|||
class _AnimatedGradientBackgroundState extends State<AnimatedGradientBackground> |
|||
with SingleTickerProviderStateMixin { |
|||
late AnimationController _controller; |
|||
late Animation<Alignment?> _beginAnimation; |
|||
late Animation<Alignment?> _endAnimation; |
|||
|
|||
final Color color1 = const Color(0xFFfbcfdd); |
|||
final Color color2 = const Color(0xFFe2e9fb); |
|||
|
|||
@override |
|||
void initState() { |
|||
super.initState(); |
|||
|
|||
_controller = AnimationController( |
|||
vsync: this, |
|||
duration: const Duration(seconds: 10), |
|||
)..repeat(reverse: true); |
|||
|
|||
// 创建起始点动画 |
|||
_beginAnimation = AlignmentTween( |
|||
begin: Alignment.topCenter, |
|||
end: Alignment.bottomLeft, |
|||
).animate(_controller); |
|||
|
|||
// 创建结束点动画 |
|||
_endAnimation = AlignmentTween( |
|||
begin: Alignment.bottomCenter, |
|||
end: Alignment.topRight, |
|||
).animate(_controller); |
|||
} |
|||
|
|||
@override |
|||
void dispose() { |
|||
_controller.dispose(); |
|||
super.dispose(); |
|||
} |
|||
|
|||
@override |
|||
Widget build(BuildContext context) { |
|||
return AnimatedBuilder( |
|||
animation: _controller, |
|||
builder: (context, child) { |
|||
return Container( |
|||
decoration: BoxDecoration( |
|||
gradient: LinearGradient( |
|||
colors: [color1, color2], |
|||
begin: _beginAnimation.value!, |
|||
end: _endAnimation.value!, |
|||
), |
|||
), |
|||
); |
|||
}, |
|||
); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue