Skip to content

Commit d472347

Browse files
authored
[flutter_markdown] Add TableCellVerticalAlignment property in markdown stylesheet (#3880)
Add property `tableVerticalAlignment` to markdown style sheet that sets the vertical alignment of table properties. flutter/flutter#125872
1 parent 7042079 commit d472347

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

packages/flutter_markdown/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.16
2+
3+
* Adds `tableVerticalAlignment` property to allow aligning table cells vertically.
4+
15
## 0.6.15+1
26

37
* Fixes 'The Scrollbar's ScrollController has no ScrollPosition attached' exception when scrolling scrollable code blocks.

packages/flutter_markdown/lib/src/builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ class MarkdownBuilder implements md.NodeVisitor {
421421
} else if (tag == 'table') {
422422
child = Table(
423423
defaultColumnWidth: styleSheet.tableColumnWidth!,
424-
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
424+
defaultVerticalAlignment: styleSheet.tableVerticalAlignment,
425425
border: styleSheet.tableBorder,
426426
children: _tables.removeLast().rows,
427427
);

packages/flutter_markdown/lib/src/style_sheet.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class MarkdownStyleSheet {
4242
this.tableColumnWidth,
4343
this.tableCellsPadding,
4444
this.tableCellsDecoration,
45+
this.tableVerticalAlignment = TableCellVerticalAlignment.middle,
4546
this.blockquotePadding,
4647
this.blockquoteDecoration,
4748
this.codeblockPadding,
@@ -362,6 +363,7 @@ class MarkdownStyleSheet {
362363
TableColumnWidth? tableColumnWidth,
363364
EdgeInsets? tableCellsPadding,
364365
Decoration? tableCellsDecoration,
366+
TableCellVerticalAlignment? tableVerticalAlignment,
365367
EdgeInsets? blockquotePadding,
366368
Decoration? blockquoteDecoration,
367369
EdgeInsets? codeblockPadding,
@@ -414,6 +416,8 @@ class MarkdownStyleSheet {
414416
tableColumnWidth: tableColumnWidth ?? this.tableColumnWidth,
415417
tableCellsPadding: tableCellsPadding ?? this.tableCellsPadding,
416418
tableCellsDecoration: tableCellsDecoration ?? this.tableCellsDecoration,
419+
tableVerticalAlignment:
420+
tableVerticalAlignment ?? this.tableVerticalAlignment,
417421
blockquotePadding: blockquotePadding ?? this.blockquotePadding,
418422
blockquoteDecoration: blockquoteDecoration ?? this.blockquoteDecoration,
419423
codeblockPadding: codeblockPadding ?? this.codeblockPadding,
@@ -475,6 +479,7 @@ class MarkdownStyleSheet {
475479
tableColumnWidth: other.tableColumnWidth,
476480
tableCellsPadding: other.tableCellsPadding,
477481
tableCellsDecoration: other.tableCellsDecoration,
482+
tableVerticalAlignment: other.tableVerticalAlignment,
478483
blockquotePadding: other.blockquotePadding,
479484
blockquoteDecoration: other.blockquoteDecoration,
480485
codeblockPadding: other.codeblockPadding,
@@ -594,6 +599,9 @@ class MarkdownStyleSheet {
594599
/// The decoration to use for `th` and `td` elements.
595600
final Decoration? tableCellsDecoration;
596601

602+
/// The [TableCellVerticalAlignment] to use for `th` and `td` elements.
603+
final TableCellVerticalAlignment tableVerticalAlignment;
604+
597605
/// The padding to use for `blockquote` elements.
598606
final EdgeInsets? blockquotePadding;
599607

@@ -692,6 +700,7 @@ class MarkdownStyleSheet {
692700
other.tableColumnWidth == tableColumnWidth &&
693701
other.tableCellsPadding == tableCellsPadding &&
694702
other.tableCellsDecoration == tableCellsDecoration &&
703+
other.tableVerticalAlignment == tableVerticalAlignment &&
695704
other.blockquotePadding == blockquotePadding &&
696705
other.blockquoteDecoration == blockquoteDecoration &&
697706
other.codeblockPadding == codeblockPadding &&
@@ -748,6 +757,7 @@ class MarkdownStyleSheet {
748757
tableColumnWidth,
749758
tableCellsPadding,
750759
tableCellsDecoration,
760+
tableVerticalAlignment,
751761
blockquotePadding,
752762
blockquoteDecoration,
753763
codeblockPadding,

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.6.15+1
7+
version: 0.6.16
88

99
environment:
1010
sdk: ">=3.0.0 <4.0.0"

packages/flutter_markdown/test/table_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,66 @@ void defineTests() {
121121
},
122122
);
123123

124+
testWidgets(
125+
'table cell vertical alignment should default to middle',
126+
(WidgetTester tester) async {
127+
final ThemeData theme =
128+
ThemeData.light().copyWith(textTheme: textTheme);
129+
130+
const String data = '|Header|\n|----|\n|Column|';
131+
final MarkdownStyleSheet style = MarkdownStyleSheet.fromTheme(theme);
132+
await tester.pumpWidget(
133+
boilerplate(MarkdownBody(data: data, styleSheet: style)));
134+
135+
final Table table = tester.widget(find.byType(Table));
136+
137+
expect(
138+
table.defaultVerticalAlignment, TableCellVerticalAlignment.middle);
139+
},
140+
);
141+
142+
testWidgets(
143+
'table cell vertical alignment should follow stylesheet',
144+
(WidgetTester tester) async {
145+
final ThemeData theme =
146+
ThemeData.light().copyWith(textTheme: textTheme);
147+
148+
const String data = '|Header|\n|----|\n|Column|';
149+
const TableCellVerticalAlignment tableCellVerticalAlignment =
150+
TableCellVerticalAlignment.top;
151+
final MarkdownStyleSheet style = MarkdownStyleSheet.fromTheme(theme)
152+
.copyWith(tableVerticalAlignment: tableCellVerticalAlignment);
153+
154+
await tester.pumpWidget(
155+
boilerplate(MarkdownBody(data: data, styleSheet: style)));
156+
157+
final Table table = tester.widget(find.byType(Table));
158+
159+
expect(table.defaultVerticalAlignment, tableCellVerticalAlignment);
160+
},
161+
);
162+
163+
testWidgets(
164+
'table cell vertical alignment should follow stylesheet for different values',
165+
(WidgetTester tester) async {
166+
final ThemeData theme =
167+
ThemeData.light().copyWith(textTheme: textTheme);
168+
169+
const String data = '|Header|\n|----|\n|Column|';
170+
const TableCellVerticalAlignment tableCellVerticalAlignment =
171+
TableCellVerticalAlignment.bottom;
172+
final MarkdownStyleSheet style = MarkdownStyleSheet.fromTheme(theme)
173+
.copyWith(tableVerticalAlignment: tableCellVerticalAlignment);
174+
175+
await tester.pumpWidget(
176+
boilerplate(MarkdownBody(data: data, styleSheet: style)));
177+
178+
final Table table = tester.widget(find.byType(Table));
179+
180+
expect(table.defaultVerticalAlignment, tableCellVerticalAlignment);
181+
},
182+
);
183+
124184
testWidgets(
125185
'table with last row of empty table cells',
126186
(WidgetTester tester) async {

0 commit comments

Comments
 (0)