Visitar URL original
Expose tableColumnWidth argument (#138) · stuartmorgan-g/packages@8ea980e · GitHub
Skip to content

Commit 8ea980e

Browse files
神楽坂花火domesticmouse
authored andcommitted
Expose tableColumnWidth argument (flutter#138)
1 parent 860b42f commit 8ea980e

4 files changed

Lines changed: 70 additions & 9 deletions

File tree

‎packages/flutter_markdown/lib/src/builder.dart‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ abstract class MarkdownBuilderDelegate {
9494
class MarkdownBuilder implements md.NodeVisitor {
9595
/// Creates an object that builds a [Widget] tree from parsed Markdown.
9696
MarkdownBuilder({
97-
this.delegate,
98-
this.styleSheet,
99-
this.imageDirectory,
100-
this.imageBuilder,
101-
this.checkboxBuilder,
97+
@required this.delegate,
98+
@required this.styleSheet,
99+
@required this.imageDirectory,
100+
@required this.imageBuilder,
101+
@required this.checkboxBuilder,
102102
});
103103

104104
/// A delegate that controls how link and `pre` elements behave.
@@ -233,6 +233,9 @@ class MarkdownBuilder implements md.NodeVisitor {
233233
_listIndents.removeLast();
234234
} else if (tag == 'li') {
235235
if (_listIndents.isNotEmpty) {
236+
if (element.children.length == 0) {
237+
element.children.add(md.Text(''));
238+
}
236239
Widget bullet;
237240
dynamic el = element.children[0];
238241
if (el is md.Element && el.attributes['type'] == 'checkbox') {
@@ -253,7 +256,7 @@ class MarkdownBuilder implements md.NodeVisitor {
253256
}
254257
} else if (tag == 'table') {
255258
child = Table(
256-
defaultColumnWidth: IntrinsicColumnWidth(),
259+
defaultColumnWidth: styleSheet.tableColumnWidth,
257260
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
258261
border: styleSheet.tableBorder,
259262
children: _tables.removeLast().rows,

‎packages/flutter_markdown/lib/src/style_sheet.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class MarkdownStyleSheet {
3030
this.tableBody,
3131
this.tableHeadAlign,
3232
this.tableBorder,
33+
this.tableColumnWidth,
3334
this.tableCellsPadding,
3435
this.tableCellsDecoration,
3536
this.blockquotePadding,
@@ -93,6 +94,7 @@ class MarkdownStyleSheet {
9394
tableBody: theme.textTheme.body1,
9495
tableHeadAlign: TextAlign.center,
9596
tableBorder: TableBorder.all(color: Colors.grey.shade300, width: 0),
97+
tableColumnWidth: const FlexColumnWidth(),
9698
tableCellsPadding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
9799
tableCellsDecoration: BoxDecoration(color: Colors.grey.shade50),
98100
blockquotePadding: const EdgeInsets.all(8.0),
@@ -147,6 +149,7 @@ class MarkdownStyleSheet {
147149
tableBody: theme.textTheme.body1,
148150
tableHeadAlign: TextAlign.center,
149151
tableBorder: TableBorder.all(color: Colors.grey.shade300),
152+
tableColumnWidth: const FlexColumnWidth(),
150153
tableCellsPadding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
151154
tableCellsDecoration: BoxDecoration(color: Colors.grey.shade50),
152155
blockquotePadding: const EdgeInsets.all(8.0),
@@ -192,6 +195,7 @@ class MarkdownStyleSheet {
192195
TextStyle tableBody,
193196
TextAlign tableHeadAlign,
194197
TableBorder tableBorder,
198+
TableColumnWidth tableColumnWidth,
195199
EdgeInsets tableCellsPadding,
196200
Decoration tableCellsDecoration,
197201
EdgeInsets blockquotePadding,
@@ -224,6 +228,7 @@ class MarkdownStyleSheet {
224228
tableBody: tableBody ?? this.tableBody,
225229
tableHeadAlign: tableHeadAlign ?? this.tableHeadAlign,
226230
tableBorder: tableBorder ?? this.tableBorder,
231+
tableColumnWidth: tableColumnWidth ?? this.tableColumnWidth,
227232
tableCellsPadding: tableCellsPadding ?? this.tableCellsPadding,
228233
tableCellsDecoration: tableCellsDecoration ?? this.tableCellsDecoration,
229234
blockquotePadding: blockquotePadding ?? this.blockquotePadding,
@@ -302,6 +307,9 @@ class MarkdownStyleSheet {
302307
/// The [TableBorder] to use for `table` elements.
303308
final TableBorder tableBorder;
304309

310+
/// The [TableColumnWidth] to use for `th` and `td` elements.
311+
final TableColumnWidth tableColumnWidth;
312+
305313
/// The padding to use for `th` and `td` elements.
306314
final EdgeInsets tableCellsPadding;
307315

@@ -357,6 +365,7 @@ class MarkdownStyleSheet {
357365
typedOther.tableBody == tableBody &&
358366
typedOther.tableHeadAlign == tableHeadAlign &&
359367
typedOther.tableBorder == tableBorder &&
368+
typedOther.tableColumnWidth == tableColumnWidth &&
360369
typedOther.tableCellsPadding == tableCellsPadding &&
361370
typedOther.tableCellsDecoration == tableCellsDecoration &&
362371
typedOther.blockquotePadding == blockquotePadding &&
@@ -392,6 +401,7 @@ class MarkdownStyleSheet {
392401
tableBody,
393402
tableHeadAlign,
394403
tableBorder,
404+
tableColumnWidth,
395405
tableCellsPadding,
396406
tableCellsDecoration,
397407
blockquotePadding,

‎packages/flutter_markdown/lib/src/widget.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class _MarkdownWidgetState extends State<MarkdownWidget>
140140
delegate: this,
141141
styleSheet: styleSheet,
142142
imageDirectory: widget.imageDirectory,
143+
imageBuilder: widget.imageBuilder,
144+
checkboxBuilder: widget.checkboxBuilder,
143145
);
144146
_children = builder.build(document.parseLines(lines));
145147
}

‎packages/flutter_markdown/test/flutter_markdown_test.dart‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter/gestures.dart';
99
import 'package:flutter/material.dart';
1010
import 'package:flutter/widgets.dart';
1111
import 'package:flutter_markdown/flutter_markdown.dart';
12+
import 'package:flutter_markdown/src/style_sheet.dart';
1213
import 'package:flutter_test/flutter_test.dart';
1314
import 'package:mockito/mockito.dart';
1415

@@ -92,16 +93,34 @@ void main() {
9293
]);
9394
});
9495

96+
testWidgets('Empty List Item', (WidgetTester tester) async {
97+
await tester.pumpWidget(
98+
_boilerplate(const MarkdownBody(data: '- \n- Item 2\n- Item 3')),
99+
);
100+
101+
final Iterable<Widget> widgets = tester.allWidgets;
102+
_expectTextStrings(widgets, <String>[
103+
'•',
104+
'•',
105+
'Item 2',
106+
'•',
107+
'Item 3',
108+
]);
109+
});
110+
95111
testWidgets('Task list', (WidgetTester tester) async {
96112
await tester.pumpWidget(
97113
_boilerplate(const MarkdownBody(data: '- [x] Item 1\n- [ ] Item 2')),
98114
);
99115

100116
final Iterable<Widget> widgets = tester.allWidgets;
101117

102-
// \ue834 -> Icons.check_box
103-
// \ue835 -> Icons.check_box_outline_blank
104-
_expectTextStrings(widgets, <String>['\ue834', 'Item 1', '\ue835', 'Item 2']);
118+
_expectTextStrings(widgets, <String>[
119+
String.fromCharCode(Icons.check_box.codePoint),
120+
'Item 1',
121+
String.fromCharCode(Icons.check_box_outline_blank.codePoint),
122+
'Item 2',
123+
]);
105124
});
106125

107126
testWidgets('Horizontal Rule', (WidgetTester tester) async {
@@ -420,6 +439,21 @@ void main() {
420439

421440
expect(tables.length, 2);
422441
});
442+
443+
testWidgets('column width should follow stylesheet', (WidgetTester tester) async {
444+
final ThemeData theme = ThemeData.light().copyWith(textTheme: textTheme);
445+
446+
const String data = '|Header|\n|----|\n|Column|';
447+
const FixedColumnWidth columnWidth = FixedColumnWidth(100);
448+
final MarkdownStyleSheet style = MarkdownStyleSheet.fromTheme(theme)
449+
.copyWith(tableColumnWidth: columnWidth);
450+
451+
await tester.pumpWidget(_boilerplate(MarkdownBody(data: data, styleSheet: style)));
452+
453+
final Table table = tester.widget(find.byType(Table));
454+
455+
expect(table.defaultColumnWidth, columnWidth);
456+
});
423457
});
424458

425459
group('uri data scheme', () {
@@ -545,6 +579,18 @@ void main() {
545579
expect(text1.text, isNot(text2.text));
546580
});
547581

582+
testWidgets('Changing config - imageBuilder', (WidgetTester tester) async {
583+
final String data = '![alt](https://img.png)';
584+
final MarkdownImageBuilder builder = (_) => Image.asset('assets/logo.png');
585+
586+
await tester.pumpWidget(_boilerplate(Markdown(data: data, imageBuilder: builder)));
587+
588+
final Image image = tester.widget(find.byType(Image));
589+
590+
expect(image.image.runtimeType, AssetImage);
591+
expect((image.image as AssetImage).assetName, 'assets/logo.png');
592+
});
593+
548594
testWidgets('Style equality', (WidgetTester tester) async {
549595
final ThemeData theme = new ThemeData.light().copyWith(textTheme: textTheme);
550596

0 commit comments

Comments
 (0)