import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; import '../domain/artifact_table.dart'; /// The truncation note for a capped table preview. String artifactTableNote(ArtifactTable table) => 'Showing ${table.rows.length} of ${table.totalRows} rows. Open the file ' 'for the rest.'; /// A CSV/TSV preview: the first row as the header, every cell selectable, and /// the whole grid scrolling horizontally inside the viewer's own vertical /// scroll. Short rows are padded with empty cells; nothing is invented. class ArtifactTableView extends StatelessWidget { const ArtifactTableView({ super.key, required this.text, required this.delimiter, }); final String text; final String delimiter; @override Widget build(BuildContext context) { final table = parseArtifactTable(text, delimiter: delimiter); if (table.rows.isEmpty) { return const Padding( padding: EdgeInsets.all(16), child: Text('The table is empty.'), ); } final cupertino = usesCupertinoHost(context); final border = cupertino ? CupertinoColors.separator.resolveFrom(context) : Theme.of(context).dividerColor; final header = cupertino ? CupertinoColors.secondarySystemFill.resolveFrom(context) : Theme.of(context).colorScheme.surfaceContainerHighest; final columns = table.columnCount; Widget cell(String value, {bool head = false}) => Padding( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6), child: SelectableText( value, style: head ? const TextStyle(fontWeight: FontWeight.w600) : null, ), ); return Padding( padding: const EdgeInsetsDirectional.fromSTEB(16, 8, 16, 16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (table.truncated) Padding( padding: const EdgeInsets.only(bottom: 8), child: Text(artifactTableNote(table)), ), SingleChildScrollView( key: const Key('artifact-table'), scrollDirection: Axis.horizontal, child: Table( defaultColumnWidth: const IntrinsicColumnWidth(), border: TableBorder.all(color: border), children: [ for (var r = 0; r < table.rows.length; r++) TableRow( decoration: r == 0 ? BoxDecoration(color: header) : null, children: [ for (var c = 0; c < columns; c++) cell( c < table.rows[r].length ? table.rows[r][c] : '', head: r == 0, ), ], ), ], ), ), ], ), ); } }