Gradient Text Effect in Flutter Without Package

Thu, May 18, 2023

Read in 3 minutes

In this Flutter tutorial, we will explore how to create a gradient text effect using the ShaderMask widget. We'll build a sample HomeScreen widget that displays a gradient text with a shadow effect. Let's dive into the code step by step.

Step 1: Defining the HomeScreen Widget To begin, we define a stateless widget called HomeScreen. This widget will serve as the main screen of our application.

class HomeScreen extends StatelessWidget {
  // ...
}

Step 2: Initializing Variables Inside the HomeScreen class, we initialize two variables: gradient and textStyle. These variables will be used to define the properties of the gradient and text style applied to the text widget.

final gradient = LinearGradient(
  begin: Alignment.topCenter,
  end: Alignment.centerLeft,
  colors: [
    Colors.blueAccent,
    Colors.redAccent,
    Colors.amber,
  ],
);

final textStyle = TextStyle(
  fontSize: 50,
  fontWeight: FontWeight.bold,
  shadows: [
    BoxShadow(
      color: Colors.white,
      spreadRadius: 5,
      offset: Offset(1, 1),
    ),
  ],
);

Step 3: Building the Widget Tree In the build method of the HomeScreen widget, we construct the widget tree for our screen.

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: ShaderMask(
        shaderCallback: (bounds) {
          return gradient.createShader(
            Rect.fromLTWH(0, 0, bounds.width, bounds.height),
          );
        },
        child: Text(
          "DO NOT\nUSE\nPACKAGE\nFOR THIS",
          style: textStyle,
        ),
      ),
    ),
  );
}

Step 4: Running the App To see the gradient text effect in action, we need to run the app. Ensure that you have set up the Flutter development environment correctly and can run Flutter applications.

Conclusion: In this tutorial, we learned how to create a gradient text effect using the ShaderMask widget in Flutter. We explored the step-by-step implementation of a HomeScreen widget that displays a gradient text with a shadow effect. You can customize the gradient colors, text style, and shader bounds to create various unique and visually appealing text effects in your Flutter applications.


Shohruh AK





See Also

Flutter Login Screen UI tutorial
How to create animated list view in Flutter without any package: AnimatedList widget
Flutter: Copy ZIP folder with all subfolders from assets to application documents directory
How to create animated loading button in Flutter
How to publish Flutter app on Play Store: Beginner guide