Skip to content

Commit

Permalink
[flutter_markdown] Replace deprecated API (flutter#6134)
Browse files Browse the repository at this point in the history
* Internally, removes use of the deprecated framework methods related to `textScaleFactor`, in favor of the newer `textScaler`.
* Plumbs that same change through the public API of this package, deprecating the style sheet's `textScaleFactor` and adding a `textScaler`.
* Updates the min Flutter SDK to 3.16 where the new APIs were added.
* Also updates test code that uses the deprecated `renderViewElement` to use `rootElement` instead.

Fixes flutter/flutter#143400
Fixes flutter/flutter#143448
  • Loading branch information
stuartmorgan authored Feb 20, 2024
1 parent 84ff11d commit 947e34c
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 31 deletions.
10 changes: 7 additions & 3 deletions packages/flutter_markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## NEXT
## 0.6.20

* Updates minimum supported SDK version to Flutter 3.13/Dart 3.1.
* Adds `textScaler` to `MarkdownStyleSheet`, and deprecates `textScaleFactor`.
* Clients using `textScaleFactor: someFactor` should replace it with
`TextScaler.linear(someFactor)` to preserve behavior.
* Removes use of deprecated Flutter framework `textScaleFactor` methods.
* Updates minimum supported SDK version to Flutter 3.16.

## 0.6.19

Expand Down Expand Up @@ -44,7 +48,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.1.0
flutter: ">=3.13.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.1.0
flutter: ">=3.13.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

0 comments on commit 947e34c

Please sign in to comment.