ledgerSubLine function

Widget? ledgerSubLine(
  1. BuildContext context, {
  2. String? start,
  3. String? end,
  4. bool startMono = false,
  5. LedgerTone startTone = LedgerTone.plain,
  6. LedgerTone endTone = LedgerTone.plain,
})

A row's sub line (.sub, 13 on 18 in the third ink): its start words and its end words at the far side, 12 apart at least; null with neither. Plumbing of the rows.

Implementation

Widget? ledgerSubLine(
  BuildContext context, {
  String? start,
  String? end,
  bool startMono = false,
  LedgerTone startTone = LedgerTone.plain,
  LedgerTone endTone = LedgerTone.plain,
}) {
  if ((start == null || start.isEmpty) && (end == null || end.isEmpty)) {
    return null;
  }
  final t = context.ledgerTheme;
  Color ink(LedgerTone tone) => switch (tone) {
    LedgerTone.moneyIn => t.moneyIn,
    LedgerTone.over => t.over,
    _ => t.textTertiary,
  };
  final base = t.bodySmall;
  final strong = base.medium.colored(t.textSecondary);
  // `.sub .mono`: the mono at 12, untracked, on the line's 18.
  final mono = t.amount
      .sized(_mono)
      .tracked(0)
      .line(base.fontSize! * base.height!)
      .copyWith(fontWeight: FontWeight.w400);
  final startSpan = start == null || start.isEmpty
      ? null
      : ledgerMarked(
          start,
          (startMono ? mono : base).colored(ink(startTone)),
          strong,
        );
  final endSpan = end == null || end.isEmpty
      ? null
      : ledgerMarked(end, base.colored(ink(endTone)), strong);
  // Each end's share of the row is its own width's (CSS's flex-shrink over
  // its flex-basis): both stand whole while they fit, and give way in
  // proportion, the end words wrapping against the row's end, when not.
  final scaler = MediaQuery.textScalerOf(context);
  final direction = Directionality.of(context);
  int share(InlineSpan span) {
    final p = TextPainter(
      text: span,
      textDirection: direction,
      textScaler: scaler,
    )..layout();
    final w = p.width;
    p.dispose();
    return math.max(1, (w * 100).ceil());
  }

  // At a large text size the two give way by turning rather than squeezing
  // (a squeezed word breaks letter by letter): the end words stand under the
  // start's when both do not fit one line.
  if (scaler.scale(1) > 1 && startSpan != null && endSpan != null) {
    return Wrap(
      alignment: WrapAlignment.spaceBetween,
      crossAxisAlignment: WrapCrossAlignment.end,
      spacing: _gap,
      children: [
        Text.rich(startSpan),
        Text.rich(endSpan, textAlign: TextAlign.end),
      ],
    );
  }
  // The mono and the sans on one baseline stand 18.23 tall: held to a whole
  // pixel, so the row, its rule and every row under it are too.
  return LedgerWholeHeight(
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.baseline,
      textBaseline: TextBaseline.alphabetic,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        if (startSpan != null)
          Flexible(
            flex: endSpan == null ? 1 : share(startSpan),
            child: Text.rich(startSpan),
          ),
        if (endSpan != null) ...[
          const SizedBox(width: _gap),
          Flexible(
            flex: startSpan == null ? 1 : share(endSpan),
            child: Text.rich(endSpan, textAlign: TextAlign.end),
          ),
        ],
      ],
    ),
  );
}