Skip to content

Commit

Permalink
[a11y] Slider should respect bold text setting (#149053)
Browse files Browse the repository at this point in the history
Fix #147600

internal GAR issue: b/316933135

reopen from #148435
  • Loading branch information
hangyujin committed May 24, 2024
1 parent 7be97ab commit 8ff5709
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/flutter/lib/src/material/slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,11 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
?? 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(
trackHeight: sliderTheme.trackHeight ?? defaults.trackHeight,
activeTrackColor: widget.activeColor ?? sliderTheme.activeTrackColor ?? defaults.activeTrackColor,
Expand All @@ -839,7 +844,7 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
overlayShape: sliderTheme.overlayShape ?? defaultOverlayShape,
valueIndicatorShape: valueIndicatorShape,
showValueIndicator: sliderTheme.showValueIndicator ?? defaultShowValueIndicator,
valueIndicatorTextStyle: sliderTheme.valueIndicatorTextStyle ?? defaults.valueIndicatorTextStyle,
valueIndicatorTextStyle: valueIndicatorTextStyle,
);
final MouseCursor effectiveMouseCursor = MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states)
?? sliderTheme.mouseCursor?.resolve(states)
Expand Down
103 changes: 103 additions & 0 deletions packages/flutter/test/material/slider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
@override
Size getPreferredSize({required SliderThemeData sliderTheme, required bool isEnabled}) {
Expand Down Expand Up @@ -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 {
Widget buildSlider({
required int divisions,
Expand Down

0 comments on commit 8ff5709

Please sign in to comment.