7.7 C
New York
Saturday, December 2, 2023

Flutter Gradient Text Examples

Below are a few examples of creating gradient color text in Flutter without using any third-party plugins.

Example 1: Linear Gradient Text (with Shader)

Screenshot:

The code:

class HomePage extends StatelessWidget {
  final Shader _linearGradient = LinearGradient(
    colors: [Colors.yellow, Colors.deepPurple],
    begin: Alignment.centerLeft,
    end: Alignment.bottomRight,
  ).createShader(Rect.fromLTWH(0.0, 0.0, 320.0, 80.0)); // Creates a Shader for this gradient to fill the given rect
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Kindacode.com'),
      ),
      body: Center(
        child: Text(
          'KINDACODE',
          style: TextStyle(
              fontSize: 50,
              fontStyle: FontStyle.italic,
              fontWeight: FontWeight.bold,
              foreground: Paint()..shader = _linearGradient),
        ),
      ),
    );
  }
}

You can find more information about the Shader class in the official docs.

Example 2: Linear Gradient Text (with ShaderMask)

Screenshot:

The code:

class HomePage extends StatelessWidget {
  final Gradient _gradient = LinearGradient(
    colors: [Colors.yellow, Colors.deepPurple],
    begin: Alignment.topLeft,
    end: Alignment.bottomRight,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Kindacode.com'),
      ),
      body: Center(
        child: ShaderMask(
          blendMode: BlendMode.modulate,
          shaderCallback: (size) => _gradient.createShader(
            Rect.fromLTWH(0, 0, size.width, size.height),
          ),
          child: Text(
            'ABCDEFGH',
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.bold,
              fontSize: 60,
            ),
          ),
        ),
      ),
    );
  }
}

You can see the details about ShaderMaks here.

Example 3: Radial Gradient Text (with ShaderMask)

Screenshot:

Advertisements

The code:

class HomePage extends StatelessWidget {
  final Gradient _gradient = RadialGradient(
    colors: [
      Colors.yellow,
      Colors.deepPurple,
      Colors.red,
      Colors.amber,
      Colors.blue
    ],
    center: Alignment.center,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Kindacode.com'),
      ),
      body: Center(
        child: ShaderMask(
          blendMode: BlendMode.modulate,
          shaderCallback: (size) => _gradient.createShader(
            Rect.fromLTWH(0, 0, size.width, size.height),
          ),
          child: Text(
            'A',
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.bold,
              fontSize: 300,
            ),
          ),
        ),
      ),
    );
  }
}

Wrapping Up

WeÂ’ve gone through some examples of making beautiful gradient text in Flutter. Continue learning and exploring more by taking a look at the following:

You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.

Advertisements

Related Articles

Latest Articles