You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.7 KiB
69 lines
1.7 KiB
import 'package:flutter/material.dart';
|
|
|
|
class AnimatedGradientBackground extends StatefulWidget {
|
|
final Widget child;
|
|
|
|
const AnimatedGradientBackground(this.child, {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!,
|
|
),
|
|
),
|
|
child: child,
|
|
);
|
|
},
|
|
child: widget.child,
|
|
);
|
|
}
|
|
}
|
|
|