Bugfixes for MarkdownStyleSheet and textScaleFactor (#243)

This commit is contained in:
Tim Bierbaum
2020-08-04 03:57:48 +02:00
committed by GitHub
parent 7a165b214b
commit 35f090bc4a
6 changed files with 87 additions and 28 deletions

View File

@ -1,3 +1,8 @@
## 0.4.3
* Fix merging of `MarkdownStyleSheets`
* Fix if not provided in `MarkdownStyleSheet` textScaleFactor uses default value of 1.0 instead using the textScaleFactor of the nearest MediaQuery
## 0.4.2
* Fix parsing of image caption & alt attributes

View File

@ -43,17 +43,25 @@ final MarkdownStyleSheet Function(BuildContext, MarkdownStyleSheetBaseTheme)
BuildContext context,
MarkdownStyleSheetBaseTheme baseTheme,
) {
MarkdownStyleSheet result;
switch (baseTheme) {
case MarkdownStyleSheetBaseTheme.platform:
return (Platform.isIOS || Platform.isMacOS)
result = (Platform.isIOS || Platform.isMacOS)
? MarkdownStyleSheet.fromCupertinoTheme(CupertinoTheme.of(context))
: MarkdownStyleSheet.fromTheme(Theme.of(context));
break;
case MarkdownStyleSheetBaseTheme.cupertino:
return MarkdownStyleSheet.fromCupertinoTheme(CupertinoTheme.of(context));
result =
MarkdownStyleSheet.fromCupertinoTheme(CupertinoTheme.of(context));
break;
case MarkdownStyleSheetBaseTheme.material:
default:
return MarkdownStyleSheet.fromTheme(Theme.of(context));
result = MarkdownStyleSheet.fromTheme(Theme.of(context));
}
return result.copyWith(
textScaleFactor: MediaQuery.textScaleFactorOf(context),
);
};
Widget _handleDataSchemeUri(Uri uri, final double width, final double height) {

View File

@ -43,18 +43,26 @@ final MarkdownStyleSheet Function(BuildContext, MarkdownStyleSheetBaseTheme)
BuildContext context,
MarkdownStyleSheetBaseTheme baseTheme,
) {
MarkdownStyleSheet result;
switch (baseTheme) {
case MarkdownStyleSheetBaseTheme.platform:
final String userAgent = window.navigator.userAgent;
return userAgent.contains('Mac OS X')
result = userAgent.contains('Mac OS X')
? MarkdownStyleSheet.fromCupertinoTheme(CupertinoTheme.of(context))
: MarkdownStyleSheet.fromTheme(Theme.of(context));
break;
case MarkdownStyleSheetBaseTheme.cupertino:
return MarkdownStyleSheet.fromCupertinoTheme(CupertinoTheme.of(context));
result =
MarkdownStyleSheet.fromCupertinoTheme(CupertinoTheme.of(context));
break;
case MarkdownStyleSheetBaseTheme.material:
default:
return MarkdownStyleSheet.fromTheme(Theme.of(context));
result = MarkdownStyleSheet.fromTheme(Theme.of(context));
}
return result.copyWith(
textScaleFactor: MediaQuery.textScaleFactorOf(context),
);
};
Widget _handleDataSchemeUri(Uri uri, final double width, final double height) {

View File

@ -577,7 +577,7 @@ class MarkdownBuilder implements md.NodeVisitor {
if (selectable) {
return SelectableText.rich(
text,
//textScaleFactor: styleSheet.textScaleFactor,
textScaleFactor: styleSheet.textScaleFactor,
textAlign: textAlign ?? TextAlign.start,
);
} else {

View File

@ -50,7 +50,7 @@ class MarkdownStyleSheet {
this.orderedListAlign = WrapAlignment.start,
this.blockquoteAlign = WrapAlignment.start,
this.codeblockAlign = WrapAlignment.start,
this.textScaleFactor = 1.0,
this.textScaleFactor,
}) : _styles = <String, TextStyle>{
'a': a,
'p': p,
@ -130,6 +130,7 @@ class MarkdownStyleSheet {
),
),
),
// textScaleFactor:
);
}
@ -394,26 +395,26 @@ class MarkdownStyleSheet {
MarkdownStyleSheet merge(MarkdownStyleSheet other) {
if (other == null) return this;
return copyWith(
a: other.a,
p: other.p,
code: other.code,
h1: other.h1,
h2: other.h2,
h3: other.h3,
h4: other.h4,
h5: other.h5,
h6: other.h6,
em: other.em,
strong: other.strong,
del: other.del,
blockquote: other.blockquote,
img: other.img,
checkbox: other.checkbox,
a: a.merge(other.a),
p: p.merge(other.p),
code: code.merge(other.code),
h1: h1.merge(other.h1),
h2: h2.merge(other.h2),
h3: h3.merge(other.h3),
h4: h4.merge(other.h4),
h5: h5.merge(other.h5),
h6: h6.merge(other.h6),
em: em.merge(other.em),
strong: strong.merge(other.strong),
del: del.merge(other.del),
blockquote: blockquote.merge(other.blockquote),
img: img.merge(other.img),
checkbox: checkbox.merge(other.checkbox),
blockSpacing: other.blockSpacing,
listIndent: other.listIndent,
listBullet: other.listBullet,
tableHead: other.tableHead,
tableBody: other.tableBody,
listBullet: listBullet.merge(other.listBullet),
tableHead: tableHead.merge(other.tableHead),
tableBody: tableBody.merge(other.tableBody),
tableHeadAlign: other.tableHeadAlign,
tableBorder: other.tableBorder,
tableColumnWidth: other.tableColumnWidth,

View File

@ -779,6 +779,39 @@ void main() {
final RichText richText = tester.widget(find.byType(RichText));
expect(richText.textScaleFactor, 2.0);
});
testWidgets(' - should use MediaQuery textScaleFactor in RichText',
(WidgetTester tester) async {
await tester.pumpWidget(_boilerplate(
MediaQuery(
data: MediaQueryData(textScaleFactor: 2.0),
child: MarkdownBody(
data: 'Hello',
),
),
));
final RichText richText = tester.widget(find.byType(RichText));
expect(richText.textScaleFactor, 2.0);
});
testWidgets(
' - should use MediaQuery textScaleFactor in SelectableText.rich',
(WidgetTester tester) async {
await tester.pumpWidget(_boilerplate(
MediaQuery(
data: MediaQueryData(textScaleFactor: 2.0),
child: MarkdownBody(
data: 'Hello',
selectable: true,
),
),
));
final SelectableText selectableText =
tester.widget(find.byType(SelectableText));
expect(selectableText.textScaleFactor, 2.0);
});
});
group('Custom builders', () {
@ -995,11 +1028,15 @@ void main() {
testWidgets('merge', (WidgetTester tester) async {
final ThemeData theme = ThemeData.light().copyWith(textTheme: textTheme);
final MarkdownStyleSheet style1 = MarkdownStyleSheet.fromTheme(theme);
final MarkdownStyleSheet style2 =
MarkdownStyleSheet(p: TextStyle(color: Colors.red));
final MarkdownStyleSheet style2 = MarkdownStyleSheet(
p: TextStyle(color: Colors.red),
blockquote: TextStyle(fontSize: 16),
);
final MarkdownStyleSheet merged = style1.merge(style2);
expect(merged.p.color, Colors.red);
expect(merged.blockquote.fontSize, 16);
expect(merged.blockquote.color, theme.textTheme.bodyText2.color);
});
testWidgets('create based on which theme', (WidgetTester tester) async {