encodeStyle function

Map<String, dynamic> encodeStyle(
  1. BuildContext context, {
  2. Color? tint,
  3. Color? thumbTint,
  4. Color? trackTint,
  5. Color? trackBackgroundTint,
})

Creates a unified style map for platform views.

This function encodes color styling information into a single map that can be passed to native iOS/macOS code via method channels. All colors are resolved and converted to ARGB format for compatibility.

Parameters

  • context: Build context for resolving dynamic colors
  • tint: General accent/tint color (primary color)
  • thumbTint: Slider thumb or switch knob color
  • trackTint: Active track color (slider/switch)
  • trackBackgroundTint: Inactive track background color

Returns

A map with string keys and ARGB integer values. Only non-null colors are included in the map. Keys:

  • 'tint': General accent color
  • 'thumbTint': Slider/switch thumb color
  • 'trackTint': Active track color
  • 'trackBackgroundTint': Inactive track color

Example

final style = encodeStyle(
  context,
  tint: CupertinoColors.systemBlue,
  thumbTint: CupertinoColors.systemPink,
  trackTint: CupertinoColors.systemGreen,
);
// Returns: {
//   'tint': 0xFF007AFF,
//   'thumbTint': 0xFFFF2D55,
//   'trackTint': 0xFF34C759,
// }

Implementation

Map<String, dynamic> encodeStyle(
  BuildContext context, {
  Color? tint,
  Color? thumbTint,
  Color? trackTint,
  Color? trackBackgroundTint,
}) {
  final style = <String, dynamic>{};
  final tintInt = resolveColorToArgb(tint, context);
  final thumbInt = resolveColorToArgb(thumbTint, context);
  final trackInt = resolveColorToArgb(trackTint, context);
  final trackBgInt = resolveColorToArgb(trackBackgroundTint, context);
  if (tintInt != null) style['tint'] = tintInt;
  if (thumbInt != null) style['thumbTint'] = thumbInt;
  if (trackInt != null) style['trackTint'] = trackInt;
  if (trackBgInt != null) style['trackBackgroundTint'] = trackBgInt;
  return style;
}