glossKeyPaint function

Widget glossKeyPaint(
  1. BuildContext context,
  2. Set<WidgetState> states, {
  3. required GlossKeyKind kind,
  4. required String label,
  5. Widget? icon,
  6. bool destructive = false,
  7. GlossStyle? style,
})

A pearl or gel key of kind reading label (after icon), painted in states; the pressable round it is the caller's.

Implementation

Widget glossKeyPaint(
  BuildContext context,
  Set<WidgetState> states, {
  required GlossKeyKind kind,
  required String label,
  Widget? icon,
  bool destructive = false,
  GlossStyle? style,
}) {
  final theme = GlossTheme.of(context);
  final layered = DesignStyleOf([style, glossKeyTheme(theme, kind)], states);
  final face = glossKeyFace(
    theme,
    states,
    kind: kind,
    destructive: destructive,
    style: style,
  );
  final padding =
      layered.padding(Directionality.of(context)) ??
      EdgeInsets.symmetric(horizontal: 22 + theme.density.paddingDelta);
  final minimum = layered.minimumSize ?? Size(0, theme.controlHeight);
  // At large text sizes a key's words take a second line and the key
  // grows round them, rather than losing their end.
  final large = MediaQuery.textScalerOf(context).scale(10) >= 15;
  final gel =
      kind == GlossKeyKind.gel && !states.contains(WidgetState.disabled);
  // The words take the face's own colour as it moves (white on gel),
  // read from the surface they stand on.
  final words = Builder(
    builder: (context) => DefaultTextStyle.merge(
      style: theme.textTheme.label
          .copyWith(
            color: DefaultTextStyle.of(context).style.color,
            shadows: gel
                ? [
                    Shadow(
                      color: face.edge.withValues(alpha: 0.45),
                      offset: const Offset(0, -1),
                    ),
                  ]
                : null,
          )
          .merge(layered.textStyle),
      maxLines: large ? 2 : 1,
      textAlign: TextAlign.center,
      // Two lines are as wide as the longer, so the glyph stays
      // beside the words, not at the key's far end.
      textWidthBasis: TextWidthBasis.longestLine,
      overflow: TextOverflow.ellipsis,
      child: DesignCapCenter(
        capHeightFactor: glossCapHeightFactor,
        child: Text(label),
      ),
    ),
  );
  return DesignTapTarget(
    child: ConstrainedBox(
      constraints: BoxConstraints(
        minWidth: minimum.width,
        minHeight: minimum.height,
      ),
      child: AnimatedGlossSurface(
        face: face.copyWith(radius: _radius(layered)),
        child: Padding(
          padding: large
              ? padding +
                    const EdgeInsets.symmetric(vertical: GlossSpacing.small)
              : padding,
          // As tall as its words need (with the floor above): left
          // open, it would stretch to fill a bar's whole height.
          child: Center(
            widthFactor: 1,
            heightFactor: 1,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                if (icon != null) ...[
                  IconTheme.merge(
                    data: IconThemeData(size: layered.iconSize ?? 20),
                    child: icon,
                  ),
                  const SizedBox(width: GlossSpacing.small),
                ],
                Flexible(child: words),
              ],
            ),
          ),
        ),
      ),
    ),
  );
}