Skip to content

Commit f8ab53b

Browse files
stuartmorgan-garc-yong
authored andcommitted
[flutter_markdown] Replace deprecated API (flutter#6134)
* 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
1 parent 340c7f1 commit f8ab53b

11 files changed

+134
-31
lines changed

packages/flutter_markdown/CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
## NEXT
1+
## 0.6.20
22

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

59
## 0.6.19
610

@@ -44,7 +48,7 @@
4448

4549
* Introduces a new `MarkdownElementBuilder.visitElementAfterWithContext()` method passing the widget `BuildContext` and
4650
the parent text's `TextStyle`.
47-
51+
4852
## 0.6.16
4953

5054
* Adds `tableVerticalAlignment` property to allow aligning table cells vertically.

packages/flutter_markdown/example/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ description: Demonstrates how to use the flutter_markdown package.
33
publish_to: none
44

55
environment:
6-
sdk: ^3.1.0
7-
flutter: ">=3.13.0"
6+
sdk: ^3.2.0
7+
flutter: ">=3.16.0"
88

99
dependencies:
1010
flutter:

packages/flutter_markdown/lib/src/_functions_io.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ final MarkdownStyleSheet Function(BuildContext, MarkdownStyleSheetBaseTheme?)
6464
}
6565

6666
return result.copyWith(
67-
textScaleFactor: MediaQuery.textScaleFactorOf(context),
67+
textScaler: MediaQuery.textScalerOf(context),
6868
);
6969
};
7070

packages/flutter_markdown/lib/src/_functions_web.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ final MarkdownStyleSheet Function(BuildContext, MarkdownStyleSheetBaseTheme?)
6666
}
6767

6868
return result.copyWith(
69-
textScaleFactor: MediaQuery.textScaleFactorOf(context),
69+
textScaler: MediaQuery.textScalerOf(context),
7070
);
7171
};
7272

packages/flutter_markdown/lib/src/builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,15 +867,15 @@ class MarkdownBuilder implements md.NodeVisitor {
867867
if (selectable) {
868868
return SelectableText.rich(
869869
text!,
870-
textScaleFactor: styleSheet.textScaleFactor,
870+
textScaler: styleSheet.textScaler,
871871
textAlign: textAlign ?? TextAlign.start,
872872
onTap: onTapText,
873873
key: k,
874874
);
875875
} else {
876876
return Text.rich(
877877
text!,
878-
textScaleFactor: styleSheet.textScaleFactor,
878+
textScaler: styleSheet.textScaler,
879879
textAlign: textAlign ?? TextAlign.start,
880880
key: k,
881881
);

packages/flutter_markdown/lib/src/style_sheet.dart

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,19 @@ class MarkdownStyleSheet {
5959
this.orderedListAlign = WrapAlignment.start,
6060
this.blockquoteAlign = WrapAlignment.start,
6161
this.codeblockAlign = WrapAlignment.start,
62-
this.textScaleFactor,
63-
}) : _styles = <String, TextStyle?>{
62+
@Deprecated('Use textScaler instead.') this.textScaleFactor,
63+
TextScaler? textScaler,
64+
}) : assert(
65+
textScaler == null || textScaleFactor == null,
66+
'textScaleFactor is deprecated and cannot be specified when textScaler is specified.',
67+
),
68+
textScaler = textScaler ??
69+
// Internally, only textScaler is used, so convert the scale factor
70+
// to a linear scaler.
71+
(textScaleFactor == null
72+
? null
73+
: TextScaler.linear(textScaleFactor)),
74+
_styles = <String, TextStyle?>{
6475
'a': a,
6576
'p': p,
6677
'li': p,
@@ -380,8 +391,19 @@ class MarkdownStyleSheet {
380391
WrapAlignment? orderedListAlign,
381392
WrapAlignment? blockquoteAlign,
382393
WrapAlignment? codeblockAlign,
383-
double? textScaleFactor,
394+
@Deprecated('Use textScaler instead.') double? textScaleFactor,
395+
TextScaler? textScaler,
384396
}) {
397+
assert(
398+
textScaler == null || textScaleFactor == null,
399+
'textScaleFactor is deprecated and cannot be specified when textScaler is specified.',
400+
);
401+
// If either of textScaler or textScaleFactor is non-null, pass null for the
402+
// other instead of the previous value, since only one is allowed.
403+
final TextScaler? newTextScaler =
404+
textScaler ?? (textScaleFactor == null ? this.textScaler : null);
405+
final double? nextTextScaleFactor =
406+
textScaleFactor ?? (textScaler == null ? this.textScaleFactor : null);
385407
return MarkdownStyleSheet(
386408
a: a ?? this.a,
387409
p: p ?? this.p,
@@ -435,7 +457,8 @@ class MarkdownStyleSheet {
435457
orderedListAlign: orderedListAlign ?? this.orderedListAlign,
436458
blockquoteAlign: blockquoteAlign ?? this.blockquoteAlign,
437459
codeblockAlign: codeblockAlign ?? this.codeblockAlign,
438-
textScaleFactor: textScaleFactor ?? this.textScaleFactor,
460+
textScaler: newTextScaler,
461+
textScaleFactor: nextTextScaleFactor,
439462
);
440463
}
441464

@@ -497,6 +520,11 @@ class MarkdownStyleSheet {
497520
blockquoteAlign: other.blockquoteAlign,
498521
codeblockAlign: other.codeblockAlign,
499522
textScaleFactor: other.textScaleFactor,
523+
// Only one of textScaler and textScaleFactor can be passed. If
524+
// other.textScaleFactor is non-null, then the sheet was created with a
525+
// textScaleFactor and the textScaler was derived from that, so should be
526+
// ignored so that the textScaleFactor continues to be set.
527+
textScaler: other.textScaleFactor == null ? other.textScaler : null,
500528
);
501529
}
502530

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

