Visitar URL original
Add option to fix intrinsic height measurement problems (#313) · stuartmorgan-g/packages@f5226f2 · GitHub
Skip to content

Commit f5226f2

Browse files
Add option to fix intrinsic height measurement problems (flutter#313)
1 parent eb51236 commit f5226f2

4 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎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.5.2
2+
3+
* Added `MarkdownListItemCrossAxisAlignment` to allow for intrinsic height
4+
measurements of lists.
5+
16
## 0.5.1
27

38
* Fix user defined builders

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class MarkdownBuilder implements md.NodeVisitor {
100100
@required this.imageBuilder,
101101
@required this.checkboxBuilder,
102102
@required this.builders,
103+
@required this.listItemCrossAxisAlignment,
103104
this.fitContent = false,
104105
});
105106

@@ -129,6 +130,13 @@ class MarkdownBuilder implements md.NodeVisitor {
129130
/// Whether to allow the widget to fit the child content.
130131
final bool fitContent;
131132

133+
/// Controls the cross axis alignment for the bullet and list item content
134+
/// in lists.
135+
///
136+
/// Defaults to [MarkdownListItemCrossAxisAlignment.baseline], which
137+
/// does not allow for intrinsic height measurements.
138+
final MarkdownListItemCrossAxisAlignment listItemCrossAxisAlignment;
139+
132140
final List<String> _listIndents = <String>[];
133141
final List<_BlockElement> _blocks = <_BlockElement>[];
134142
final List<_TableElement> _tables = <_TableElement>[];
@@ -318,10 +326,15 @@ class MarkdownBuilder implements md.NodeVisitor {
318326
} else {
319327
bullet = _buildBullet(_listIndents.last);
320328
}
321-
// See #147 and #169
322329
child = Row(
323-
textBaseline: TextBaseline.alphabetic,
324-
crossAxisAlignment: CrossAxisAlignment.baseline,
330+
textBaseline: listItemCrossAxisAlignment ==
331+
MarkdownListItemCrossAxisAlignment.start
332+
? null
333+
: TextBaseline.alphabetic,
334+
crossAxisAlignment: listItemCrossAxisAlignment ==
335+
MarkdownListItemCrossAxisAlignment.start
336+
? CrossAxisAlignment.start
337+
: CrossAxisAlignment.baseline,
325338
children: <Widget>[
326339
SizedBox(
327340
width: styleSheet.listIndent,

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:convert';
77
import 'package:flutter/cupertino.dart';
88
import 'package:flutter/gestures.dart';
99
import 'package:flutter/material.dart';
10+
import 'package:flutter/rendering.dart';
1011
import 'package:markdown/markdown.dart' as md;
1112
import 'package:meta/meta.dart';
1213

@@ -72,6 +73,40 @@ abstract class MarkdownElementBuilder {
7273
/// is running on. Material on Android and Cupertino on iOS
7374
enum MarkdownStyleSheetBaseTheme { material, cupertino, platform }
7475

76+
/// Enumeration of alignment strategies for the cross axis of list items.
77+
enum MarkdownListItemCrossAxisAlignment {
78+
/// Uses [CrossAxisAlignment.baseline] for the row the bullet and the list
79+
/// item are placed in.
80+
///
81+
/// This alignment will ensure that the bullet always lines up with
82+
/// the list text on the baseline.
83+
///
84+
/// However, note that this alignment does not support intrinsic height
85+
/// measurements because [RenderFlex] does not support it for
86+
/// [CrossAxisAlignment.baseline].
87+
/// See https://github.com/flutter/flutter_markdown/issues/311 for cases,
88+
/// where this might be a problem for you.
89+
///
90+
/// See also:
91+
/// * [start], which allows for intrinsic height measurements.
92+
baseline,
93+
94+
/// Uses [CrossAxisAlignment.start] for the row the bullet and the list item
95+
/// are placed in.
96+
///
97+
/// This alignment will ensure that intrinsic height measurements work.
98+
///
99+
/// However, note that this alignment might not line up the bullet with the
100+
/// list text in the way you would expect in certain scenarios.
101+
/// See https://github.com/flutter/flutter_markdown/issues/169 for example
102+
/// cases that do not produce expected results.
103+
///
104+
/// See also:
105+
/// * [baseline], which will position the bullet and list item on the
106+
/// baseline.
107+
start,
108+
}
109+
75110
/// A base class for widgets that parse and display Markdown.
76111
///
77112
/// Supports all standard Markdown from the original
@@ -102,9 +137,12 @@ abstract class MarkdownWidget extends StatefulWidget {
102137
this.checkboxBuilder,
103138
this.builders = const {},
104139
this.fitContent = false,
140+
this.listItemCrossAxisAlignment =
141+
MarkdownListItemCrossAxisAlignment.baseline,
105142
}) : assert(data != null),
106143
assert(selectable != null),
107144
assert(builders != null),
145+
assert(listItemCrossAxisAlignment != null),
108146
super(key: key);
109147

110148
/// The Markdown to display.
@@ -169,6 +207,13 @@ abstract class MarkdownWidget extends StatefulWidget {
169207
/// Whether to allow the widget to fit the child content.
170208
final bool fitContent;
171209

210+
/// Controls the cross axis alignment for the bullet and list item content
211+
/// in lists.
212+
///
213+
/// Defaults to [MarkdownListItemCrossAxisAlignment.baseline], which
214+
/// does not allow for intrinsic height measurements.
215+
final MarkdownListItemCrossAxisAlignment listItemCrossAxisAlignment;
216+
172217
/// Subclasses should override this function to display the given children,
173218
/// which are the parsed representation of [data].
174219
@protected
@@ -234,6 +279,7 @@ class _MarkdownWidgetState extends State<MarkdownWidget>
234279
checkboxBuilder: widget.checkboxBuilder,
235280
builders: widget.builders,
236281
fitContent: widget.fitContent,
282+
listItemCrossAxisAlignment: widget.listItemCrossAxisAlignment,
237283
);
238284

239285
_children = builder.build(astNodes);
@@ -298,6 +344,8 @@ class MarkdownBody extends MarkdownWidget {
298344
MarkdownImageBuilder imageBuilder,
299345
MarkdownCheckboxBuilder checkboxBuilder,
300346
Map<String, MarkdownElementBuilder> builders = const {},
347+
MarkdownListItemCrossAxisAlignment listItemCrossAxisAlignment =
348+
MarkdownListItemCrossAxisAlignment.baseline,
301349
this.shrinkWrap = true,
302350
this.fitContent = true,
303351
}) : super(
@@ -315,6 +363,7 @@ class MarkdownBody extends MarkdownWidget {
315363
imageBuilder: imageBuilder,
316364
checkboxBuilder: checkboxBuilder,
317365
builders: builders,
366+
listItemCrossAxisAlignment: listItemCrossAxisAlignment,
318367
);
319368

320369
/// See [ScrollView.shrinkWrap]
@@ -361,6 +410,8 @@ class Markdown extends MarkdownWidget {
361410
MarkdownImageBuilder imageBuilder,
362411
MarkdownCheckboxBuilder checkboxBuilder,
363412
Map<String, MarkdownElementBuilder> builders = const {},
413+
MarkdownListItemCrossAxisAlignment listItemCrossAxisAlignment =
414+
MarkdownListItemCrossAxisAlignment.baseline,
364415
this.padding = const EdgeInsets.all(16.0),
365416
this.controller,
366417
this.physics,
@@ -380,6 +431,7 @@ class Markdown extends MarkdownWidget {
380431
imageBuilder: imageBuilder,
381432
checkboxBuilder: checkboxBuilder,
382433
builders: builders,
434+
listItemCrossAxisAlignment: listItemCrossAxisAlignment,
383435
);
384436

385437
/// The amount of space by which to inset the children.

‎packages/flutter_markdown/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
33
including text styles, tables, links, and more, from plain text data
44
formatted with simple Markdown tags.
55
homepage: https://github.com/flutter/flutter_markdown
6-
version: 0.5.1
6+
version: 0.5.2
77

88
dependencies:
99
flutter:

0 commit comments

Comments
 (0)