TTableHeader<T, K>.editable constructor

TTableHeader<T, K>.editable(
  1. String text, {
  2. required Object? get(
    1. T
    ),
  3. required Widget builder(
    1. BuildContext ctx,
    2. T data
    ),
  4. Widget displayBuilder(
    1. BuildContext ctx,
    2. T data
    )?,
  5. String? error(
    1. T
    )?,
  6. String? placeholder,
  7. int? flex,
  8. double? minWidth,
  9. double? maxWidth,
  10. Alignment? alignment,
})

Creates an editable cell header.

Implementation

TTableHeader.editable(
  this.text, {
  required Object? Function(T) get,
  required Widget Function(BuildContext ctx, T data) builder,
  Widget Function(BuildContext ctx, T data)? displayBuilder,
  String? Function(T data)? error,
  String? placeholder,
  this.flex,
  this.minWidth,
  this.maxWidth,
  this.alignment,
})  : map = get,
      builder = ((ctx, item, index) {
        final cellScope = TTableCellScope.maybeScopeOf(ctx);
        final activeCursor = cellScope?.activeCellNotifier ?? TTableCellScope.maybeOf(ctx);
        final data = item.data;
        final cellKey = "${item.key}_$text";
        final colors = ctx.colors;
        final textStyle =
            TTableScope.maybeOf(ctx)?.theme?.rowCardTheme.contentTextStyle ?? ctx.theme.tableTheme.rowCardTheme.contentTextStyle;

        final cellError = error?.call(data) ?? cellScope?.getError(data, text, cellKey);
        final hasError = cellError != null && cellError.isNotEmpty;

        final textAlign = alignment == null
            ? TextAlign.left
            : (alignment == Alignment.centerRight || alignment == Alignment.topRight || alignment == Alignment.bottomRight
                ? TextAlign.right
                : (alignment == Alignment.center ? TextAlign.center : TextAlign.left));

        Widget buildDisplay() {
          if (displayBuilder != null) {
            return displayBuilder(ctx, data);
          }

          final rawVal = get(data);
          final displayStr = rawVal?.toString() ?? '';
          final isEmpty = displayStr.trim().isEmpty;

          if (hasError) {
            return Tooltip(
              message: cellError,
              waitDuration: const Duration(milliseconds: 200),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Flexible(
                    child: Text(
                      isEmpty ? (placeholder ?? '—') : displayStr,
                      style: textStyle?.copyWith(
                            color: colors.error,
                            fontWeight: FontWeight.w600,
                          ) ??
                          TextStyle(
                            color: colors.error,
                            fontWeight: FontWeight.w600,
                          ),
                      textAlign: textAlign,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  const SizedBox(width: 4),
                  TIcon.raw(
                    HugeIcons.strokeRoundedAlert02,
                    size: 14,
                    color: colors.error,
                  ),
                ],
              ),
            );
          }

          if (isEmpty && placeholder != null) {
            return Text(
              placeholder,
              style: textStyle?.copyWith(
                    color: colors.onSurfaceVariant.withAlpha(100),
                    fontStyle: FontStyle.italic,
                  ) ??
                  TextStyle(
                    color: colors.onSurfaceVariant.withAlpha(100),
                    fontStyle: FontStyle.italic,
                  ),
              textAlign: textAlign,
              overflow: TextOverflow.ellipsis,
            );
          }

          return Text(
            displayStr,
            style: textStyle,
            textAlign: textAlign,
            overflow: TextOverflow.ellipsis,
          );
        }

        Widget buildEditor() {
          final editorWidget = builder(ctx, data);
          if (hasError) {
            return Tooltip(
              message: cellError,
              waitDuration: const Duration(milliseconds: 200),
              child: editorWidget,
            );
          }
          return editorWidget;
        }

        if (activeCursor != null) {
          return InkWell(
            focusColor: Colors.transparent,
            splashColor: Colors.transparent,
            onTap: () => activeCursor.value = cellKey,
            child: Container(
              width: double.infinity,
              alignment: alignment ?? Alignment.centerLeft,
              child: ValueListenableBuilder(
                valueListenable: activeCursor,
                builder: (ctx, active, _) => active == cellKey ? buildEditor() : buildDisplay(),
              ),
            ),
          );
        }
        return buildDisplay();
      });