Skip to content

Commit 96b86e1

Browse files
committed
[rfw] Fix fontWeight not applied in release mode
FontWeight is not a real Dart enum; it is a hand-crafted class with static const members. The previous implementation decoded fontWeight via enumValue(), which matched strings against FontWeight.value.toString(). In AOT/release builds this is not guaranteed to be stable, so matches silently failed and fontWeight defaulted to null (rendered as w400). Add a dedicated ArgumentDecoders.fontWeight() 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(). Update textStyle and strutStyle to use it. Also fix three pre-existing bugs where absolute keys were used instead of relative keys: textDirection in the sweep shader decoder, textBaseline and overflow in textStyle. Fixes flutter/flutter#180223 Made-with: Cursor
1 parent cac0fc5 commit 96b86e1

2 files changed

Lines changed: 243 additions & 7 deletions

File tree

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

Lines changed: 47 additions & 7 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
@@ -1243,7 +1283,7 @@ class ArgumentDecoders {
12431283
case 'sweep':
12441284
return gradient(source, key)!.createShader(
12451285
rect(source, [...key, 'rect']) ?? Rect.zero,
1246-
textDirection: enumValue<TextDirection>(TextDirection.values, source, ['textDirection']) ?? TextDirection.ltr,
1286+
textDirection: enumValue<TextDirection>(TextDirection.values, source, [...key, 'textDirection']) ?? TextDirection.ltr,
12471287
);
12481288
default:
12491289
final ArgumentDecoder<Shader?>? decoder = shaderDecoders[type];
@@ -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,11 +1409,11 @@ 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']),
1376-
textBaseline: enumValue<TextBaseline>(TextBaseline.values, source, ['textBaseline']),
1416+
textBaseline: enumValue<TextBaseline>(TextBaseline.values, source, [...key, 'textBaseline']),
13771417
height: source.v<double>([...key, 'height']),
13781418
leadingDistribution: enumValue<TextLeadingDistribution>(TextLeadingDistribution.values, source, [...key, 'leadingDistribution']),
13791419
locale: locale(source, [...key, 'locale']),
@@ -1387,7 +1427,7 @@ class ArgumentDecoders {
13871427
decorationThickness: source.v<double>([...key, 'decorationThickness']),
13881428
fontFamily: source.v<String>([...key, 'fontFamily']),
13891429
fontFamilyFallback: list<String>(source, [...key, 'fontFamilyFallback'], string),
1390-
overflow: enumValue<TextOverflow>(TextOverflow.values, source, ['overflow']),
1430+
overflow: enumValue<TextOverflow>(TextOverflow.values, source, [...key, 'overflow']),
13911431
);
13921432
}
13931433

‎packages/rfw/test/argument_decoders_test.dart‎

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

