Rules / Style / use-full-hex-values-for-flutter-colors

use-full-hex-values-for-flutter-colors

lint/style/use-full-hex-values-for-flutter-colors
recommendedflutter
Same as use_full_hex_values_for_flutter_colors · Dart lints

Flags a Color constructed from a hexadecimal literal with fewer than eight digits.

Flutter's Color takes a 32-bit 0xAARRGGBB value where the leading two digits are the alpha channel. A shorter literal such as 0xFFFFFF silently drops the alpha byte to 0x00, producing a fully transparent color rather than the opaque one the author almost certainly intended. Spelling out all eight digits makes the alpha channel explicit and the value unambiguous. The rule fires only on a Color(...) call with a single positional integer-literal argument in 0x/0X form whose hex-digit count (ignoring _ separators) is between one and seven.

Invalid

example.dartdart
// Bad: Color constructed with fewer than 8 hex digits.
import 'package:flutter/material.dart';

final c0 = Color(0xFFFFFF);

final c1 = Color(0x00FF00);

final c2 = Color(0xABC);

final c3 = Color(0Xff0000);

final c4 = Color(0xF);

final c5 = Color(0xFF00FF);
Use the full 8-digit hexadecimal value (0xAARRGGBB) for a Color.
3
4final c0 = Color(0xFFFFFF);
Use the full 8-digit hexadecimal value (0xAARRGGBB) for a Color.
5
6final c1 = Color(0x00FF00);
Use the full 8-digit hexadecimal value (0xAARRGGBB) for a Color.
7
8final c2 = Color(0xABC);
Use the full 8-digit hexadecimal value (0xAARRGGBB) for a Color.
9
10final c3 = Color(0Xff0000);
Use the full 8-digit hexadecimal value (0xAARRGGBB) for a Color.
11
12final c4 = Color(0xF);
Use the full 8-digit hexadecimal value (0xAARRGGBB) for a Color.
13
14final c5 = Color(0xFF00FF);

Valid

example.dartdart
// Good: full 8-digit hex colors, or not a bare Color() call.
import 'package:flutter/material.dart';

final c0 = Color(0xFFFFFFFF);

final c1 = Color(0x00FF00FF);

final c2 = Color(0xFF123456);

final c3 = Colors.red;

final c4 = Color.fromARGB(255, 0, 0, 0);

final c5 = Color(0xFFAABBCC);

How to configure

Set the severity of use-full-hex-values-for-flutter-colors in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "use-full-hex-values-for-flutter-colors": "error"
      }
    }
  }
}