[a11y] Slider should respect bold text setting (#149053)

Fix https://github.com/flutter/flutter/issues/147600

internal GAR issue: b/316933135

reopen from #148435
This commit is contained in:
hangyu 2024-05-24 12:48:14 -07:00 committed by GitHub
parent 7be97ab781
commit 8ff57091bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 109 additions and 1 deletions

View File

@ -817,6 +817,11 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
?? MaterialStateProperty.resolveAs<Color?>(defaults.overlayColor, states); ?? MaterialStateProperty.resolveAs<Color?>(defaults.overlayColor, states);
} }
TextStyle valueIndicatorTextStyle = sliderTheme.valueIndicatorTextStyle ?? defaults.valueIndicatorTextStyle!;
if (MediaQuery.boldTextOf(context)) {
valueIndicatorTextStyle = valueIndicatorTextStyle.merge(const TextStyle(fontWeight: FontWeight.bold));
}
sliderTheme = sliderTheme.copyWith( sliderTheme = sliderTheme.copyWith(
trackHeight: sliderTheme.trackHeight ?? defaults.trackHeight, trackHeight: sliderTheme.trackHeight ?? defaults.trackHeight,
activeTrackColor: widget.activeColor ?? sliderTheme.activeTrackColor ?? defaults.activeTrackColor, activeTrackColor: widget.activeColor ?? sliderTheme.activeTrackColor ?? defaults.activeTrackColor,
@ -839,7 +844,7 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
overlayShape: sliderTheme.overlayShape ?? defaultOverlayShape, overlayShape: sliderTheme.overlayShape ?? defaultOverlayShape,
valueIndicatorShape: valueIndicatorShape, valueIndicatorShape: valueIndicatorShape,
showValueIndicator: sliderTheme.showValueIndicator ?? defaultShowValueIndicator, showValueIndicator: sliderTheme.showValueIndicator ?? defaultShowValueIndicator,
valueIndicatorTextStyle: sliderTheme.valueIndicatorTextStyle ?? defaults.valueIndicatorTextStyle, valueIndicatorTextStyle: valueIndicatorTextStyle,
); );
final MouseCursor effectiveMouseCursor = MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states) final MouseCursor effectiveMouseCursor = MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states)
?? sliderTheme.mouseCursor?.resolve(states) ?? sliderTheme.mouseCursor?.resolve(states)

View File

@ -49,6 +49,36 @@ class LoggingThumbShape extends SliderComponentShape {
} }
} }
// A value indicator shape to log labelPainter text.
class LoggingValueIndicatorShape extends SliderComponentShape {
LoggingValueIndicatorShape(this.logLabel);
final List<InlineSpan> logLabel;
@override
Size getPreferredSize(bool isEnabled, bool isDiscrete) {
return const Size(10.0, 10.0);
}
@override
void paint(
PaintingContext context,
Offset offset, {
required Animation<double> activationAnimation,
required Animation<double> enableAnimation,
required bool isDiscrete,
required TextPainter labelPainter,
required RenderBox parentBox,
required SliderThemeData sliderTheme,
required TextDirection textDirection,
required double value,
required double textScaleFactor,
required Size sizeWithOverflow,
}) {
logLabel.add(labelPainter.text!);
}
}
class TallSliderTickMarkShape extends SliderTickMarkShape { class TallSliderTickMarkShape extends SliderTickMarkShape {
@override @override
Size getPreferredSize({required SliderThemeData sliderTheme, required bool isEnabled}) { Size getPreferredSize({required SliderThemeData sliderTheme, required bool isEnabled}) {
@ -975,6 +1005,79 @@ void main() {
} }
}); });
testWidgets('Slider value indicator respects bold text', (WidgetTester tester) async {
final Key sliderKey = UniqueKey();
double value = 0.0;
final List<InlineSpan> log = <InlineSpan>[];
final LoggingValueIndicatorShape loggingValueIndicatorShape = LoggingValueIndicatorShape(log);
Widget buildSlider({ bool boldText = false }) {
return MaterialApp(
home: Directionality(
textDirection: TextDirection.ltr,
child: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return MediaQuery(
data: MediaQueryData(boldText: boldText),
child: Material(
child: Theme(
data: Theme.of(context).copyWith(
sliderTheme: Theme.of(context).sliderTheme.copyWith(
showValueIndicator: ShowValueIndicator.always,
valueIndicatorShape: loggingValueIndicatorShape,
),
),
child: Center(
child: OverflowBox(
maxWidth: double.infinity,
maxHeight: double.infinity,
child: Slider(
key: sliderKey,
max: 100.0,
divisions: 4,
label: '${value.round()}',
value: value,
onChanged: (double newValue) {
setState(() {
value = newValue;
});
},
),
),
),
),
),
);
},
),
),
);
}
// Normal text
await tester.pumpWidget(buildSlider());
Offset center = tester.getCenter(find.byType(Slider));
TestGesture gesture = await tester.startGesture(center);
await tester.pumpAndSettle();
expect(log.last.toPlainText(), '50');
expect(log.last.style!.fontWeight, FontWeight.w500);
await gesture.up();
await tester.pumpAndSettle();
// Bold text
await tester.pumpWidget(buildSlider(boldText: true));
center = tester.getCenter(find.byType(Slider));
gesture = await tester.startGesture(center);
await tester.pumpAndSettle();
expect(log.last.toPlainText(), '50');
expect(log.last.style!.fontWeight, FontWeight.w700);
await gesture.up();
await tester.pumpAndSettle();
});
testWidgets('Tick marks are skipped when they are too dense', (WidgetTester tester) async { testWidgets('Tick marks are skipped when they are too dense', (WidgetTester tester) async {
Widget buildSlider({ Widget buildSlider({
required int divisions, required int divisions,