Wrap chat markdown tables to the transcript width

Tables no longer scroll sideways: columns size to content and shrink to
fit, wrapping cell text; an unbreakable token breaks inside its cell.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 10:15:12 +00:00
parent b264043eee
commit 93e9042a8b
6 changed files with 154 additions and 22 deletions
@@ -11,13 +11,14 @@ import 'chat_link_spans.dart';
import 'chat_markdown.dart';
import 'chat_markdown_cache.dart';
import 'chat_message_bubble.dart';
import 'chat_table_column_width.dart';
/// Renders one assistant text part with native text widgets and the host's own
/// theme roles. Links (`http`, `https`, `mailto` and `www.` hosts only) open
/// through [onOpenLink] when given, else the [ChatLinkScope] launcher, which by
/// default opens them outside the app. No font family, size, colour or component
/// dimension is overridden; code keeps its exact whitespace and scrolls
/// sideways instead of widening the transcript.
/// sideways instead of widening the transcript, while table cells wrap.
class ChatMarkdownView extends StatefulWidget {
const ChatMarkdownView({super.key, required this.text, this.onOpenLink});
@@ -176,24 +177,22 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
),
);
case MarkdownTable(header: final header, rows: final rows):
return ChatTranscriptHorizontalScrollView(
label: 'table',
child: Table(
defaultColumnWidth: const IntrinsicColumnWidth(),
border: TableBorder.all(color: _dividerColor(context)),
children: [
// Cells wrap to the transcript width; the table never scrolls sideways.
return Table(
defaultColumnWidth: ChatTableColumnWidth(header.length),
border: TableBorder.all(color: _dividerColor(context)),
children: [
TableRow(
decoration: BoxDecoration(color: _headerFill(context)),
children: [
for (final cell in header) _cell(context, cell, header: true),
],
),
for (final row in rows)
TableRow(
decoration: BoxDecoration(color: _headerFill(context)),
children: [
for (final cell in header) _cell(context, cell, header: true),
],
children: [for (final cell in row) _cell(context, cell)],
),
for (final row in rows)
TableRow(
children: [for (final cell in row) _cell(context, cell)],
),
],
),
],
);
case MarkdownCode(language: final language, code: final code):
final labelStyle = _roleStyle(
@@ -0,0 +1,33 @@
import 'dart:math' as math;
import 'package:flutter/widgets.dart';
/// Sizes a markdown table column to its content but lets it wrap to fit the
/// transcript instead of scrolling sideways.
///
/// A column asks for its widest line ([IntrinsicColumnWidth]). When the table
/// is wider than its container, [Table] shrinks columns towards their minimum,
/// which is normally the longest single word. That floor is capped at an equal
/// share of the container, so a long identifier or URL breaks inside its cell
/// rather than pushing the table past the edge.
class ChatTableColumnWidth extends TableColumnWidth {
const ChatTableColumnWidth(this.columnCount);
final int columnCount;
static const _content = IntrinsicColumnWidth();
@override
double maxIntrinsicWidth(Iterable<RenderBox> cells, double containerWidth) =>
_content.maxIntrinsicWidth(cells, containerWidth);
@override
double minIntrinsicWidth(Iterable<RenderBox> cells, double containerWidth) {
final words = _content.minIntrinsicWidth(cells, containerWidth);
if (!containerWidth.isFinite || columnCount < 1) return words;
return math.min(words, containerWidth / columnCount);
}
@override
double? flex(Iterable<RenderBox> cells) => null;
}