552552
expect(eventLog, isEmpty);
553553
}, skip: kIsWeb || !isMainChannel); // https://github.com/flutter/flutter/pull/129851
554+
555+
// fontWeight decoder tests
556+
557+
testWidgets('fontWeight string values in textStyle', (WidgetTester tester) async {
558+
final runtime = Runtime()
559+
..update(const LibraryName(<String>['core']), createCoreWidgets());
560+
addTearDown(runtime.dispose);
561+
562+
Future<FontWeight?> pumpWithFontWeight(String value) async {
563+
runtime.update(
564+
const LibraryName(<String>['test']),
565+
parseLibraryFile('''
566+
import core;
567+
widget root = Directionality(
568+
textDirection: "ltr",
569+
child: Text(text: "hello", style: { fontWeight: "$value", fontSize: 16.0 }),
570+
);
571+
'''),
572+
);
573+
await tester.pumpWidget(RemoteWidget(
574+
runtime: runtime,
575+
data: DynamicContent(),
576+
widget: const FullyQualifiedWidgetName(LibraryName(<String>['test']), 'root'),
577+
onEvent: (_, __) {},
578+
));
579+
await tester.pump();
580+
return tester.widget<Text>(find.byType(Text)).style?.fontWeight;
581+
}
582+
583+
expect(await pumpWithFontWeight('w100'), FontWeight.w100);
584+
expect(await pumpWithFontWeight('w200'), FontWeight.w200);
585+
expect(await pumpWithFontWeight('w300'), FontWeight.w300);
586+
expect(await pumpWithFontWeight('w400'), FontWeight.w400);
587+
expect(await pumpWithFontWeight('w500'), FontWeight.w500);
588+
expect(await pumpWithFontWeight('w600'), FontWeight.w600);
589+
expect(await pumpWithFontWeight('w700'), FontWeight.w700);
590+
expect(await pumpWithFontWeight('w800'), FontWeight.w800);
591+
expect(await pumpWithFontWeight('w900'), FontWeight.w900);
592+
expect(await pumpWithFontWeight('normal'), FontWeight.w400);
593+
expect(await pumpWithFontWeight('bold'), FontWeight.w700);
594+
});
595+
596+
testWidgets('fontWeight integer values in textStyle', (WidgetTester tester) async {
597+
final runtime = Runtime()
598+
..update(const LibraryName(<String>['core']), createCoreWidgets());
599+
addTearDown(runtime.dispose);
600+
601+
Future<FontWeight?> pumpWithFontWeightInt(int value) async {
602+
runtime.update(
603+
const LibraryName(<String>['test']),
604+
parseLibraryFile('''
605+
import core;
606+
widget root = Directionality(
607+
textDirection: "ltr",
608+
child: Text(text: "hello", style: { fontWeight: $value, fontSize: 16.0 }),
609+
);
610+
'''),
611+
);
612+
await tester.pumpWidget(RemoteWidget(
613+
runtime: runtime,
614+
data: DynamicContent(),
615+
widget: const FullyQualifiedWidgetName(LibraryName(<String>['test']), 'root'),
616+
onEvent: (_, __) {},
617+
));
618+
await tester.pump();
619+
return tester.widget<Text>(find.byType(Text)).style?.fontWeight;
620+
}
621+
622+
expect(await pumpWithFontWeightInt(100), FontWeight.w100);
623+
expect(await pumpWithFontWeightInt(200), FontWeight.w200);
624+
expect(await pumpWithFontWeightInt(300), FontWeight.w300);
625+
expect(await pumpWithFontWeightInt(400), FontWeight.w400);
626+
expect(await pumpWithFontWeightInt(500), FontWeight.w500);
627+
expect(await pumpWithFontWeightInt(600), FontWeight.w600);
628+
expect(await pumpWithFontWeightInt(700), FontWeight.w700);
629+
expect(await pumpWithFontWeightInt(800), FontWeight.w800);
630+
expect(await pumpWithFontWeightInt(900), FontWeight.w900);
631+
});
632+
633+
testWidgets('fontWeight missing key returns null in textStyle', (WidgetTester tester) async {
634+
final runtime = Runtime()
635+
..update(const LibraryName(<String>['core']), createCoreWidgets())
636+
..update(const LibraryName(<String>['test']), parseLibraryFile('''
637+
import core;
638+
widget root = Directionality(
639+
textDirection: "ltr",
640+
child: Text(text: "hello", style: { fontSize: 16.0 }),
641+
);
642+
'''));
643+
addTearDown(runtime.dispose);
644+
await tester.pumpWidget(RemoteWidget(
645+
runtime: runtime,
646+
data: DynamicContent(),
647+
widget: const FullyQualifiedWidgetName(LibraryName(<String>['test']), 'root'),
648+
onEvent: (_, __) {},
649+
));
650+
await tester.pump();
651+
expect(tester.widget<Text>(find.byType(Text)).style?.fontWeight, isNull);
652+
});
653+
654+
testWidgets('fontWeight unknown string returns null in textStyle', (WidgetTester tester) async {
655+
final runtime = Runtime()
656+
..update(const LibraryName(<String>['core']), createCoreWidgets())
657+
..update(const LibraryName(<String>['test']), parseLibraryFile('''
658+
import core;
659+
widget root = Directionality(
660+
textDirection: "ltr",
661+
child: Text(text: "hello", style: { fontWeight: "heavy", fontSize: 16.0 }),
662+
);
663+
'''));
664+
addTearDown(runtime.dispose);
665+
await tester.pumpWidget(RemoteWidget(
666+
runtime: runtime,
667+
data: DynamicContent(),
668+
widget: const FullyQualifiedWidgetName(LibraryName(<String>['test']), 'root'),
669+
onEvent: (_, __) {},
670+
));
671+
await tester.pump();
672+
expect(tester.widget<Text>(find.byType(Text)).style?.fontWeight, isNull);
673+
});
674+
675+
testWidgets('fontWeight string values in strutStyle', (WidgetTester tester) async {
676+
final runtime = Runtime()
677+
..update(const LibraryName(<String>['core']), createCoreWidgets());
678+
addTearDown(runtime.dispose);
679+
680+
Future<FontWeight?> pumpWithStrutFontWeight(String value) async {
681+
runtime.update(
682+
const LibraryName(<String>['test']),
683+
parseLibraryFile('''
684+
import core;
685+
widget root = Directionality(
686+
textDirection: "ltr",
687+
child: Text(text: "hello", strutStyle: { fontWeight: "$value", fontSize: 16.0 }),
688+
);
689+
'''),
690+
);
691+
await tester.pumpWidget(RemoteWidget(
692+
runtime: runtime,
693+
data: DynamicContent(),
694+
widget: const FullyQualifiedWidgetName(LibraryName(<String>['test']), 'root'),
695+
onEvent: (_, __) {},
696+
));
697+
await tester.pump();
698+
return tester.widget<Text>(find.byType(Text)).strutStyle?.fontWeight;
699+
}
700+
701+
expect(await pumpWithStrutFontWeight('w100'), FontWeight.w100);
702+
expect(await pumpWithStrutFontWeight('w200'), FontWeight.w200);
703+
expect(await pumpWithStrutFontWeight('w300'), FontWeight.w300);
704+
expect(await pumpWithStrutFontWeight('w400'), FontWeight.w400);
705+
expect(await pumpWithStrutFontWeight('w500'), FontWeight.w500);
706+
expect(await pumpWithStrutFontWeight('w600'), FontWeight.w600);
707+
expect(await pumpWithStrutFontWeight('w700'), FontWeight.w700);
708+
expect(await pumpWithStrutFontWeight('w800'), FontWeight.w800);
709+
expect(await pumpWithStrutFontWeight('w900'), FontWeight.w900);
710+
expect(await pumpWithStrutFontWeight('normal'), FontWeight.w400);
711+
expect(await pumpWithStrutFontWeight('bold'), FontWeight.w700);
712+
});
713+
714+
testWidgets('fontWeight integer values in strutStyle', (WidgetTester tester) async {
715+
final runtime = Runtime()
716+
..update(const LibraryName(<String>['core']), createCoreWidgets());
717+
addTearDown(runtime.dispose);
718+
719+
Future<FontWeight?> pumpWithStrutFontWeightInt(int value) async {
720+
runtime.update(
721+
const LibraryName(<String>['test']),
722+
parseLibraryFile('''
723+
import core;
724+
widget root = Directionality(
725+
textDirection: "ltr",
726+
child: Text(text: "hello", strutStyle: { fontWeight: $value, fontSize: 16.0 }),
727+
);
728+
'''),
729+
);
730+
await tester.pumpWidget(RemoteWidget(
731+
runtime: runtime,
732+
data: DynamicContent(),
733+
widget: const FullyQualifiedWidgetName(LibraryName(<String>['test']), 'root'),
734+
onEvent: (_, __) {},
735+
));
736+
await tester.pump();
737+
return tester.widget<Text>(find.byType(Text)).strutStyle?.fontWeight;
738+
}
739+
740+
expect(await pumpWithStrutFontWeightInt(100), FontWeight.w100);
741+
expect(await pumpWithStrutFontWeightInt(200), FontWeight.w200);
742+
expect(await pumpWithStrutFontWeightInt(300), FontWeight.w300);
743+
expect(await pumpWithStrutFontWeightInt(400), FontWeight.w400);
744+
expect(await pumpWithStrutFontWeightInt(500), FontWeight.w500);
745+
expect(await pumpWithStrutFontWeightInt(600), FontWeight.w600);
746+
expect(await pumpWithStrutFontWeightInt(700), FontWeight.w700);
747+
expect(await pumpWithStrutFontWeightInt(800), FontWeight.w800);
748+
expect(await pumpWithStrutFontWeightInt(900), FontWeight.w900);
749+
});
554750
}

0 commit comments

Comments
 (0)