653-
/// The text scale factor to use in textual elements
681+
/// The text scaler to use in textual elements.
682+
final TextScaler? textScaler;
683+
684+
/// The text scale factor to use in textual elements.
685+
///
686+
/// This will be non-null only if the sheet was created with the deprecated
687+
/// [textScaleFactor] instead of [textScaler].
688+
@Deprecated('Use textScaler instead.')
654689
final double? textScaleFactor;
655690

656691
/// A [Map] from element name to the corresponding [TextStyle] object.
@@ -717,7 +752,7 @@ class MarkdownStyleSheet {
717752
other.orderedListAlign == orderedListAlign &&
718753
other.blockquoteAlign == blockquoteAlign &&
719754
other.codeblockAlign == codeblockAlign &&
720-
other.textScaleFactor == textScaleFactor;
755+
other.textScaler == textScaler;
721756
}
722757

723758
@override
@@ -774,6 +809,7 @@ class MarkdownStyleSheet {
774809
orderedListAlign,
775810
blockquoteAlign,
776811
codeblockAlign,
812+
textScaler,
777813
textScaleFactor,
778814
]);
779815
}

packages/flutter_markdown/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ description: A Markdown renderer for Flutter. Create rich text output,
44
formatted with simple Markdown tags.
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
7-
version: 0.6.19
7+
version: 0.6.20
88

99
environment:
10-
sdk: ^3.1.0
11-
flutter: ">=3.13.0"
10+
sdk: ^3.2.0
11+
flutter: ">=3.16.0"
1212

1313
dependencies:
1414
flutter:

packages/flutter_markdown/test/all.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import 'selection_area_compatibility_test.dart' as selection_area_test;
1818
import 'style_sheet_test.dart' as style_sheet_test;
1919
import 'table_test.dart' as table_test;
2020
import 'text_alignment_test.dart' as text_alignment_test;
21-
import 'text_scale_factor_test.dart' as text_scale_factor;
21+
import 'text_scaler_test.dart' as text_scaler;
2222
import 'text_test.dart' as text_test;
2323
import 'uri_test.dart' as uri_test;
2424

@@ -40,6 +40,6 @@ void main() {
4040
table_test.defineTests();
4141
text_test.defineTests();
4242
text_alignment_test.defineTests();
43-
text_scale_factor.defineTests();
43+
text_scaler.defineTests();
4444
uri_test.defineTests();
4545
}

