Skip to main content
Several Color properties used have been deprecated, the new properties return a different type and require conversion and value mapping
Source Link
President James K. Polk
  • 42.3k
  • 34
  • 113
  • 149

In Flutter, the Color classColor class only accepts integers as parametersintegers as parameters, or there is the possibility to use the named constructors fromARGB and fromRGBO.

So we only need to convert the string #b74093 to an integer value. Also we need to respect that opacityopacity always needs to be specified.
255 (full) opacity is represented by the hexadecimal value FF. This already leaves us with 0xFF0xFF. Now, we just need to append our color string like this:

extension HexColor on Color {
  /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  static Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
      '${a̶l̶p̶h̶a̶alpha.toRadixString(16).padLeft(2, '0')}'
      '${r̶e̶d̶red.toRadixString(16).padLeft(2, '0')}'
      '${̶g̶r̶e̶e̶n̶green.toRadixString(16).padLeft(2, '0')}'
      '${̶b̶l̶u̶e̶blue.toRadixString(16).padLeft(2, '0')}';
}

NOTE: After version 3.27 of flutterFlutter, the properties alpha, red, green, and blue are now deprecated. Check the changes on breaking-changes.

In Flutter, the Color class only accepts integers as parameters, or there is the possibility to use the named constructors fromARGB and fromRGBO.

So we only need to convert the string #b74093 to an integer value. Also we need to respect that opacity always needs to be specified.
255 (full) opacity is represented by the hexadecimal value FF. This already leaves us with 0xFF. Now, we just need to append our color string like this:

extension HexColor on Color {
  /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  static Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
      '${a̶l̶p̶h̶a̶.toRadixString(16).padLeft(2, '0')}'
      '${r̶e̶d̶.toRadixString(16).padLeft(2, '0')}'
      '${̶g̶r̶e̶e̶n̶.toRadixString(16).padLeft(2, '0')}'
      '${̶b̶l̶u̶e̶.toRadixString(16).padLeft(2, '0')}';
}

After version 3.27 of flutter the properties alpha, red, green and blue are now deprecated. Check the changes on breaking-changes.

In Flutter, the Color class only accepts integers as parameters, or there is the possibility to use the named constructors fromARGB and fromRGBO.

So we only need to convert the string #b74093 to an integer value. Also we need to respect that opacity always needs to be specified.
255 (full) opacity is represented by the hexadecimal value FF. This already leaves us with 0xFF. Now, we just need to append our color string like this:

extension HexColor on Color {
  /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  static Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
      '${alpha.toRadixString(16).padLeft(2, '0')}'
      '${red.toRadixString(16).padLeft(2, '0')}'
      '${green.toRadixString(16).padLeft(2, '0')}'
      '${blue.toRadixString(16).padLeft(2, '0')}';
}

NOTE: After version 3.27 of Flutter, the properties alpha, red, green, and blue are now deprecated. Check the changes on breaking-changes.

Several Color properties used have been deprecated, the new properties return a different type and require conversion and value mapping
Source Link
extension HexColor on Color {
  /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  static Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
      '${alphaa̶l̶p̶h̶a̶.toRadixString(16).padLeft(2, '0')}'
      '${redr̶e̶d̶.toRadixString(16).padLeft(2, '0')}'
      '${green̶g̶r̶e̶e̶n̶.toRadixString(16).padLeft(2, '0')}'
      '${blue̶b̶l̶u̶e̶.toRadixString(16).padLeft(2, '0')}';
}

After version 3.27 of flutter the properties alpha, red, green and blue are now deprecated. Check the changes on breaking-changes.

The new getters return a double value instead of an integer requiring a multiplication and a value casting.

extension HexColor on Color {
  /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  static Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
      '${(255 * a).toInt().toRadixString(16).padLeft(2, '0')}'
      '${(255 * r).toInt().toRadixString(16).padLeft(2, '0')}'
      '${(255 * g).toInt().toRadixString(16).padLeft(2, '0')}'
      '${(255 * b).toInt().toRadixString(16).padLeft(2, '0')}';
}
extension HexColor on Color {
  /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  static Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
      '${alpha.toRadixString(16).padLeft(2, '0')}'
      '${red.toRadixString(16).padLeft(2, '0')}'
      '${green.toRadixString(16).padLeft(2, '0')}'
      '${blue.toRadixString(16).padLeft(2, '0')}';
}
extension HexColor on Color {
  /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  static Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
      '${a̶l̶p̶h̶a̶.toRadixString(16).padLeft(2, '0')}'
      '${r̶e̶d̶.toRadixString(16).padLeft(2, '0')}'
      '${̶g̶r̶e̶e̶n̶.toRadixString(16).padLeft(2, '0')}'
      '${̶b̶l̶u̶e̶.toRadixString(16).padLeft(2, '0')}';
}

After version 3.27 of flutter the properties alpha, red, green and blue are now deprecated. Check the changes on breaking-changes.

The new getters return a double value instead of an integer requiring a multiplication and a value casting.

extension HexColor on Color {
  /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
  static Color fromHex(String hexString) {
    final buffer = StringBuffer();
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }

  /// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
  String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
      '${(255 * a).toInt().toRadixString(16).padLeft(2, '0')}'
      '${(255 * r).toInt().toRadixString(16).padLeft(2, '0')}'
      '${(255 * g).toInt().toRadixString(16).padLeft(2, '0')}'
      '${(255 * b).toInt().toRadixString(16).padLeft(2, '0')}';
}
fixed broken links
Source Link
Md. Yeasin Sheikh
  • 64.8k
  • 7
  • 50
  • 86

In Flutter, the Color classColor class only accepts integers as parameters, or there is the possibility to use the named constructors fromARGBfromARGB and fromRGBOfromRGBO.

In Flutter, the Color class only accepts integers as parameters, or there is the possibility to use the named constructors fromARGB and fromRGBO.

In Flutter, the Color class only accepts integers as parameters, or there is the possibility to use the named constructors fromARGB and fromRGBO.

added 1 character in body
Source Link
creativecreatorormaybenot
  • 128.2k
  • 66
  • 318
  • 428
Loading
added 304 characters in body
Source Link
creativecreatorormaybenot
  • 128.2k
  • 66
  • 318
  • 428
Loading
added 64 characters in body
Source Link
creativecreatorormaybenot
  • 128.2k
  • 66
  • 318
  • 428
Loading
added 48 characters in body
Source Link
creativecreatorormaybenot
  • 128.2k
  • 66
  • 318
  • 428
Loading
added 1641 characters in body
Source Link
creativecreatorormaybenot
  • 128.2k
  • 66
  • 318
  • 428
Loading
added 38 characters in body
Source Link
creativecreatorormaybenot
  • 128.2k
  • 66
  • 318
  • 428
Loading
added 156 characters in body
Source Link
creativecreatorormaybenot
  • 128.2k
  • 66
  • 318
  • 428
Loading
Source Link
creativecreatorormaybenot
  • 128.2k
  • 66
  • 318
  • 428
Loading