Skip to content

Commit 4717a64

Browse files
Add Slider.showValueIndicator property. (flutter#179661)
Closes flutter#179660 ### Description - Adds `Slider.showValueIndicator` property - Adds test to verify that `Slider.showValueIndicator` takes priority over `SliderTheme.showValueIndicator` ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com>
1 parent 5d8b59b commit 4717a64

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

packages/flutter/lib/src/material/slider.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class Slider extends StatefulWidget {
189189
this.autofocus = false,
190190
this.allowedInteraction,
191191
this.padding,
192+
this.showValueIndicator,
192193
@Deprecated(
193194
'Set this flag to false to opt into the 2024 slider appearance. Defaults to true. '
194195
'In the future, this flag will default to false. Use SliderThemeData to customize individual properties. '
@@ -216,7 +217,7 @@ class Slider extends StatefulWidget {
216217
///
217218
/// If a [CupertinoSlider] is created, the following parameters are ignored:
218219
/// [secondaryTrackValue], [label], [inactiveColor], [secondaryActiveColor],
219-
/// [semanticFormatterCallback].
220+
/// [semanticFormatterCallback], [showValueIndicator].
220221
///
221222
/// The target platform is based on the current [Theme]: [ThemeData.platform].
222223
const Slider.adaptive({
@@ -240,6 +241,7 @@ class Slider extends StatefulWidget {
240241
this.focusNode,
241242
this.autofocus = false,
242243
this.allowedInteraction,
244+
this.showValueIndicator,
243245
@Deprecated(
244246
'Set this flag to false to opt into the 2024 slider appearance. Defaults to true. '
245247
'In the future, this flag will default to false. Use SliderThemeData to customize individual properties. '
@@ -568,6 +570,13 @@ class Slider extends StatefulWidget {
568570
/// overlay shape, whichever is larger.
569571
final EdgeInsetsGeometry? padding;
570572

573+
/// Determines the conditions under which the value indicator is shown.
574+
///
575+
/// If [Slider.showValueIndicator] is null then the
576+
/// ambient [SliderThemeData.showValueIndicator] is used. If that is also
577+
/// null, defaults to [ShowValueIndicator.onlyForDiscrete].
578+
final ShowValueIndicator? showValueIndicator;
579+
571580
/// When true, the [Slider] will use the 2023 Material Design 3 appearance.
572581
/// Defaults to true.
573582
///
@@ -914,7 +923,8 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
914923
thumbShape: sliderTheme.thumbShape ?? defaults.thumbShape,
915924
overlayShape: sliderTheme.overlayShape ?? defaults.overlayShape,
916925
valueIndicatorShape: valueIndicatorShape,
917-
showValueIndicator: sliderTheme.showValueIndicator ?? defaultShowValueIndicator,
926+
showValueIndicator:
927+
widget.showValueIndicator ?? sliderTheme.showValueIndicator ?? defaultShowValueIndicator,
918928
valueIndicatorTextStyle: valueIndicatorTextStyle,
919929
padding: widget.padding ?? sliderTheme.padding,
920930
thumbSize: sliderTheme.thumbSize ?? defaults.thumbSize,

packages/flutter/lib/src/material/slider_theme.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class SliderTheme extends InheritedTheme {
112112
}
113113

114114
/// Describes the conditions under which the value indicator on a [Slider]
115-
/// will be shown. Used with [SliderThemeData.showValueIndicator].
115+
/// will be shown. Used in [Slider.showValueIndicator] and
116+
/// [SliderThemeData.showValueIndicator].
116117
///
117118
/// See also:
118119
///

packages/flutter/test/material/slider_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,6 +4210,62 @@ void main() {
42104210
variant: TargetPlatformVariant.desktop(),
42114211
);
42124212

4213+
testWidgets('showValueIndicator takes priority over theme', (WidgetTester tester) async {
4214+
Widget buildApp({
4215+
required ShowValueIndicator? themeShowValueIndicator,
4216+
required ShowValueIndicator? sliderShowValueIndicator,
4217+
}) {
4218+
return MaterialApp(
4219+
home: Material(
4220+
child: Center(
4221+
child: SliderTheme(
4222+
data: SliderThemeData(
4223+
valueIndicatorColor: Colors.red,
4224+
showValueIndicator: themeShowValueIndicator,
4225+
),
4226+
child: Slider(
4227+
value: 0.5,
4228+
label: '0.5',
4229+
onChanged: (double newValue) {},
4230+
showValueIndicator: sliderShowValueIndicator,
4231+
),
4232+
),
4233+
),
4234+
),
4235+
);
4236+
}
4237+
4238+
void checkValueIndicator({required bool isVisible}) {
4239+
// _RenderValueIndicator is the last render object in the tree.
4240+
final RenderObject valueIndicatorBox = tester.allRenderObjects.last;
4241+
final PaintPattern matcher = paints
4242+
..path(color: Colors.red)
4243+
..paragraph();
4244+
expect(valueIndicatorBox, isVisible ? matcher : isNot(matcher));
4245+
}
4246+
4247+
await tester.pumpWidget(
4248+
buildApp(themeShowValueIndicator: ShowValueIndicator.never, sliderShowValueIndicator: null),
4249+
);
4250+
checkValueIndicator(isVisible: false);
4251+
4252+
await tester.pumpWidget(
4253+
buildApp(
4254+
themeShowValueIndicator: ShowValueIndicator.never,
4255+
sliderShowValueIndicator: ShowValueIndicator.alwaysVisible,
4256+
),
4257+
);
4258+
checkValueIndicator(isVisible: true);
4259+
4260+
await tester.pumpWidget(
4261+
buildApp(
4262+
themeShowValueIndicator: ShowValueIndicator.alwaysVisible,
4263+
sliderShowValueIndicator: ShowValueIndicator.never,
4264+
),
4265+
);
4266+
checkValueIndicator(isVisible: false);
4267+
});
4268+
42134269
testWidgets('Event on Slider should perform no-op if already unmounted', (
42144270
WidgetTester tester,
42154271
) async {

0 commit comments

Comments
 (0)