From 4f92082c2646494afeddf182c10c99ced6127571 Mon Sep 17 00:00:00 2001 From: Nikita Kupriyanov Date: Tue, 25 Aug 2026 02:05:49 +0400 Subject: [PATCH] fix: serialize custom asset colors as RGBA --- .github/workflows/ci.yml | 3 ++ .../adaptyui_custom_assets_color.dart | 8 +++- ...custom_asset_color_serialization_test.dart | 37 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/adapty_custom_asset_color_serialization_test.dart diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0f2842c..37c56b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,3 +37,6 @@ jobs: - name: Analyze root package run: flutter analyze + + - name: Test root package + run: flutter test diff --git a/lib/src/models/custom_assets/adaptyui_custom_assets_color.dart b/lib/src/models/custom_assets/adaptyui_custom_assets_color.dart index c538dd8..ed18411 100644 --- a/lib/src/models/custom_assets/adaptyui_custom_assets_color.dart +++ b/lib/src/models/custom_assets/adaptyui_custom_assets_color.dart @@ -1,7 +1,13 @@ part of 'adaptyui_custom_assets.dart'; extension on Color { - String get stringHexValue => '#${toARGB32().toRadixString(16).padLeft(8, '0').toUpperCase()}'; + /// Converts Flutter's ARGB value to the RGBA wire format expected by AdaptyUI. + String get stringHexValue { + final argb = toARGB32(); + final rgba = ((argb & 0x00FFFFFF) << 8) | ((argb >> 24) & 0xFF); + + return '#${rgba.toRadixString(16).padLeft(8, '0').toUpperCase()}'; + } } final class AdaptyCustomAssetColor extends AdaptyCustomAsset { diff --git a/test/adapty_custom_asset_color_serialization_test.dart b/test/adapty_custom_asset_color_serialization_test.dart new file mode 100644 index 0000000..269c4c0 --- /dev/null +++ b/test/adapty_custom_asset_color_serialization_test.dart @@ -0,0 +1,37 @@ +import 'package:adapty_flutter/adapty_flutter.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + group('AdaptyCustomAsset color serialization', () { + test('serializes solid colors as #RRGGBBAA', () { + final cases = { + const Color(0xFF0000FF): '#0000FFFF', + const Color(0x80010203): '#01020380', + }; + + for (final MapEntry(key: color, value: expected) in cases.entries) { + final asset = AdaptyCustomAsset.color(color: color); + + expect(asset.jsonValue['value'], expected); + } + }); + + test('serializes linear gradient stop colors as #RRGGBBAA', () { + final asset = AdaptyCustomAsset.linearGradient( + gradient: const LinearGradient( + colors: [Color(0xFF0000FF), Color(0x80010203)], + stops: [0.25, 0.75], + ), + ); + + final values = asset.jsonValue['values'] as List; + final serializedColors = values + .cast>() + .map((value) => value['color']) + .toList(); + + expect(serializedColors, ['#0000FFFF', '#01020380']); + }); + }); +}