mirror of
https://github.com/flutter/packages.git
synced 2025-07-04 09:38:17 +08:00
Fix text alignment when formatting is involved (#209)
This commit is contained in:
@ -334,7 +334,7 @@ class MarkdownBuilder implements md.NodeVisitor {
|
||||
}
|
||||
}
|
||||
Widget child = _buildTableCell(
|
||||
_mergeInlineChildren(current.children),
|
||||
_mergeInlineChildren(current.children, align),
|
||||
textAlign: align,
|
||||
);
|
||||
_tables.single.rows.last.children.add(child);
|
||||
@ -449,13 +449,18 @@ class MarkdownBuilder implements md.NodeVisitor {
|
||||
if (_inlines.isEmpty) return;
|
||||
|
||||
WrapAlignment blockAlignment = WrapAlignment.start;
|
||||
TextAlign textAlign = TextAlign.start;
|
||||
if (_isBlockTag(_currentBlockTag)) {
|
||||
blockAlignment = _wrapAlignmentForBlockTag(_currentBlockTag);
|
||||
textAlign = _textAlignForBlockTag(_currentBlockTag);
|
||||
}
|
||||
|
||||
final _InlineElement inline = _inlines.single;
|
||||
if (inline.children.isNotEmpty) {
|
||||
List<Widget> mergedInlines = _mergeInlineChildren(inline.children);
|
||||
List<Widget> mergedInlines = _mergeInlineChildren(
|
||||
inline.children,
|
||||
textAlign,
|
||||
);
|
||||
final Wrap wrap = Wrap(
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: mergedInlines,
|
||||
@ -467,7 +472,10 @@ class MarkdownBuilder implements md.NodeVisitor {
|
||||
}
|
||||
|
||||
/// Merges adjacent [TextSpan] children
|
||||
List<Widget> _mergeInlineChildren(List<Widget> children) {
|
||||
List<Widget> _mergeInlineChildren(
|
||||
List<Widget> children,
|
||||
TextAlign textAlign,
|
||||
) {
|
||||
List<Widget> mergedTexts = <Widget>[];
|
||||
for (Widget child in children) {
|
||||
if (mergedTexts.isNotEmpty &&
|
||||
@ -480,7 +488,10 @@ class MarkdownBuilder implements md.NodeVisitor {
|
||||
: [previousTextSpan];
|
||||
children.add(child.text);
|
||||
TextSpan mergedSpan = TextSpan(children: children);
|
||||
mergedTexts.add(_buildRichText(mergedSpan));
|
||||
mergedTexts.add(_buildRichText(
|
||||
mergedSpan,
|
||||
textAlign: textAlign,
|
||||
));
|
||||
} else if (mergedTexts.isNotEmpty &&
|
||||
mergedTexts.last is SelectableText &&
|
||||
child is SelectableText) {
|
||||
@ -494,6 +505,7 @@ class MarkdownBuilder implements md.NodeVisitor {
|
||||
mergedTexts.add(
|
||||
_buildRichText(
|
||||
mergedSpan,
|
||||
textAlign: textAlign,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
@ -544,6 +556,7 @@ class MarkdownBuilder implements md.NodeVisitor {
|
||||
return SelectableText.rich(
|
||||
text,
|
||||
//textScaleFactor: styleSheet.textScaleFactor,
|
||||
textAlign: textAlign ?? TextAlign.start,
|
||||
);
|
||||
} else {
|
||||
return RichText(
|
||||
|
@ -740,6 +740,38 @@ void main() {
|
||||
reason: "default alignment if none is set in stylesheet");
|
||||
});
|
||||
|
||||
testWidgets('should align formatted text', (WidgetTester tester) async {
|
||||
final ThemeData theme = ThemeData.light().copyWith(textTheme: textTheme);
|
||||
final MarkdownStyleSheet style = MarkdownStyleSheet.fromTheme(theme).copyWith(
|
||||
textAlign: WrapAlignment.spaceBetween,
|
||||
);
|
||||
|
||||
const String data = 'hello __my formatted text__';
|
||||
await tester.pumpWidget(_boilerplate(MarkdownBody(data: data, styleSheet: style)));
|
||||
|
||||
final RichText text = tester.widgetList(find.byType(RichText)).single;
|
||||
expect(text.textAlign, TextAlign.justify);
|
||||
});
|
||||
|
||||
testWidgets('should align selectable text', (WidgetTester tester) async {
|
||||
final ThemeData theme = ThemeData.light().copyWith(textTheme: textTheme);
|
||||
final MarkdownStyleSheet style = MarkdownStyleSheet.fromTheme(theme).copyWith(
|
||||
textAlign: WrapAlignment.spaceBetween,
|
||||
);
|
||||
|
||||
const String data = 'hello __my formatted text__';
|
||||
await tester.pumpWidget(
|
||||
_boilerplate(
|
||||
MediaQuery(
|
||||
data: MediaQueryData(),
|
||||
child: MarkdownBody(data: data, styleSheet: style, selectable: true),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final SelectableText text = tester.widgetList(find.byType(SelectableText)).single;
|
||||
expect(text.textAlign, TextAlign.justify);
|
||||
});
|
||||
}
|
||||
|
||||
void _expectWidgetTypes(Iterable<Widget> widgets, List<Type> expected) {
|
||||
|
Reference in New Issue
Block a user