Skip to content

Commit f02c867

Browse files
committed
text: Add bolderWghtTextStyle
1 parent 01555e8 commit f02c867

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

lib/widgets/text.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,44 @@ double bolderWght(double baseWght, {double by = 300}) {
264264
return clampDouble(baseWght + by, kWghtMin, kWghtMax);
265265
}
266266

267+
/// A [TextStyle] whose [FontVariation] "wght" and [TextStyle.fontWeight]
268+
/// have been raised using [bolderWght].
269+
///
270+
/// [style] must have already been processed with [weightVariableTextStyle],
271+
/// and [by] must be positive.
272+
///
273+
/// The increase done here will commute with any increase that was done by
274+
/// [weightVariableTextStyle] to respond to the device bold-text setting,
275+
/// because both adjustments are done with [bolderWght] with positive `by`.
276+
///
277+
/// [by] defaults to 300.
278+
TextStyle bolderWghtTextStyle(TextStyle style, {double by = 300}) {
279+
assert(
280+
style.debugLabel!.contains('weightVariableTextStyle')
281+
// ([ContentTheme.textStylePlainParagraph] applies [weightVariableTextStyle])
282+
|| style.debugLabel!.contains('ContentTheme.textStylePlainParagraph')
283+
|| style.debugLabel!.contains('bolderWghtTextStyle')
284+
);
285+
assert(by > 0);
286+
assert(style.fontVariations!.where((v) => v.axis == 'wght').length == 1);
287+
288+
final newWght = bolderWght(wghtFromTextStyle(style)!, by: by);
289+
290+
TextStyle result = style.copyWith(
291+
fontVariations: style.fontVariations!.map((v) => v.axis == 'wght'
292+
? FontVariation('wght', newWght)
293+
: v).toList(),
294+
fontWeight: clampVariableFontWeight(newWght),
295+
);
296+
297+
assert(() {
298+
result = result.copyWith(debugLabel: 'bolderWghtTextStyle(by: $by)');
299+
return true;
300+
}());
301+
302+
return result;
303+
}
304+
267305
/// Find the nearest [FontWeight] constant for a variable-font "wght"-axis value.
268306
///
269307
/// Use this for a reasonable [TextStyle.fontWeight] for glyphs that need to be

test/widgets/text_test.dart

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:checks/checks.dart';
2+
import 'package:collection/collection.dart';
23
import 'package:flutter/material.dart';
34
import 'package:flutter_test/flutter_test.dart';
45
import 'package:zulip/widgets/text.dart';
@@ -169,6 +170,82 @@ void main() {
169170
check(bolderWght(900, by: 200)).equals(1000);
170171
});
171172

173+
group('bolderWghtTextStyle', () {
174+
Future<void> testBolderWghtTextStyle(
175+
String description, {
176+
required TextStyle Function(BuildContext context) makeStyle,
177+
bool platformRequestsBold = false,
178+
required double expectedWght,
179+
required FontWeight expectedFontWeight,
180+
}) async {
181+
testWidgets(description, (WidgetTester tester) async {
182+
tester.platformDispatcher.accessibilityFeaturesTestValue =
183+
FakeAccessibilityFeatures(boldText: platformRequestsBold);
184+
185+
await tester.pumpWidget(
186+
MaterialApp(
187+
home: Builder(builder: (context) =>
188+
Text('', style: makeStyle(context)))));
189+
190+
final TextStyle? style = tester.widget<Text>(find.byType(Text)).style;
191+
192+
check(style).isNotNull().fontWeight.isNotNull().equals(expectedFontWeight);
193+
194+
final fontVariations = style!.fontVariations;
195+
check(fontVariations).isNotNull();
196+
final wghtVariation = fontVariations!.singleWhereOrNull((v) => v.axis == 'wght');
197+
check(wghtVariation).isNotNull().value.equals(expectedWght);
198+
199+
tester.platformDispatcher.clearAccessibilityFeaturesTestValue();
200+
});
201+
}
202+
203+
testBolderWghtTextStyle('default + default',
204+
makeStyle: (context) => bolderWghtTextStyle(weightVariableTextStyle(context)),
205+
expectedWght: 700,
206+
expectedFontWeight: FontWeight.w700);
207+
208+
testBolderWghtTextStyle('default + default (platform requests bold)',
209+
platformRequestsBold: true,
210+
makeStyle: (context) => bolderWghtTextStyle(weightVariableTextStyle(context)),
211+
expectedWght: 1000,
212+
expectedFontWeight: FontWeight.w900);
213+
214+
testBolderWghtTextStyle('320 + 200',
215+
makeStyle: (context) => bolderWghtTextStyle(
216+
weightVariableTextStyle(context, wght: 320),
217+
by: 200,
218+
),
219+
expectedWght: 520,
220+
expectedFontWeight: FontWeight.w500);
221+
222+
testBolderWghtTextStyle('320 + 200 (platform requests bold)',
223+
platformRequestsBold: true,
224+
makeStyle: (context) => bolderWghtTextStyle(
225+
weightVariableTextStyle(context, wght: 320),
226+
by: 200,
227+
),
228+
expectedWght: 820,
229+
expectedFontWeight: FontWeight.w800);
230+
231+
testBolderWghtTextStyle('320 + 200 (platform requests bold; custom response to setting)',
232+
platformRequestsBold: true,
233+
makeStyle: (context) => bolderWghtTextStyle(
234+
weightVariableTextStyle(context, wght: 320, wghtIfPlatformRequestsBold: 410),
235+
by: 200,
236+
),
237+
expectedWght: 610,
238+
expectedFontWeight: FontWeight.w600);
239+
240+
testBolderWghtTextStyle('900 + 200',
241+
makeStyle: (context) => bolderWghtTextStyle(
242+
weightVariableTextStyle(context, wght: 900),
243+
by: 200,
244+
),
245+
expectedWght: 1000,
246+
expectedFontWeight: FontWeight.w900);
247+
});
248+
172249
test('clampVariableFontWeight: FontWeight has the assumed list of values', () {
173250
// Implementation assumes specific FontWeight values; we should
174251
// adapt if these change in a new Flutter version.

0 commit comments

Comments
 (0)