Skip to content

[flutter_markdown] Replace deprecated API #6134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/flutter_markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.6.20

* Adds `textScaler` to `MarkdownStyleSheet`, and deprecates `textScaleFactor`.
* Clients using `textScaleFactor: someFactor` should replace it with
`TextScaler.linear(someFactor)` to preserve behavior.
* Remove use of deprecated `textScaleFactor` methods from the Flutter framework.

## 0.6.19

* Replaces `RichText` with `Text.rich` so the widget can work with `SelectionArea` when `selectable` is set to false.
Expand Down Expand Up @@ -40,7 +47,7 @@

* Introduces a new `MarkdownElementBuilder.visitElementAfterWithContext()` method passing the widget `BuildContext` and
the parent text's `TextStyle`.

## 0.6.16

* Adds `tableVerticalAlignment` property to allow aligning table cells vertically.
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_markdown/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ description: Demonstrates how to use the flutter_markdown package.
publish_to: none

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"
sdk: ^3.2.0
flutter: ">=3.16.0"

dependencies:
flutter:
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_markdown/lib/src/_functions_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final MarkdownStyleSheet Function(BuildContext, MarkdownStyleSheetBaseTheme?)
}

return result.copyWith(
textScaleFactor: MediaQuery.textScaleFactorOf(context),
textScaler: MediaQuery.textScalerOf(context),
);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_markdown/lib/src/_functions_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ final MarkdownStyleSheet Function(BuildContext, MarkdownStyleSheetBaseTheme?)
}

return result.copyWith(
textScaleFactor: MediaQuery.textScaleFactorOf(context),
textScaler: MediaQuery.textScalerOf(context),
);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_markdown/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -867,15 +867,15 @@ class MarkdownBuilder implements md.NodeVisitor {
if (selectable) {
return SelectableText.rich(
text!,
textScaleFactor: styleSheet.textScaleFactor,
textScaler: styleSheet.textScaler,
textAlign: textAlign ?? TextAlign.start,
onTap: onTapText,
key: k,
);
} else {
return Text.rich(
text!,
textScaleFactor: styleSheet.textScaleFactor,
textScaler: styleSheet.textScaler,
textAlign: textAlign ?? TextAlign.start,
key: k,
);
Expand Down
48 changes: 42 additions & 6 deletions packages/flutter_markdown/lib/src/style_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,19 @@ class MarkdownStyleSheet {
this.orderedListAlign = WrapAlignment.start,
this.blockquoteAlign = WrapAlignment.start,
this.codeblockAlign = WrapAlignment.start,
this.textScaleFactor,
}) : _styles = <String, TextStyle?>{
@Deprecated('Use textScaler instead.') this.textScaleFactor,
TextScaler? textScaler,
}) : assert(
textScaler == null || textScaleFactor == null,
'textScaleFactor is deprecated and cannot be specified when textScaler is specified.',
),
textScaler = textScaler ??
// Internally, only textScaler is used, so convert the scale factor
// to a linear scaler.
(textScaleFactor == null
? null
: TextScaler.linear(textScaleFactor)),
_styles = <String, TextStyle?>{
'a': a,
'p': p,
'li': p,
Expand Down Expand Up @@ -380,8 +391,19 @@ class MarkdownStyleSheet {
WrapAlignment? orderedListAlign,
WrapAlignment? blockquoteAlign,
WrapAlignment? codeblockAlign,
double? textScaleFactor,
@Deprecated('Use textScaler instead.') double? textScaleFactor,
TextScaler? textScaler,
}) {
assert(
textScaler == null || textScaleFactor == null,
'textScaleFactor is deprecated and cannot be specified when textScaler is specified.',
);
// If either of textScaler or textScaleFactor is non-null, pass null for the
// other instead of the previous value, since only one is allowed.
final TextScaler? newTextScaler =
textScaler ?? (textScaleFactor == null ? this.textScaler : null);
final double? nextTextScaleFactor =
textScaleFactor ?? (textScaler == null ? this.textScaleFactor : null);
return MarkdownStyleSheet(
a: a ?? this.a,
p: p ?? this.p,
Expand Down Expand Up @@ -435,7 +457,8 @@ class MarkdownStyleSheet {
orderedListAlign: orderedListAlign ?? this.orderedListAlign,
blockquoteAlign: blockquoteAlign ?? this.blockquoteAlign,
codeblockAlign: codeblockAlign ?? this.codeblockAlign,
textScaleFactor: textScaleFactor ?? this.textScaleFactor,
textScaler: newTextScaler,
textScaleFactor: nextTextScaleFactor,
);
}

Expand Down Expand Up @@ -497,6 +520,11 @@ class MarkdownStyleSheet {
blockquoteAlign: other.blockquoteAlign,
codeblockAlign: other.codeblockAlign,
textScaleFactor: other.textScaleFactor,
// Only one of textScaler and textScaleFactor can be passed. If
// other.textScaleFactor is non-null, then the sheet was created with a
// textScaleFactor and the textScaler was derived from that, so should be
// ignored so that the textScaleFactor continues to be set.
textScaler: other.textScaleFactor == null ? other.textScaler : null,
);
}

