Skip to content

Commit 9b86d89

Browse files
authored
[rfw] Fix fontWeight not applied in release mode (flutter#11581)
Fixes the `fontWeight` property of RFW `TextStyle` (and `StrutStyle`) not being applied in release mode for `Text` widgets. Fixes flutter#180223 ## Root cause `ArgumentDecoders.textStyle` (and `strutStyle`) decoded `fontWeight` using `enumValue(FontWeight.values, ...)`, which matched the input string against `FontWeight.value.toString().split('.').last`. `FontWeight` is not a real Dart `enum`; it is a hand-crafted class with `static const` members. In AOT/release builds, `toString()` for such classes is not guaranteed to return the same human-readable name (tree-shaking can strip debug labels), so all matches silently fail and `null` is returned, causing Flutter to fall back to the default weight (`w400`). ## Fix Added a new `ArgumentDecoders.fontWeight` static method that maps strings (`"w100"`–`"w900"`, `"normal"`, `"bold"`) and integers (`100`–`900`) to the correct `FontWeight` constant using an explicit `switch`, without relying on `toString()`. Updated both `textStyle` and `strutStyle` to use this new decoder. ## Pre-Review Checklist
1 parent 34c0119 commit 9b86d89

4 files changed

Lines changed: 154 additions & 6 deletions

File tree

‎packages/rfw/CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## NEXT
1+
## 1.1.4
22

33
* Updates minimum supported SDK version to Flutter 3.38/Dart 3.10.
4+
* Fixes `fontWeight` not being applied in release mode for `Text` and `StrutStyle` widgets.
45

56
## 1.1.3
67

‎packages/rfw/lib/src/flutter/argument_decoders.dart‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,46 @@ class ArgumentDecoders {
632632
return null;
633633
}
634634

635+
/// Returns a [FontWeight] from the specified string or integer.
636+
///
637+
/// This does not use [FontWeight.toString], which is not guaranteed to be
638+
/// stable in release builds (since [FontWeight] is not a real Dart enum).
639+
///
640+
/// Supported string values: `"w100"` through `"w900"`, `"normal"` (mapped to
641+
/// [FontWeight.w400]), and `"bold"` (mapped to [FontWeight.w700]).
642+
///
643+
/// Supported integer values: `100` through `900` in increments of 100.
644+
static FontWeight? fontWeight(DataSource source, List<Object> key) {
645+
final int? numeric = source.v<int>(key);
646+
if (numeric != null) {
647+
switch (numeric) {
648+
case 100: return FontWeight.w100;
649+
case 200: return FontWeight.w200;
650+
case 300: return FontWeight.w300;
651+
case 400: return FontWeight.w400;
652+
case 500: return FontWeight.w500;
653+
case 600: return FontWeight.w600;
654+
case 700: return FontWeight.w700;
655+
case 800: return FontWeight.w800;
656+
case 900: return FontWeight.w900;
657+
}
658+
}
659+
switch (source.v<String>(key)) {
660+
case 'w100': return FontWeight.w100;
661+
case 'w200': return FontWeight.w200;
662+
case 'w300': return FontWeight.w300;
663+
case 'w400':
664+
case 'normal': return FontWeight.w400;
665+
case 'w500': return FontWeight.w500;
666+
case 'w600': return FontWeight.w600;
667+
case 'w700':
668+
case 'bold': return FontWeight.w700;
669+
case 'w800': return FontWeight.w800;
670+
case 'w900': return FontWeight.w900;
671+
}
672+
return null;
673+
}
674+
635675
/// Returns a [FontFeature] from the specified map.
636676
///
637677
/// The `feature` key is used as the font feature name (defaulting to the
@@ -1276,7 +1316,7 @@ class ArgumentDecoders {
12761316
/// following keys: 'fontFamily` (string), `fontFamilyFallback` ([list] of
12771317
/// [string]), `fontSize` (double), `height` (double), `leadingDistribution`
12781318
/// ([enumValue] of [TextLeadingDistribution]), `leading` (double),
1279-
/// `fontWeight` ([enumValue] of [FontWeight]), `fontStyle` ([enumValue] of
1319+
/// `fontWeight` ([fontWeight]), `fontStyle` ([enumValue] of
12801320
/// [FontStyle]), `forceStrutHeight` (boolean).
12811321
static StrutStyle? strutStyle(DataSource source, List<Object> key) {
12821322
if (!source.isMap(key)) {
@@ -1289,7 +1329,7 @@ class ArgumentDecoders {
12891329
height: source.v<double>([...key, 'height']),
12901330
leadingDistribution: enumValue<TextLeadingDistribution>(TextLeadingDistribution.values, source, [...key, 'leadingDistribution']),
12911331
leading: source.v<double>([...key, 'leading']),
1292-
fontWeight: enumValue<FontWeight>(FontWeight.values, source, [...key, 'fontWeight']),
1332+
fontWeight: fontWeight(source, [...key, 'fontWeight']),
12931333
fontStyle: enumValue<FontStyle>(FontStyle.values, source, [...key, 'fontStyle']),
12941334
forceStrutHeight: source.v<bool>([...key, 'forceStrutHeight']),
12951335
);
@@ -1350,7 +1390,7 @@ class ArgumentDecoders {
13501390
///
13511391
/// Otherwise (even if it has no keys), the [TextStyle] is created from the
13521392
/// following keys: `color` ([color]), `backgroundColor` ([color]), `fontSize`
1353-
/// (double), `fontWeight` ([enumValue] of [FontWeight]), `fontStyle`
1393+
/// (double), `fontWeight` ([fontWeight]), `fontStyle`
13541394
/// ([enumValue] of [FontStyle]), `letterSpacing` (double), `wordSpacing`
13551395
/// (double), `textBaseline` ([enumValue] of [TextBaseline]), `height`
13561396
/// (double), `leadingDistribution` ([enumValue] of
@@ -1369,7 +1409,7 @@ class ArgumentDecoders {
13691409
color: color(source, [...key, 'color']),
13701410
backgroundColor: color(source, [...key, 'backgroundColor']),
13711411
fontSize: source.v<double>([...key, 'fontSize']),
1372-
fontWeight: enumValue<FontWeight>(FontWeight.values, source, [...key, 'fontWeight']),
1412+
fontWeight: fontWeight(source, [...key, 'fontWeight']),
13731413
fontStyle: enumValue<FontStyle>(FontStyle.values, source, [...key, 'fontStyle']),
13741414
letterSpacing: source.v<double>([...key, 'letterSpacing']),
13751415
wordSpacing: source.v<double>([...key, 'wordSpacing']),

‎packages/rfw/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: rfw
22
description: "Remote Flutter widgets: a library for rendering declarative widget description files at runtime."
33
repository: https://github.com/flutter/packages/tree/main/packages/rfw
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+rfw%22
5-
version: 1.1.3
5+
version: 1.1.4
66

77
environment:
88
sdk: ^3.10.0

‎packages/rfw/test/argument_decoders_test.dart‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,4 +551,111 @@ void main() {
551551

552552
expect(eventLog, isEmpty);
553553
}, skip: kIsWeb || !isMainChannel); // https://github.com/flutter/flutter/pull/129851
554+
555+
testWidgets('fontWeight decoder', (WidgetTester tester) async {
556+
final runtime = Runtime()
557+
..update(const LibraryName(<String>['core']), createCoreWidgets())
558+
..update(const LibraryName(<String>['test']), parseLibraryFile('import core; widget root = SizedBox();'));
559+
addTearDown(runtime.dispose);
560+
await tester.pumpWidget(
561+
RemoteWidget(
562+
runtime: runtime,
563+
data: DynamicContent(),
564+
widget: const FullyQualifiedWidgetName(LibraryName(<String>['test']), 'root'),
565+
onEvent: (String name, DynamicMap arguments) { },
566+
),
567+
);
568+
569+
// String values w100–w900 in textStyle.
570+
for (final (String name, FontWeight weight) in <(String, FontWeight)>[
571+
('w100', FontWeight.w100), ('w200', FontWeight.w200), ('w300', FontWeight.w300),
572+
('w400', FontWeight.w400), ('w500', FontWeight.w500), ('w600', FontWeight.w600),
573+
('w700', FontWeight.w700), ('w800', FontWeight.w800), ('w900', FontWeight.w900),
574+
('normal', FontWeight.w400), ('bold', FontWeight.w700),
575+
]) {
576+
runtime.update(const LibraryName(<String>['test']), parseLibraryFile('''
577+
import core;
578+
widget root = Directionality(
579+
textDirection: "ltr",
580+
child: Text(text: "hello", style: { fontWeight: "$name", fontSize: 16.0 }),
581+
);
582+
'''));
583+
await tester.pump();
584+
expect(tester.widget<Text>(find.byType(Text)).style?.fontWeight, weight, reason: 'string "$name"');
585+
}
586+
587+
// Integer values 100–900 in textStyle.
588+
for (final (int value, FontWeight weight) in <(int, FontWeight)>[
589+
(100, FontWeight.w100), (200, FontWeight.w200), (300, FontWeight.w300),
590+
(400, FontWeight.w400), (500, FontWeight.w500), (600, FontWeight.w600),
591+
(700, FontWeight.w700), (800, FontWeight.w800), (900, FontWeight.w900),
592+
]) {
593+
runtime.update(const LibraryName(<String>['test']), parseLibraryFile('''
594+
import core;
595+
widget root = Directionality(
596+
textDirection: "ltr",
597+
child: Text(text: "hello", style: { fontWeight: $value, fontSize: 16.0 }),
598+
);
599+
'''));
600+
await tester.pump();
601+
expect(tester.widget<Text>(find.byType(Text)).style?.fontWeight, weight, reason: 'int $value');
602+
}
603+
604+
// Unknown string returns null.
605+
runtime.update(const LibraryName(<String>['test']), parseLibraryFile('''
606+
import core;
607+
widget root = Directionality(
608+
textDirection: "ltr",
609+
child: Text(text: "hello", style: { fontWeight: "heavy", fontSize: 16.0 }),
610+
);
611+
'''));
612+
await tester.pump();
613+
expect(tester.widget<Text>(find.byType(Text)).style?.fontWeight, isNull);
614+
615+
// Missing key returns null.
616+
runtime.update(const LibraryName(<String>['test']), parseLibraryFile('''
617+
import core;
618+
widget root = Directionality(
619+
textDirection: "ltr",
620+
child: Text(text: "hello", style: { fontSize: 16.0 }),
621+
);
622+
'''));
623+
await tester.pump();
624+
expect(tester.widget<Text>(find.byType(Text)).style?.fontWeight, isNull);
625+
626+
// String values w100–w900 in strutStyle.
627+
for (final (String name, FontWeight weight) in <(String, FontWeight)>[
628+
('w100', FontWeight.w100), ('w200', FontWeight.w200), ('w300', FontWeight.w300),
629+
('w400', FontWeight.w400), ('w500', FontWeight.w500), ('w600', FontWeight.w600),
630+
('w700', FontWeight.w700), ('w800', FontWeight.w800), ('w900', FontWeight.w900),
631+
('normal', FontWeight.w400), ('bold', FontWeight.w700),
632+
]) {
633+
runtime.update(const LibraryName(<String>['test']), parseLibraryFile('''
634+
import core;
635+
widget root = Directionality(
636+
textDirection: "ltr",
637+
child: Text(text: "hello", strutStyle: { fontWeight: "$name", fontSize: 16.0 }),
638+
);
639+
'''));
640+
await tester.pump();
641+
expect(tester.widget<Text>(find.byType(Text)).strutStyle?.fontWeight, weight, reason: 'strutStyle string "$name"');
642+
}
643+
644+
// Integer values 100–900 in strutStyle.
645+
for (final (int value, FontWeight weight) in <(int, FontWeight)>[
646+
(100, FontWeight.w100), (200, FontWeight.w200), (300, FontWeight.w300),
647+
(400, FontWeight.w400), (500, FontWeight.w500), (600, FontWeight.w600),
648+
(700, FontWeight.w700), (800, FontWeight.w800), (900, FontWeight.w900),
649+
]) {
650+
runtime.update(const LibraryName(<String>['test']), parseLibraryFile('''
651+
import core;
652+
widget root = Directionality(
653+
textDirection: "ltr",
654+
child: Text(text: "hello", strutStyle: { fontWeight: $value, fontSize: 16.0 }),
655+
);
656+
'''));
657+
await tester.pump();
658+
expect(tester.widget<Text>(find.byType(Text)).strutStyle?.fontWeight, weight, reason: 'strutStyle int $value');
659+
}
660+
});
554661
}

0 commit comments

Comments
 (0)