2026-04-09 21:32:23 +08:00

90 lines
2.3 KiB
Dart

import 'package:flutter/material.dart';
class SCShiningText extends StatefulWidget {
final String text;
final double fontSize;
final FontWeight fontWeight;
final Color baseColor;
final Color shineColor;
const SCShiningText({
super.key,
required this.text,
this.fontSize = 24,
this.fontWeight = FontWeight.bold,
this.baseColor = Colors.grey,
this.shineColor = Colors.white,
});
@override
State<SCShiningText> createState() => _ShiningText1State();
}
class _ShiningText1State extends State<SCShiningText>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 3000),
vsync: this,
)..repeat(reverse: false);
_animation = Tween<double>(begin: -1.0, end: 2.0).animate(_controller);
}
@override
Widget build(BuildContext context) {
return Center(
child: AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return ShaderMask(
blendMode: BlendMode.srcIn,
shaderCallback: (Rect bounds) {
return LinearGradient(
colors: [
widget.baseColor,
widget.shineColor,
widget.shineColor,
widget.baseColor,
],
stops: const [0.0, 0.1, 0.9, 1.0],
transform: _GradientTransform(_animation.value),
).createShader(bounds);
},
child: Text(
widget.text,
style: TextStyle(
fontSize: widget.fontSize,
fontWeight: widget.fontWeight,
),
),
);
},
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
class _GradientTransform extends GradientTransform {
final double value;
const _GradientTransform(this.value);
@override
Matrix4 transform(Rect bounds, {TextDirection? textDirection}) {
return Matrix4.translationValues(bounds.width * value, 0.0, 0.0);
}
}