From c1332b43f3dc29aa5cc18681dc3ead2b633f2c45 Mon Sep 17 00:00:00 2001 From: Jeff Peiffer Date: Thu, 12 Dec 2024 03:10:14 -0500 Subject: [PATCH] Fixed the fact that Google hated value from Color for some reason --- json_theme/lib/json_theme.dart | 1 + json_theme/lib/src/codec/theme_decoder.dart | 7 +++---- json_theme/lib/src/codec/theme_encoder.dart | 3 ++- json_theme/lib/src/utils/color_to_int.dart | 15 +++++++++++++++ json_theme/test/json_theme_test.dart | 2 +- 5 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 json_theme/lib/src/utils/color_to_int.dart diff --git a/json_theme/lib/json_theme.dart b/json_theme/lib/json_theme.dart index a26903b..0323ee8 100644 --- a/json_theme/lib/json_theme.dart +++ b/json_theme/lib/json_theme.dart @@ -3,3 +3,4 @@ export 'json_theme_schemas.dart'; export 'src/codec/theme_decoder.dart'; export 'src/codec/theme_encoder.dart'; export 'src/model/json_widget_state_property.dart'; +export 'src/utils/color_to_int.dart'; diff --git a/json_theme/lib/src/codec/theme_decoder.dart b/json_theme/lib/src/codec/theme_decoder.dart index cc4f536..9f989cd 100644 --- a/json_theme/lib/src/codec/theme_decoder.dart +++ b/json_theme/lib/src/codec/theme_decoder.dart @@ -8,10 +8,10 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:json_class/json_class.dart'; +import 'package:json_theme/json_theme.dart'; import 'package:json_theme_annotation/json_theme_annotation.dart'; import '../model/map_widget_state_property.dart'; -import '../schema/schema_validator.dart'; /// Decoder capable of converting JSON compatible values into Flutter Theme /// related classes and enums. @@ -6939,11 +6939,10 @@ class ThemeDecoder { ); result = MaterialColor( - decodeColor( + colorToInt(decodeColor( value['primary'], validate: false, - )! - .value, + )!)!, swatches, ); } diff --git a/json_theme/lib/src/codec/theme_encoder.dart b/json_theme/lib/src/codec/theme_encoder.dart index aadc992..85837be 100644 --- a/json_theme/lib/src/codec/theme_encoder.dart +++ b/json_theme/lib/src/codec/theme_encoder.dart @@ -6,6 +6,7 @@ import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; +import 'package:json_theme/json_theme.dart'; import 'package:json_theme_annotation/json_theme_annotation.dart'; /// Encoder capable of converting Flutter Theme related classes and enums into @@ -3857,7 +3858,7 @@ class ThemeEncoder { if (value != null) { result = { - 'primary': encodeColor(Color(value.value)), + 'primary': encodeColor(Color(colorToInt(value)!)), 'swatches': { '50': encodeColor(value.shade50), '100': encodeColor(value.shade100), diff --git a/json_theme/lib/src/utils/color_to_int.dart b/json_theme/lib/src/utils/color_to_int.dart new file mode 100644 index 0000000..f5f8048 --- /dev/null +++ b/json_theme/lib/src/utils/color_to_int.dart @@ -0,0 +1,15 @@ +import 'package:flutter/material.dart'; + +int? colorToInt(Color? color) { + int? result; + if (color != null) { + final hex = (color.a * 255).toInt().toRadixString(16).padLeft(2, '0') + + (color.r * 255).toInt().toRadixString(16).padLeft(2, '0') + + (color.g * 255).toInt().toRadixString(16).padLeft(2, '0') + + (color.b * 255).toInt().toRadixString(16).padLeft(2, '0'); + + return int.parse(hex, radix: 16); + } + + return result; +} diff --git a/json_theme/test/json_theme_test.dart b/json_theme/test/json_theme_test.dart index 25854fb..4c0c721 100644 --- a/json_theme/test/json_theme_test.dart +++ b/json_theme/test/json_theme_test.dart @@ -5482,7 +5482,7 @@ void main() { expect(ThemeDecoder.decodeMaterialColor(null), null); expect(ThemeEncoder.encodeMaterialColor(null), null); - final entry = MaterialColor(_kColor.value, const { + final entry = MaterialColor(colorToInt(_kColor)!, const { 50: _kColor, 100: _kColor, 200: _kColor,