Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit eaeae8e

Browse files
authored
Make it possible to obtain FontWeight integer value (#35183)
1 parent ce3397f commit eaeae8e

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

lib/ui/text.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,40 @@ enum FontStyle {
1414

1515
/// The thickness of the glyphs used to draw the text
1616
class FontWeight {
17-
const FontWeight._(this.index);
17+
const FontWeight._(this.index, this.value);
1818

1919
/// The encoded integer value of this font weight.
2020
final int index;
2121

22+
/// The thickness value of this font weight.
23+
final int value;
24+
2225
/// Thin, the least thick
23-
static const FontWeight w100 = FontWeight._(0);
26+
static const FontWeight w100 = FontWeight._(0, 100);
2427

2528
/// Extra-light
26-
static const FontWeight w200 = FontWeight._(1);
29+
static const FontWeight w200 = FontWeight._(1, 200);
2730

2831
/// Light
29-
static const FontWeight w300 = FontWeight._(2);
32+
static const FontWeight w300 = FontWeight._(2, 300);
3033

3134
/// Normal / regular / plain
32-
static const FontWeight w400 = FontWeight._(3);
35+
static const FontWeight w400 = FontWeight._(3, 400);
3336

3437
/// Medium
35-
static const FontWeight w500 = FontWeight._(4);
38+
static const FontWeight w500 = FontWeight._(4, 500);
3639

3740
/// Semi-bold
38-
static const FontWeight w600 = FontWeight._(5);
41+
static const FontWeight w600 = FontWeight._(5, 600);
3942

4043
/// Bold
41-
static const FontWeight w700 = FontWeight._(6);
44+
static const FontWeight w700 = FontWeight._(6, 700);
4245

4346
/// Extra-bold
44-
static const FontWeight w800 = FontWeight._(7);
47+
static const FontWeight w800 = FontWeight._(7, 800);
4548

4649
/// Black, the most thick
47-
static const FontWeight w900 = FontWeight._(8);
50+
static const FontWeight w900 = FontWeight._(8, 900);
4851

4952
/// The default font weight.
5053
static const FontWeight normal = w400;

lib/web_ui/lib/text.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ enum PlaceholderAlignment {
1919
}
2020

2121
class FontWeight {
22-
const FontWeight._(this.index);
22+
const FontWeight._(this.index, this.value);
2323
final int index;
24-
static const FontWeight w100 = FontWeight._(0);
25-
static const FontWeight w200 = FontWeight._(1);
26-
static const FontWeight w300 = FontWeight._(2);
27-
static const FontWeight w400 = FontWeight._(3);
28-
static const FontWeight w500 = FontWeight._(4);
29-
static const FontWeight w600 = FontWeight._(5);
30-
static const FontWeight w700 = FontWeight._(6);
31-
static const FontWeight w800 = FontWeight._(7);
32-
static const FontWeight w900 = FontWeight._(8);
24+
final int value;
25+
static const FontWeight w100 = FontWeight._(0, 100);
26+
static const FontWeight w200 = FontWeight._(1, 200);
27+
static const FontWeight w300 = FontWeight._(2, 300);
28+
static const FontWeight w400 = FontWeight._(3, 400);
29+
static const FontWeight w500 = FontWeight._(4, 500);
30+
static const FontWeight w600 = FontWeight._(5, 600);
31+
static const FontWeight w700 = FontWeight._(6, 700);
32+
static const FontWeight w800 = FontWeight._(7, 800);
33+
static const FontWeight w900 = FontWeight._(8, 900);
3334
static const FontWeight normal = w400;
3435
static const FontWeight bold = w700;
3536
static const List<FontWeight> values = <FontWeight>[

lib/web_ui/test/text_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,16 @@ Future<void> testMain() async {
352352
equals('hello'));
353353
});
354354
});
355+
356+
test('FontWeights have the correct value', () {
357+
expect(FontWeight.w100.value, 100);
358+
expect(FontWeight.w200.value, 200);
359+
expect(FontWeight.w300.value, 300);
360+
expect(FontWeight.w400.value, 400);
361+
expect(FontWeight.w500.value, 500);
362+
expect(FontWeight.w600.value, 600);
363+
expect(FontWeight.w700.value, 700);
364+
expect(FontWeight.w800.value, 800);
365+
expect(FontWeight.w900.value, 900);
366+
});
355367
}

testing/dart/text_test.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Future<Uint8List> readFile(String fileName) async {
1919
return file.readAsBytes();
2020
}
2121

22-
void testFontWeightLerp() {
22+
void testFontWeight() {
2323
test('FontWeight.lerp works with non-null values', () {
2424
expect(FontWeight.lerp(FontWeight.w400, FontWeight.w600, .5), equals(FontWeight.w500));
2525
});
@@ -35,6 +35,18 @@ void testFontWeightLerp() {
3535
test('FontWeight.lerp returns FontWeight.w400 if b is null', () {
3636
expect(FontWeight.lerp(FontWeight.w400, null, 1), equals(FontWeight.w400));
3737
});
38+
39+
test('FontWeights have the correct value', () {
40+
expect(FontWeight.w100.value, 100);
41+
expect(FontWeight.w200.value, 200);
42+
expect(FontWeight.w300.value, 300);
43+
expect(FontWeight.w400.value, 400);
44+
expect(FontWeight.w500.value, 500);
45+
expect(FontWeight.w600.value, 600);
46+
expect(FontWeight.w700.value, 700);
47+
expect(FontWeight.w800.value, 800);
48+
expect(FontWeight.w900.value, 900);
49+
});
3850
}
3951

4052
void testParagraphStyle() {
@@ -277,7 +289,7 @@ void testFontVariation() {
277289
}
278290

279291
void main() {
280-
testFontWeightLerp();
292+
testFontWeight();
281293
testParagraphStyle();
282294
testTextStyle();
283295
testTextHeightBehavior();

0 commit comments

Comments
 (0)