build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the UI represented by this widget.

Implementation

@override
Widget build(BuildContext context) {
  final theme = ThemeScope.of(context);
  final active = enabled && onTap != null;
  final fg = selected
      ? (selectedForeground ?? theme.onPrimary)
      : (foreground ?? (enabled ? theme.onSurface : theme.muted));
  final secondaryFg = selected
      ? (selectedForeground ?? theme.onPrimary)
      : (enabled ? theme.muted : theme.muted);
  final bg = selected
      ? (selectedBackground ?? theme.primary)
      : (background ?? theme.surface);

  final resolvedTitleStyle = _copyStyle(titleStyle ?? theme.bodyMedium)
    ..foreground(fg);
  final resolvedSubtitleStyle = _copyStyle(subtitleStyle ?? theme.bodySmall)
    ..foreground(secondaryFg);
  if (!enabled) {
    resolvedTitleStyle.dim();
    resolvedSubtitleStyle.dim();
  }

  final titleWidget = _slotWidget(title, resolvedTitleStyle);
  final subtitleWidget = subtitle == null
      ? null
      : _slotWidget(subtitle!, resolvedSubtitleStyle);

  final body = Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    gap: subtitleWidget == null ? 0 : 1,
    children: [titleWidget, if (subtitleWidget != null) subtitleWidget],
  );

  Widget tile = Container(
    padding:
        padding ??
        EdgeInsets.symmetric(horizontal: 2, vertical: dense ? 0 : 1),
    color: bg,
    child: Row(
      gap: gap,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (leading != null)
          minLeadingWidth > 0
              ? Container(width: minLeadingWidth, child: leading)
              : leading!,
        Expanded(child: body),
        if (trailing != null) trailing!,
      ],
    ),
  );

  if (active) {
    tile = GestureDetector(onTap: onTap, child: tile);
  }

  if (!enabled) {
    tile = Opacity(opacity: 0.7, child: tile);
  }

  return tile;
}