Nea*_*arl 43
你可以用ShaderMask它。记住将Text的颜色设置为白色,你就很好
import 'package:flutter/material.dart';
class GradientText extends StatelessWidget {
GradientText(
this.text, {
@required this.gradient,
});
final String text;
final Gradient gradient;
@override
Widget build(BuildContext context) {
return ShaderMask(
shaderCallback: (bounds) => gradient.createShader(
Rect.fromLTWH(0, 0, bounds.width, bounds.height),
),
child: Text(
text,
style: TextStyle(
// The color must be set to white for this to work
color: Colors.white,
fontSize: 40,
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
class GradientTextWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox.expand(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GradientText(
'Hello Flutter',
gradient: LinearGradient(colors: [
Colors.blue.shade400,
Colors.blue.shade900,
]),
),
GradientText(
'Rainbow',
gradient: LinearGradient(colors: [
Colors.red,
Colors.pink,
Colors.purple,
Colors.deepPurple,
Colors.deepPurple,
Colors.indigo,
Colors.blue,
Colors.lightBlue,
Colors.cyan,
Colors.teal,
Colors.green,
Colors.lightGreen,
Colors.lime,
Colors.yellow,
Colors.amber,
Colors.orange,
Colors.deepOrange,
]),
),
GradientText(
'Fade out',
gradient: LinearGradient(
colors: [
Colors.black,
Colors.white,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
Abd*_*ؤمن 10
从这里开始,您可以使用Text的样式绘制器。
创建着色器,
final Shader linearGradient = LinearGradient(
colors: <Color>[Color(0xffDA44bb), Color(0xff8921aa)],
).createShader(Rect.fromLTWH(0.0, 0.0, 200.0, 70.0));
Run Code Online (Sandbox Code Playgroud)
然后在TextStyle的前景中使用它
Text(
'Hello Gradients!',
style: new TextStyle(
fontSize: 60.0,
fontWeight: FontWeight.bold,
foreground: Paint()..shader = linearGradient),
)
Run Code Online (Sandbox Code Playgroud)
Ale*_*lex 10
使用simple_gradient_text包并创建GradienText
GradientText(
'Gradient Text Example',
style: TextStyle(
fontSize: 40.0,
),
colors: [
Colors.blue,
Colors.red,
Colors.teal,
],
),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2001 次 |
| 最近记录: |