How to create Flutter gradient from one color

A LinearGradient is a class in the Flutter framework that creates a gradient, which is a gradual blend of two or more colors that create a transition between them in a straight line. It can be used to create a variety of visual effects in Flutter applications, such as backgrounds, text colors, and foregrounds.

import 'package:flutter/material.dart';

class GradientGenerator {
  static LinearGradient generateGradient(Color color) {
    return LinearGradient(
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      colors: [
        color.withOpacity(0.3),
        color.withOpacity(0.5),
        color.withOpacity(0.7),
        color.withOpacity(0.9),
        color,
      ],
      stops: [0.1, 0.3, 0.5, 0.7, 1.0],
    );
  }
}

This code defines a static method called generateGradient which takes a Color as an argument and returns a LinearGradient. The LinearGradient is created with the colors property set to an array of five colors, each with a decreasing opacity level. The stops property is set to an array of values that correspond to each color in the colors array.

What is LinearGradient? How to use?

To use this generator, simply call the generateGradient method with a Color as an argument:

Color myColor = Colors.blue;
LinearGradient myGradient = GradientGenerator.generateGradient(myColor);

This will generate a gradient that fades from the specified color to transparent. You can customize the gradient by adjusting the opacity levels and the Alignment values for the begin and end properties.

Related posts:

  1. Using url_launcher in Your Flutter App
  2. Advanced sqflite Techniques in Flutter: Batch, Migrations, Transactions, Debugging
  3. How to use cupertino_icons?