Skip to content

Commit

Permalink
Fixed the fact that Google hated value from Color for some reason
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeiffer committed Dec 12, 2024
1 parent 5d2a2c2 commit c1332b4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions json_theme/lib/json_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
7 changes: 3 additions & 4 deletions json_theme/lib/src/codec/theme_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -6939,11 +6939,10 @@ class ThemeDecoder {
);

result = MaterialColor(
decodeColor(
colorToInt(decodeColor(
value['primary'],
validate: false,
)!
.value,
)!)!,
swatches,
);
}
Expand Down
3 changes: 2 additions & 1 deletion json_theme/lib/src/codec/theme_encoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3857,7 +3858,7 @@ class ThemeEncoder {

if (value != null) {
result = <String, dynamic>{
'primary': encodeColor(Color(value.value)),
'primary': encodeColor(Color(colorToInt(value)!)),
'swatches': {
'50': encodeColor(value.shade50),
'100': encodeColor(value.shade100),
Expand Down
15 changes: 15 additions & 0 deletions json_theme/lib/src/utils/color_to_int.dart
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion json_theme/test/json_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c1332b4

Please sign in to comment.