Gradient Text in Flutter

后端 未结 2 1462
南旧
南旧 2021-02-03 10:58

I was wondering if it is possible to create a gradient over text Flutter. There is a gist of text gradient using Dart\'s ui, but it is kinda long and I was hoping to be simpler.

相关标签:
2条回答
  • 2021-02-03 11:18

    You can use ShaderMask for that. Remember to set the Text's color to white and you're good

    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,
            ),
          ),
        );
      }
    }
    

    Full example

    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,
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    

    0 讨论(0)
  • 2021-02-03 11:45

    Taken from here, you can use Text's style painter.

    Create the shader,

    final Shader linearGradient = LinearGradient(
      colors: <Color>[Color(0xffDA44bb), Color(0xff8921aa)],
    ).createShader(Rect.fromLTWH(0.0, 0.0, 200.0, 70.0));
    

    then use it in the TextStyle's foreground

      Text(
            'Hello Gradients!',
            style: new TextStyle(
                fontSize: 60.0,
                fontWeight: FontWeight.bold,
                foreground: Paint()..shader = linearGradient),
          )
    

    Source code

    0 讨论(0)
提交回复
热议问题