Expand Down Expand Up @@ -650,7 +678,14 @@ class MarkdownStyleSheet {
/// The [WrapAlignment] to use for a code block. Defaults to start.
final WrapAlignment codeblockAlign;

/// The text scale factor to use in textual elements
/// The text scaler to use in textual elements.
final TextScaler? textScaler;

/// The text scale factor to use in textual elements.
///
/// This will be non-null only if the sheet was created with the deprecated
/// [textScaleFactor] instead of [textScaler].
@Deprecated('Use textScaler instead.')
final double? textScaleFactor;

/// A [Map] from element name to the corresponding [TextStyle] object.
Expand Down Expand Up @@ -717,7 +752,7 @@ class MarkdownStyleSheet {
other.orderedListAlign == orderedListAlign &&
other.blockquoteAlign == blockquoteAlign &&
other.codeblockAlign == codeblockAlign &&
other.textScaleFactor == textScaleFactor;
other.textScaler == textScaler;
}

@override
Expand Down Expand Up @@ -774,6 +809,7 @@ class MarkdownStyleSheet {
orderedListAlign,
blockquoteAlign,
codeblockAlign,
textScaler,
textScaleFactor,
]);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter_markdown/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ 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.6.19
version: 0.6.20

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"
sdk: ^3.2.0
flutter: ">=3.16.0"

dependencies:
flutter:
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_markdown/test/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'selection_area_compatibility_test.dart' as selection_area_test;
import 'style_sheet_test.dart' as style_sheet_test;
import 'table_test.dart' as table_test;
import 'text_alignment_test.dart' as text_alignment_test;
import 'text_scale_factor_test.dart' as text_scale_factor;
import 'text_scaler_test.dart' as text_scaler;
import 'text_test.dart' as text_test;
import 'uri_test.dart' as uri_test;

Expand All @@ -40,6 +40,6 @@ void main() {
table_test.defineTests();
text_test.defineTests();
text_alignment_test.defineTests();
text_scale_factor.defineTests();
text_scaler.defineTests();
uri_test.defineTests();
}
60 changes: 60 additions & 0 deletions packages/flutter_markdown/test/style_sheet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,65 @@ void defineTests() {
);
},
);

testWidgets(
'deprecated textScaleFactor is converted to linear scaler',
(WidgetTester tester) async {
const double scaleFactor = 2.0;
final MarkdownStyleSheet style = MarkdownStyleSheet(
textScaleFactor: scaleFactor,
);

expect(style.textScaler, const TextScaler.linear(scaleFactor));
expect(style.textScaleFactor, scaleFactor);
},
);