packages/flutter_markdown/test/style_sheet_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,5 +398,65 @@ void defineTests() {
398398
);
399399
},
400400
);
401+
402+
testWidgets(
403+
'deprecated textScaleFactor is converted to linear scaler',
404+
(WidgetTester tester) async {
405+
const double scaleFactor = 2.0;
406+
final MarkdownStyleSheet style = MarkdownStyleSheet(
407+
textScaleFactor: scaleFactor,
408+
);
409+
410+
expect(style.textScaler, const TextScaler.linear(scaleFactor));
411+
expect(style.textScaleFactor, scaleFactor);
412+
},
413+
);
414+
415+
testWidgets(
416+
'deprecated textScaleFactor is null when a scaler is provided',
417+
(WidgetTester tester) async {
418+
const TextScaler scaler = TextScaler.linear(2.0);
419+
final MarkdownStyleSheet style = MarkdownStyleSheet(
420+
textScaler: scaler,
421+
);
422+
423+
expect(style.textScaler, scaler);
424+
expect(style.textScaleFactor, null);
425+
},
426+
);
427+
428+
testWidgets(
429+
'copyWith textScaler overwrites both textScaler and textScaleFactor',
430+
(WidgetTester tester) async {
431+
final MarkdownStyleSheet original = MarkdownStyleSheet(
432+
textScaleFactor: 2.0,
433+
);
434+
435+
const TextScaler newScaler = TextScaler.linear(3.0);
436+
final MarkdownStyleSheet copy = original.copyWith(
437+
textScaler: newScaler,
438+
);
439+
440+
expect(copy.textScaler, newScaler);
441+
expect(copy.textScaleFactor, null);
442+
},
443+
);
444+
445+
testWidgets(
446+
'copyWith textScaleFactor overwrites both textScaler and textScaleFactor',
447+
(WidgetTester tester) async {
448+
final MarkdownStyleSheet original = MarkdownStyleSheet(
449+
textScaleFactor: 2.0,
450+
);
451+
452+
const double newScaleFactor = 3.0;
453+
final MarkdownStyleSheet copy = original.copyWith(
454+
textScaleFactor: newScaleFactor,
455+
);
456+
457+
expect(copy.textScaler, const TextScaler.linear(newScaleFactor));
458+
expect(copy.textScaleFactor, newScaleFactor);
459+
},
460+
);
401461
});
402462
}

packages/flutter_markdown/test/text_scale_factor_test.dart renamed to packages/flutter_markdown/test/text_scaler_test.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,35 @@ import 'utils.dart';
1010
void main() => defineTests();
1111

1212
void defineTests() {
13-
group('Text Scale Factor', () {
13+
group('Text Scaler', () {
1414
testWidgets(
15-
'should use style textScaleFactor in RichText',
15+
'should use style textScaler in RichText',
1616
(WidgetTester tester) async {
17+
const TextScaler scaler = TextScaler.linear(2.0);
1718
const String data = 'Hello';
1819
await tester.pumpWidget(
1920
boilerplate(
2021
MarkdownBody(
21-
styleSheet: MarkdownStyleSheet(textScaleFactor: 2.0),
22+
styleSheet: MarkdownStyleSheet(textScaler: scaler),
2223
data: data,
2324
),
2425
),
2526
);
2627

2728
final RichText richText = tester.widget(find.byType(RichText));
28-
expect(richText.textScaleFactor, 2.0);
29+
expect(richText.textScaler, scaler);
2930
},
3031
);
3132

3233
testWidgets(
33-
'should use MediaQuery textScaleFactor in RichText',
34+
'should use MediaQuery textScaler in RichText',
3435
(WidgetTester tester) async {
36+
const TextScaler scaler = TextScaler.linear(2.0);
3537
const String data = 'Hello';
3638
await tester.pumpWidget(
3739
boilerplate(
3840
const MediaQuery(
39-
data: MediaQueryData(textScaleFactor: 2.0),
41+
data: MediaQueryData(textScaler: scaler),
4042
child: MarkdownBody(
4143
data: data,
4244
),
@@ -45,18 +47,19 @@ void defineTests() {
4547
);
4648

4749
final RichText richText = tester.widget(find.byType(RichText));
48-
expect(richText.textScaleFactor, 2.0);
50+
expect(richText.textScaler, scaler);
4951
},
5052
);
5153

5254
testWidgets(
53-
'should use MediaQuery textScaleFactor in SelectableText.rich',
55+
'should use MediaQuery textScaler in SelectableText.rich',
5456
(WidgetTester tester) async {
57+
const TextScaler scaler = TextScaler.linear(2.0);
5558
const String data = 'Hello';
5659
await tester.pumpWidget(
5760
boilerplate(
5861
const MediaQuery(
59-
data: MediaQueryData(textScaleFactor: 2.0),
62+
data: MediaQueryData(textScaler: scaler),
6063
child: MarkdownBody(
6164
data: data,
6265
selectable: true,
@@ -67,7 +70,7 @@ void defineTests() {
6770

6871
final SelectableText selectableText =
6972
tester.widget(find.byType(SelectableText));
70-
expect(selectableText.textScaleFactor, 2.0);
73+
expect(selectableText.textScaler, scaler);
7174
},
7275
);
7376
});

packages/flutter_markdown/test/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void expectLinkTap(MarkdownLink? actual, MarkdownLink expected) {
169169
}
170170

171171
String dumpRenderView() {
172-
return WidgetsBinding.instance.renderViewElement!.toStringDeep().replaceAll(
172+
return WidgetsBinding.instance.rootElement!.toStringDeep().replaceAll(
173173
RegExp(r'SliverChildListDelegate#\d+', multiLine: true),
174174
'SliverChildListDelegate',
175175
);

0 commit comments

Comments
 (0)