cardFieldText function

String? cardFieldText(
  1. ResourceRecord record,
  2. BuildContext context,
  3. String? field, {
  4. bool formatDates = false,
  5. bool isolate = true,
})

The field-reading/formatting logic shared by ResourceCard and ResourceRow — extracted rather than duplicated once a second widget needed the exact same title/subtitle/badge/meta rendering rules (P23 Task 2).

formatDates gates the ISO-8601 → localised-short-date rewrite — only the meta caller sets it, preserving the pre-P6f behaviour (a title/subtitle/ badge holding a timestamp-shaped string prints it raw). context is unconditional regardless of that flag: every returned string still needs it to resolve the ambient Directionality for isolateBidi.

isolate defaults to true and is set false only by the badge caller: a badge's value is about to become SemanticBadge's colour-lookup key, and isolating it here first would isolate the key before the lookup runs, breaking colors[value] for any value that matches the grouped-digit pattern (fix round 1, finding 1). SemanticBadge isolates its own displayed text after doing that lookup on the raw value.

Implementation

String? cardFieldText(
  ResourceRecord record,
  BuildContext context,
  String? field, {
  bool formatDates = false,
  bool isolate = true,
}) {
  if (field == null) return null;

  String raw;

  // A rich column publishes its plain text on a flat sibling key,
  // `<field>.__rich.text` (design spec, "Cards") — read that instead of the
  // raw markup the base field still holds. Absent sibling means nothing to
  // convert, and falls through to the raw value below like any other field.
  if (record.get<Map<String, dynamic>>('$field.__rich') case {
    'text': final String text,
  }) {
    raw = text;
  } else {
    final value = record.get<Object>(field);

    if (value == null) return null;

    // A timestamp arrives as raw ISO 8601 — printing it verbatim puts LTR
    // digits and a `T` in the middle of an RTL card. `MaterialLocalizations`
    // formats it in the app's own locale with no new dependency.
    final date = formatDates ? _asDate(value) : null;

    raw = date != null
        ? MaterialLocalizations.of(context).formatShortDate(date)
        : value.toString();
  }

  if (!isolate) return raw;

  // Grouped digits (a phone number, a spaced IBAN, a hyphenated tax number)
  // reverse inside an RTL card otherwise — see `bidi_text.dart`.
  return isolateBidi(raw, Directionality.of(context));
}