testWidgets(
'deprecated textScaleFactor is null when a scaler is provided',
(WidgetTester tester) async {
const TextScaler scaler = TextScaler.linear(2.0);
final MarkdownStyleSheet style = MarkdownStyleSheet(
textScaler: scaler,
);

expect(style.textScaler, scaler);
expect(style.textScaleFactor, null);
},
);

testWidgets(
'copyWith textScaler overwrites both textScaler and textScaleFactor',
(WidgetTester tester) async {
final MarkdownStyleSheet original = MarkdownStyleSheet(
textScaleFactor: 2.0,
);

const TextScaler newScaler = TextScaler.linear(3.0);
final MarkdownStyleSheet copy = original.copyWith(
textScaler: newScaler,
);

expect(copy.textScaler, newScaler);
expect(copy.textScaleFactor, null);
},
);

testWidgets(
'copyWith textScaleFactor overwrites both textScaler and textScaleFactor',
(WidgetTester tester) async {
final MarkdownStyleSheet original = MarkdownStyleSheet(
textScaleFactor: 2.0,
);

const double newScaleFactor = 3.0;
final MarkdownStyleSheet copy = original.copyWith(
textScaleFactor: newScaleFactor,
);

expect(copy.textScaler, const TextScaler.linear(newScaleFactor));
expect(copy.textScaleFactor, newScaleFactor);
},
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@ import 'utils.dart';
void main() => defineTests();

void defineTests() {
group('Text Scale Factor', () {
group('Text Scaler', () {
testWidgets(
'should use style textScaleFactor in RichText',
'should use style textScaler in RichText',
(WidgetTester tester) async {
const TextScaler scaler = TextScaler.linear(2.0);
const String data = 'Hello';
await tester.pumpWidget(
boilerplate(
MarkdownBody(
styleSheet: MarkdownStyleSheet(textScaleFactor: 2.0),
styleSheet: MarkdownStyleSheet(textScaler: scaler),
data: data,
),
),
);

final RichText richText = tester.widget(find.byType(RichText));
expect(richText.textScaleFactor, 2.0);
expect(richText.textScaler, scaler);
},
);

testWidgets(
'should use MediaQuery textScaleFactor in RichText',
'should use MediaQuery textScaler in RichText',
(WidgetTester tester) async {
const TextScaler scaler = TextScaler.linear(2.0);
const String data = 'Hello';
await tester.pumpWidget(
boilerplate(
const MediaQuery(
data: MediaQueryData(textScaleFactor: 2.0),
data: MediaQueryData(textScaler: scaler),
child: MarkdownBody(
data: data,
),
Expand All @@ -45,18 +47,19 @@ void defineTests() {
);

final RichText richText = tester.widget(find.byType(RichText));
expect(richText.textScaleFactor, 2.0);
expect(richText.textScaler, scaler);
},
);

testWidgets(
'should use MediaQuery textScaleFactor in SelectableText.rich',
'should use MediaQuery textScaler in SelectableText.rich',
(WidgetTester tester) async {
const TextScaler scaler = TextScaler.linear(2.0);
const String data = 'Hello';
await tester.pumpWidget(
boilerplate(
const MediaQuery(
data: MediaQueryData(textScaleFactor: 2.0),
data: MediaQueryData(textScaler: scaler),
child: MarkdownBody(
data: data,
selectable: true,
Expand All @@ -67,7 +70,7 @@ void defineTests() {

final SelectableText selectableText =
tester.widget(find.byType(SelectableText));
expect(selectableText.textScaleFactor, 2.0);
expect(selectableText.textScaler, scaler);
},
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_markdown/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void expectLinkTap(MarkdownLink? actual, MarkdownLink expected) {
}

String dumpRenderView() {
return WidgetsBinding.instance.renderViewElement!.toStringDeep().replaceAll(
return WidgetsBinding.instance.rootElement!.toStringDeep().replaceAll(
RegExp(r'SliverChildListDelegate#\d+', multiLine: true),
'SliverChildListDelegate',
);
Expand Down