Skip to content

Commit

Permalink
[flutter_markdown] Fix table column alignment (#7327)
Browse files Browse the repository at this point in the history
> This is my first contribution here so please let me know if I have overlooked anything or am missing any required steps. Thanks!

This PR fixes the following issue: flutter/flutter#109487. This fix applies the appropriate `alignment` property to `Wrap` depending on the `textAlign`.

| Before | After |
|--------|------|
|<img width="375" alt="image" src="https://github.com/user-attachments/assets/e10c0f2b-1c6b-439c-b352-ae6447605f35">|<img width="379" alt="image" src="https://github.com/user-attachments/assets/75ca4c7a-6454-4892-86c5-fec121061a58">|
  • Loading branch information
hjiangsu authored Aug 8, 2024
1 parent 0363e42 commit bd8db03
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/flutter_markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.7.3+1

* Fixes issue with table column alignments not being respected.
* Updates minimum supported SDK version to Flutter 3.19/Dart 3.3.

## 0.7.3
Expand Down
10 changes: 9 additions & 1 deletion packages/flutter_markdown/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,15 @@ class MarkdownBuilder implements md.NodeVisitor {
child: DefaultTextStyle(
style: styleSheet.tableBody!,
textAlign: textAlign,
child: Wrap(children: children as List<Widget>),
child: Wrap(
alignment: switch (textAlign) {
TextAlign.left => WrapAlignment.start,
TextAlign.center => WrapAlignment.center,
TextAlign.right => WrapAlignment.end,
_ => WrapAlignment.start,
},
children: children as List<Widget>,
),
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_markdown/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
formatted with simple Markdown tags.
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
version: 0.7.3
version: 0.7.3+1

environment:
sdk: ^3.3.0
Expand Down
19 changes: 19 additions & 0 deletions packages/flutter_markdown/test/table_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ void defineTests() {
},
);

testWidgets(
'should work with table alignments',
(WidgetTester tester) async {
const String data =
'|Header 1|Header 2|Header 3|\n|:----|:----:|----:|\n|Col 1|Col 2|Col 3|';
await tester.pumpWidget(
boilerplate(
const MarkdownBody(data: data),
),
);

final Iterable<Wrap> wraps = tester.widgetList(find.byType(Wrap));

expect(wraps.first.alignment, WrapAlignment.start);
expect(wraps.elementAt(1).alignment, WrapAlignment.center);
expect(wraps.last.alignment, WrapAlignment.end);
},
);

testWidgets(
'should work with styling',
(WidgetTester tester) async {
Expand Down

0 comments on commit bd8db03

Please sign in to comment.