zaro

How to Use Color in Flutter?

Published in Flutter Colors 5 mins read

In Flutter, you primarily use the Color class and the Colors class to define and apply visual hues throughout your application, enabling rich and dynamic user interfaces.

Understanding Colors in Flutter

Flutter's rendering engine uses a 32-bit integer to represent colors, which aligns with the ARGB (Alpha, Red, Green, Blue) format. This format allows you to specify not only the red, green, and blue components of a color but also its opacity (alpha channel).

Defining Colors

Flutter provides several straightforward ways to define colors, catering to different needs from predefined constants to custom values.

Predefined Material Colors

The Colors class offers a vast palette of predefined colors inspired by the Material Design specification. These are incredibly convenient for consistent design.

  • Syntax: Colors.colorName
  • Variations: Most colors also have a range of shades (from 50 to 900, plus shade100, shade200, etc.) for more granular control.

Examples:

  • Colors.blue (the primary blue shade)
  • Colors.green[700] (a darker shade of green)
  • Colors.amberAccent (an accent color)
// Example of predefined colors
Container(
  color: Colors.deepPurple, // Solid color
  child: Text(
    'Hello Flutter!',
    style: TextStyle(color: Colors.white),
  ),
);

Custom Hexadecimal Colors

For precise custom colors, especially when working with design mockups that provide hex codes, you can define colors using hexadecimal values. In Flutter, you can define colors using hexadecimal values by utilizing the Color class constructor. The constructor accepts a 32-bit integer value representing the color in ARGB format, where A stands for alpha, R for red, G for green, and B for blue.

The hexadecimal format typically looks like 0xAARRGGBB, where:

  • AA: Alpha channel (opacity), from 00 (fully transparent) to FF (fully opaque).
  • RR: Red component, from 00 to FF.
  • GG: Green component, from 00 to FF.
  • BB: Blue component, from 00 to FF.

Common Alpha Values:

Hex Value Opacity
0xFF 100%
0xCC 80%
0x80 50%
0x00 0%

Example:

A common practice is to always use 0xFF for full opacity.

// Example of a custom hexadecimal color (red with 100% opacity)
Color customRed = Color(0xFFFF0000); // ARGB: Full Alpha, Full Red, No Green, No Blue

Container(
  color: customRed,
  child: Text(
    'Custom Color',
    style: TextStyle(color: Color(0xFFFFFFFF)), // White color
  ),
);

RGB/RGBA Colors

If you prefer to work with RGB or RGBA values (where R, G, B are integers from 0 to 255, and A is a double from 0.0 to 1.0), the Color.fromRGBO constructor is your go-to.

  • Syntax: Color.fromRGBO(red, green, blue, opacity)

Example:

// Example of RGB/RGBA color
Color semiTransparentBlue = Color.fromRGBO(0, 0, 255, 0.5); // Blue with 50% opacity

Container(
  color: semiTransparentBlue,
  child: Text(
    'Semi-transparent',
    style: TextStyle(color: Colors.white),
  ),
);

Applying Colors to Widgets

Once defined, colors can be applied to various widget properties that accept a Color type.

Common Widget Properties

Most visual widgets have properties to set their background, text, or icon colors:

  • Container: Use the color property directly for a solid background, or within decoration for more complex styles like BoxDecoration.
    Container(
      color: Colors.teal,
      // Or:
      // decoration: BoxDecoration(
      //   color: Colors.teal,
      // ),
    )
  • Text: Set color within the TextStyle property.
    Text(
      'Styled Text',
      style: TextStyle(color: Colors.purple, fontSize: 20.0),
    )
  • Icon: Directly set the color property.
    Icon(
      Icons.star,
      color: Colors.yellow[700],
      size: 30.0,
    )
  • AppBar: Use the backgroundColor property.
    AppBar(
      title: Text('My App'),
      backgroundColor: Colors.blueGrey[800],
    )

Theming Your Application

For a consistent look across your entire application, Flutter's theming system is powerful. You define a ThemeData object at the root of your app (often in MaterialApp) which then propagates colors to child widgets.

  • primaryColor: The dominant color of your app.
  • accentColor (deprecated in favor of ColorScheme): A secondary color for interactive elements.
  • colorScheme: The recommended way to define a comprehensive set of colors for your app, covering primary, secondary, surface, background, error colors, and their "on" counterparts (e.g., onPrimary for text/icons on a primary background).
MaterialApp(
  theme: ThemeData(
    primaryColor: Colors.indigo,
    colorScheme: ColorScheme.fromSeed(
      seedColor: Colors.indigo,
      primary: Colors.indigo,
      secondary: Colors.pinkAccent,
    ),
    appBarTheme: AppBarTheme(
      backgroundColor: Colors.indigo[700],
      foregroundColor: Colors.white, // Color of text/icons on AppBar
    ),
  ),
  home: MyHomePage(),
);

Manipulating Colors

Flutter's Color class provides methods for easy manipulation:

Adjusting Opacity

The withOpacity() method creates a new color with the specified opacity, while retaining the original RGB values.

Color originalColor = Colors.blue;
Color semiTransparentBlue = originalColor.withOpacity(0.5); // 50% opaque

Container(
  color: semiTransparentBlue,
  child: Text('Faded Blue'),
);

Shading Material Colors

For MaterialColor objects (like Colors.blue), you can access specific shades directly using bracket notation or the shade method.

Color darkerBlue = Colors.blue[800]; // Shorthand for Colors.blue.shade800

Best Practices for Color Usage

  1. Use Theming: Define your primary colors in ThemeData and use Theme.of(context).primaryColor (or Theme.of(context).colorScheme.primary) to access them throughout your app. This ensures consistency and makes global color changes effortless.
  2. Accessibility: Ensure sufficient contrast between text and background colors for readability, especially for users with visual impairments. Tools like WebAIM Contrast Checker can help.
  3. Semantic Naming: When creating custom color constants, use names that describe their purpose (e.g., kPrimaryColor, kErrorColor) rather than their literal hue (e.g., kRed, kBlue).
  4. Limited Palette: Stick to a limited and cohesive color palette to maintain a clean and professional design.
  5. Use Color.fromRGBO or Hex for Custom: Opt for Color.fromRGBO when dealing with dynamic alpha values or Color(0xAARRGGBB) for static custom hex codes.