Skip to content

Commit 3bb7397

Browse files
authored
[flutter_markdown] Allow tables to be scrollable with IntrinsicColumnWidth (#8526)
Allows markdown tables to be scrollable with IntrinsicColumnWidth. Allows setting the scrollbar thumbVisibility based on stylesheet - Additional fix from flutter/flutter#129052
1 parent 389c9b5 commit 3bb7397

File tree

6 files changed

+89
-3
lines changed

6 files changed

+89
-3
lines changed

packages/flutter_markdown/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.7.6
2+
3+
* Adds horizontal scrolling for table when using `tableColumnWidth: IntrinsicColumnWidth()`.
4+
* Adds styleSheet option `tableScrollbarThumbVisibility` for setting the `thumbVisibility` on tables' `ScrollBar`.
5+
16
## 0.7.5
27

38
* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.

packages/flutter_markdown/lib/src/builder.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,14 @@ class MarkdownBuilder implements md.NodeVisitor {
451451
);
452452
}
453453
} else if (tag == 'table') {
454-
if (styleSheet.tableColumnWidth is FixedColumnWidth) {
454+
if (styleSheet.tableColumnWidth is FixedColumnWidth ||
455+
styleSheet.tableColumnWidth is IntrinsicColumnWidth) {
455456
child = _ScrollControllerBuilder(
456457
builder: (BuildContext context,
457458
ScrollController tableScrollController, Widget? child) {
458459
return Scrollbar(
459460
controller: tableScrollController,
461+
thumbVisibility: styleSheet.tableScrollbarThumbVisibility,
460462
child: SingleChildScrollView(
461463
controller: tableScrollController,
462464
scrollDirection: Axis.horizontal,

packages/flutter_markdown/lib/src/style_sheet.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class MarkdownStyleSheet {
4141
this.tablePadding,
4242
this.tableBorder,
4343
this.tableColumnWidth,
44+
this.tableScrollbarThumbVisibility,
4445
this.tableCellsPadding,
4546
this.tableCellsDecoration,
4647
this.tableVerticalAlignment = TableCellVerticalAlignment.middle,
@@ -375,6 +376,7 @@ class MarkdownStyleSheet {
375376
EdgeInsets? tablePadding,
376377
TableBorder? tableBorder,
377378
TableColumnWidth? tableColumnWidth,
379+
bool? tableScrollbarThumbVisibility,
378380
EdgeInsets? tableCellsPadding,
379381
Decoration? tableCellsDecoration,
380382
TableCellVerticalAlignment? tableVerticalAlignment,
@@ -441,6 +443,7 @@ class MarkdownStyleSheet {
441443
tablePadding: tablePadding ?? this.tablePadding,
442444
tableBorder: tableBorder ?? this.tableBorder,
443445
tableColumnWidth: tableColumnWidth ?? this.tableColumnWidth,
446+
tableScrollbarThumbVisibility: tableScrollbarThumbVisibility,
444447
tableCellsPadding: tableCellsPadding ?? this.tableCellsPadding,
445448
tableCellsDecoration: tableCellsDecoration ?? this.tableCellsDecoration,
446449
tableVerticalAlignment:
@@ -508,6 +511,7 @@ class MarkdownStyleSheet {
508511
tablePadding: other.tablePadding,
509512
tableBorder: other.tableBorder,
510513
tableColumnWidth: other.tableColumnWidth,
514+
tableScrollbarThumbVisibility: other.tableScrollbarThumbVisibility,
511515
tableCellsPadding: other.tableCellsPadding,
512516
tableCellsDecoration: other.tableCellsDecoration,
513517
tableVerticalAlignment: other.tableVerticalAlignment,
@@ -633,6 +637,9 @@ class MarkdownStyleSheet {
633637
/// The [TableColumnWidth] to use for `th` and `td` elements.
634638
final TableColumnWidth? tableColumnWidth;
635639

640+
/// The scrollbar thumbVisibility when the table is scrollable.
641+
final bool? tableScrollbarThumbVisibility;
642+
636643
/// The padding to use for `th` and `td` elements.
637644
final EdgeInsets? tableCellsPadding;
638645

packages/flutter_markdown/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
44
formatted with simple Markdown tags.
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
7-
version: 0.7.5
7+
version: 0.7.6
88

99
environment:
1010
sdk: ^3.4.0

packages/flutter_markdown/test/scrollable_test.dart

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void defineTests() {
113113
);
114114

115115
testWidgets(
116-
'table',
116+
'table with fixed column width',
117117
(WidgetTester tester) async {
118118
const String data = '|Header 1|Header 2|Header 3|'
119119
'\n|-----|-----|-----|'
@@ -140,6 +140,34 @@ void defineTests() {
140140
},
141141
);
142142

143+
testWidgets(
144+
'table with intrinsic column width',
145+
(WidgetTester tester) async {
146+
const String data = '|Header 1|Header 2|Header 3|'
147+
'\n|-----|-----|-----|'
148+
'\n|Col 1|Col 2|Col 3|';
149+
await tester.pumpWidget(
150+
boilerplate(
151+
MediaQuery(
152+
data: const MediaQueryData(),
153+
child: MarkdownBody(
154+
data: data,
155+
styleSheet: MarkdownStyleSheet(
156+
tableColumnWidth: const IntrinsicColumnWidth(),
157+
),
158+
),
159+
),
160+
),
161+
);
162+
163+
final Iterable<Widget> widgets = tester.allWidgets;
164+
final Iterable<SingleChildScrollView> scrollViews =
165+
widgets.whereType<SingleChildScrollView>();
166+
expect(scrollViews, isNotEmpty);
167+
expect(scrollViews.first.controller, isNotNull);
168+
},
169+
);
170+
143171
testWidgets(
144172
'two tables use different scroll controllers',
145173
(WidgetTester tester) async {

packages/flutter_markdown/test/table_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,50 @@ void defineTests() {
200200
},
201201
);
202202

203+
testWidgets(
204+
'table scrollbar thumbVisibility should follow stylesheet',
205+
(WidgetTester tester) async {
206+
final ThemeData theme =
207+
ThemeData.light().copyWith(textTheme: textTheme);
208+
209+
const String data = '|Header|\n|----|\n|Column|';
210+
const bool tableScrollbarThumbVisibility = true;
211+
final MarkdownStyleSheet style = MarkdownStyleSheet.fromTheme(theme)
212+
.copyWith(
213+
tableColumnWidth: const FixedColumnWidth(100),
214+
tableScrollbarThumbVisibility: tableScrollbarThumbVisibility);
215+
216+
await tester.pumpWidget(
217+
boilerplate(MarkdownBody(data: data, styleSheet: style)));
218+
219+
final Scrollbar scrollbar = tester.widget(find.byType(Scrollbar));
220+
221+
expect(scrollbar.thumbVisibility, tableScrollbarThumbVisibility);
222+
},
223+
);
224+
225+
testWidgets(
226+
'table scrollbar thumbVisibility should follow stylesheet',
227+
(WidgetTester tester) async {
228+
final ThemeData theme =
229+
ThemeData.light().copyWith(textTheme: textTheme);
230+
231+
const String data = '|Header|\n|----|\n|Column|';
232+
const bool tableScrollbarThumbVisibility = false;
233+
final MarkdownStyleSheet style = MarkdownStyleSheet.fromTheme(theme)
234+
.copyWith(
235+
tableColumnWidth: const FixedColumnWidth(100),
236+
tableScrollbarThumbVisibility: tableScrollbarThumbVisibility);
237+
238+
await tester.pumpWidget(
239+
boilerplate(MarkdownBody(data: data, styleSheet: style)));
240+
241+
final Scrollbar scrollbar = tester.widget(find.byType(Scrollbar));
242+
243+
expect(scrollbar.thumbVisibility, tableScrollbarThumbVisibility);
244+
},
245+
);
246+
203247
testWidgets(
204248
'table with last row of empty table cells',
205249
(WidgetTester tester) async {

0 commit comments

Comments
 (0)