From e1d0ded926defa3f3bfb899f06f05051e098cc39 Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 6 Jun 2019 15:31:02 -0400 Subject: [PATCH] [Material] Create a themable Range Slider (continuous and discrete) (#31681) Creates a Material Design range slider. The range slider is based off the updated slider, with the main difference being that it has 2 thumbs. By default, the thumbs cannot pass each other and both the thumb and the value indicator create a stroke when they are overlapping for better visibility. --- packages/flutter/lib/material.dart | 1 + .../lib/src/material/range_slider.dart | 1351 ++++++++++++ .../lib/src/material/slider_theme.dart | 1941 ++++++++++++++--- .../test/material/range_slider_test.dart | 1326 +++++++++++ .../test/material/slider_theme_test.dart | 90 + 5 files changed, 4355 insertions(+), 354 deletions(-) create mode 100644 packages/flutter/lib/src/material/range_slider.dart create mode 100644 packages/flutter/test/material/range_slider_test.dart diff --git a/packages/flutter/lib/material.dart b/packages/flutter/lib/material.dart index e9ec075814..d6589e58ad 100644 --- a/packages/flutter/lib/material.dart +++ b/packages/flutter/lib/material.dart @@ -87,6 +87,7 @@ export 'src/material/progress_indicator.dart'; export 'src/material/radio.dart'; export 'src/material/radio_list_tile.dart'; export 'src/material/raised_button.dart'; +export 'src/material/range_slider.dart'; export 'src/material/refresh_indicator.dart'; export 'src/material/reorderable_list.dart'; export 'src/material/scaffold.dart'; diff --git a/packages/flutter/lib/src/material/range_slider.dart b/packages/flutter/lib/src/material/range_slider.dart new file mode 100644 index 0000000000..2ad954d3d6 --- /dev/null +++ b/packages/flutter/lib/src/material/range_slider.dart @@ -0,0 +1,1351 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; +import 'dart:math' as math; +import 'dart:ui' as ui; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/gestures.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/scheduler.dart' show timeDilation; +import 'package:flutter/widgets.dart'; + +import 'constants.dart'; +import 'debug.dart'; +import 'slider_theme.dart'; +import 'theme.dart'; + +// Examples can assume: +// RangeValues _rangeValues = RangeValues(0.3, 0.7); +// RangeValues _dollarsRange = RangeValues(50, 100); +// void setState(VoidCallback fn) { } + +/// A Material Design range slider. +/// +/// Used to select a range from a range of values. +/// +/// A range slider can be used to select from either a continuous or a discrete +/// set of values. The default is to use a continuous range of values from [min] +/// to [max]. To use discrete values, use a non-null value for [divisions], which +/// indicates the number of discrete intervals. For example, if [min] is 0.0 and +/// [max] is 50.0 and [divisions] is 5, then the slider can take on the +/// discrete values 0.0, 10.0, 20.0, 30.0, 40.0, and 50.0. +/// +/// The terms for the parts of a slider are: +/// +/// * The "thumbs", which are the shapes that slide horizontally when the user +/// drags them to change the selected range. +/// * The "track", which is the horizontal line that the thumbs can be dragged +/// along. +/// * The "tick marks", which mark the discrete values of a discrete slider. +/// * The "overlay", which is a highlight that's drawn over a thumb in response +/// to a user tap-down gesture. +/// * The "value indicators", which are the shapes that pop up when the user +/// is dragging a thumb to show the value being selected. +/// * The "active" segment of the slider is the segment between the two thumbs. +/// * The "inactive" slider segments are the two track intervals outside of the +/// slider's thumbs. +/// +/// The range slider will be disabled if [onChanged] is null or if the range +/// given by [min]..[max] is empty (i.e. if [min] is equal to [max]). +/// +/// The range slider widget itself does not maintain any state. Instead, when +/// the state of the slider changes, the widget calls the [onChanged] callback. +/// Most widgets that use a range slider will listen for the [onChanged] callback +/// and rebuild the slider with a new [value] to update the visual appearance of +/// the slider. To know when the value starts to change, or when it is done +/// changing, set the optional callbacks [onChangeStart] and/or [onChangeEnd]. +/// +/// By default, a slider will be as wide as possible, centered vertically. When +/// given unbounded constraints, it will attempt to make the track 144 pixels +/// wide (including margins on each side) and will shrink-wrap vertically. +/// +/// Requires one of its ancestors to be a [Material] widget. This is typically +/// provided by a [Scaffold] widget. +/// +/// Requires one of its ancestors to be a [MediaQuery] widget. Typically, a +/// [MediaQuery] widget is introduced by the [MaterialApp] or [WidgetsApp] +/// widget at the top of your application widget tree. +/// +/// To determine how it should be displayed (e.g. colors, thumb shape, etc.), +/// a slider uses the [SliderThemeData] available from either a [SliderTheme] +/// widget, or the [ThemeData.sliderTheme] inside a [Theme] widget above it in +/// the widget tree. You can also override some of the colors with the +/// [activeColor] and [inactiveColor] properties, although more fine-grained +/// control of the colors, and other visual properties is achieved using a +/// [SliderThemeData]. +/// +/// See also: +/// +/// * [SliderTheme] and [SliderThemeData] for information about controlling +/// the visual appearance of the slider. +/// * [Slider], for a single-valued slider. +/// * [Radio], for selecting among a set of explicit values. +/// * [Checkbox] and [Switch], for toggling a particular value on or off. +/// * +/// * [MediaQuery], from which the text scale factor is obtained. +class RangeSlider extends StatefulWidget { + /// Creates a Material Design range slider. + /// + /// The range slider widget itself does not maintain any state. Instead, when + /// the state of the slider changes, the widget calls the [onChanged] callback. + /// Most widgets that use a range slider will listen for the [onChanged] callback + /// and rebuild the slider with a new [value] to update the visual appearance of + /// the slider. To know when the value starts to change, or when it is done + /// changing, set the optional callbacks [onChangeStart] and/or [onChangeEnd]. + /// + /// * [values], which determines currently selected values for this range + /// slider. + /// * [onChanged], which is called while the user is selecting a new value for + /// the range slider. + /// * [onChangeStart], which is called when the user starts to select a new + /// value for the range slider. + /// * [onChangeEnd], which is called when the user is done selecting a new + /// value for the range slider. + /// + /// You can override some of the colors with the [activeColor] and + /// [inactiveColor] properties, although more fine-grained control of the + /// appearance is achieved using a [SliderThemeData]. + /// + /// The [values], [min], [max] must not be null. The [min] must be less than + /// or equal to the [max]. [values.start] must be less than or equal to + /// [values.end]. [values.start] and [values.end] must be greater than or + /// equal to the [min] and less than or equal to the [max]. The [divisions] + /// must be null or greater than 0. + RangeSlider({ + Key key, + @required this.values, + @required this.onChanged, + this.onChangeStart, + this.onChangeEnd, + this.min = 0.0, + this.max = 1.0, + this.divisions, + this.labels, + this.activeColor, + this.inactiveColor, + this.semanticFormatterCallback + }) : assert(values != null), + assert(min != null), + assert(max != null), + assert(min <= max), + assert(values.start <= values.end), + assert(values.start >= min && values.start <= max), + assert(values.end >= min && values.end <= max), + assert(divisions == null || divisions > 0), + super(key: key); + + /// The currently selected values for this range slider. + /// + /// The slider's thumbs are drawn at horizontal positions that corresponds to + /// these values. + final RangeValues values; + + /// Called when the user is selecting a new value for the slider by dragging. + /// + /// The slider passes the new values to the callback but does not actually + /// change state until the parent widget rebuilds the slider with the new + /// values. + /// + /// If null, the slider will be displayed as disabled. + /// + /// The callback provided to [onChanged] should update the state of the parent + /// [StatefulWidget] using the [State.setState] method, so that the parent + /// gets rebuilt; for example: + /// + /// {@tool sample} + /// + /// ```dart + /// RangeSlider( + /// values: _rangeValues, + /// min: 1.0, + /// max: 10.0, + /// onChanged: (RangeValues newValues) { + /// setState(() { + /// _rangeValues = newValues; + /// }); + /// }, + /// ) + /// ``` + /// {@end-tool} + /// + /// See also: + /// + /// * [onChangeStart], which is called when the user starts changing the + /// values. + /// * [onChangeEnd], which is called when the user stops changing the values. + final ValueChanged onChanged; + + /// Called when the user starts selecting new values for the slider. + /// + /// This callback shouldn't be used to update the slider [values] (use + /// [onChanged] for that). Rather, it should be used to be notified when the + /// user has started selecting a new value by starting a drag or with a tap. + /// + /// The values passed will be the last [values] that the slider had before the + /// change began. + /// + /// {@tool sample} + /// + /// ```dart + /// RangeSlider( + /// values: _rangeValues, + /// min: 1.0, + /// max: 10.0, + /// onChanged: (RangeValues newValues) { + /// setState(() { + /// _rangeValues = newValues; + /// }); + /// }, + /// onChangeStart: (RangeValues startValues) { + /// print('Started change at $startValues'); + /// }, + /// ) + /// ``` + /// {@end-tool} + /// + /// See also: + /// + /// * [onChangeEnd] for a callback that is called when the value change is + /// complete. + final ValueChanged onChangeStart; + + /// Called when the user is done selecting new values for the slider. + /// + /// This differs from [onChanged] because it is only called once at the end + /// of the interaction, while [onChanged] is called as the value is getting + /// updated within the interaction. + /// + /// This callback shouldn't be used to update the slider [values] (use + /// [onChanged] for that). Rather, it should be used to know when the user has + /// completed selecting a new [values] by ending a drag or a click. + /// + /// {@tool sample} + /// + /// ```dart + /// RangeSlider( + /// values: _rangeValues, + /// min: 1.0, + /// max: 10.0, + /// onChanged: (RangeValues newValues) { + /// setState(() { + /// _rangeValues = newValues; + /// }); + /// }, + /// onChangeEnd: (RangeValues endValues) { + /// print('Ended change at $endValues'); + /// }, + /// ) + /// ``` + /// {@end-tool} + /// + /// See also: + /// + /// * [onChangeStart] for a callback that is called when a value change + /// begins. + final ValueChanged onChangeEnd; + + /// The minimum value the user can select. + /// + /// Defaults to 0.0. Must be less than or equal to [max]. + /// + /// If the [max] is equal to the [min], then the slider is disabled. + final double min; + + /// The maximum value the user can select. + /// + /// Defaults to 1.0. Must be greater than or equal to [min]. + /// + /// If the [max] is equal to the [min], then the slider is disabled. + final double max; + + /// The number of discrete divisions. + /// + /// Typically used with [labels] to show the current discrete values. + /// + /// If null, the slider is continuous. + final int divisions; + + /// Labels to show as text in the [SliderThemeData.rangeValueIndicatorShape]. + /// + /// There are two labels: one for the start thumb and one for the end thumb. + /// + /// Each label is rendered using the active [ThemeData]'s + /// [ThemeData.accentTextTheme.body2] text style, and can be overriden + /// by changing the [SliderThemeData.valueIndicatorTextStyle]. + /// + /// If null, then the value indicator will not be displayed. + /// + /// See also: + /// + /// * [RangeSliderValueIndicatorShape] for how to create a custom value + /// indicator shape. + final RangeLabels labels; + + /// The color of the track's active segment, i.e. the span of track between + /// the thumbs. + /// + /// Defaults to [ColorScheme.primary]. + /// + /// Using a [SliderTheme] gives more fine-grained control over the + /// appearance of various components of the slider. + final Color activeColor; + + /// The color of the track's inactive segments, i.e. the span of tracks + /// between the min and the start thumb, and the end thumb and the max. + /// + /// Defaults to [ColorScheme.primary] with 24% opacity. + /// + /// Using a [SliderTheme] gives more fine-grained control over the + /// appearance of various components of the slider. + final Color inactiveColor; + + /// The callback used to create a semantic value from the slider's values. + /// + /// Defaults to formatting values as a percentage. + /// + /// This is used by accessibility frameworks like TalkBack on Android to + /// inform users what the currently selected value is with more context. + /// + /// {@tool sample} + /// + /// In the example below, a slider for currency values is configured to + /// announce a value with a currency label. + /// + /// ```dart + /// RangeSlider( + /// values: _dollarsRange, + /// min: 20.0, + /// max: 330.0, + /// onChanged: (RangeValues newValues) { + /// setState(() { + /// _dollarsRange = newValues; + /// }); + /// }, + /// semanticFormatterCallback: (RangeValues rangeValues) { + /// return '${rangeValues.start.round()} - ${rangeValues.end.round()} dollars'; + /// } + /// ) + /// ``` + /// {@end-tool} + final RangeSemanticFormatterCallback semanticFormatterCallback; + + // Touch width for the tap boundary of the slider thumbs. + static const double _minTouchTargetWidth = 48; + + @override + _RangeSliderState createState() => _RangeSliderState(); +} + +class _RangeSliderState extends State with TickerProviderStateMixin { + static const Duration enableAnimationDuration = Duration(milliseconds: 75); + static const Duration valueIndicatorAnimationDuration = Duration(milliseconds: 100); + + // Animation controller that is run when the overlay (a.k.a radial reaction) + // changes visibility in response to user interaction. + AnimationController overlayController; + + // Animation controller that is run when the value indicators change visibility. + AnimationController valueIndicatorController; + + // Animation controller that is run when enabling/disabling the slider. + AnimationController enableController; + + // Animation controllers that are run when transitioning between one value + // and the next on a discrete slider. + AnimationController startPositionController; + AnimationController endPositionController; + Timer interactionTimer; + + @override + void initState() { + super.initState(); + overlayController = AnimationController( + duration: kRadialReactionDuration, + vsync: this, + ); + valueIndicatorController = AnimationController( + duration: valueIndicatorAnimationDuration, + vsync: this, + ); + enableController = AnimationController( + duration: enableAnimationDuration, + vsync: this, + value: widget.onChanged != null ? 1.0 : 0.0 + ); + startPositionController = AnimationController( + duration: Duration.zero, + vsync: this, + value: _unlerp(widget.values.start) + ); + endPositionController = AnimationController( + duration: Duration.zero, + vsync: this, + value: _unlerp(widget.values.end) + ); + } + + @override + void didUpdateWidget(RangeSlider oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.onChanged == widget.onChanged) + return; + final bool wasEnabled = oldWidget.onChanged != null; + final bool isEnabled = widget.onChanged != null; + if (wasEnabled != isEnabled) { + if (isEnabled) { + enableController.forward(); + } else { + enableController.reverse(); + } + } + } + + @override + void dispose() { + interactionTimer?.cancel(); + overlayController.dispose(); + valueIndicatorController.dispose(); + enableController.dispose(); + startPositionController.dispose(); + endPositionController.dispose(); + super.dispose(); + } + + void _handleChanged(RangeValues values) { + assert(widget.onChanged != null); + final RangeValues lerpValues = _lerpRangeValues(values); + if (lerpValues != widget.values) { + widget.onChanged(lerpValues); + } + } + + void _handleDragStart(RangeValues values) { + assert(widget.onChangeStart != null); + widget.onChangeStart(_lerpRangeValues(values)); + } + + void _handleDragEnd(RangeValues values) { + assert(widget.onChangeEnd != null); + widget.onChangeEnd(_lerpRangeValues(values)); + } + + // Returns a number between min and max, proportional to value, which must + // be between 0.0 and 1.0. + double _lerp(double value) => ui.lerpDouble(widget.min, widget.max, value); + + // Returns a new range value with the start and end lerped. + RangeValues _lerpRangeValues(RangeValues values) { + return RangeValues(_lerp(values.start), _lerp(values.end)); + } + + // Returns a number between 0.0 and 1.0, given a value between min and max. + double _unlerp(double value) { + assert(value <= widget.max); + assert(value >= widget.min); + return widget.max > widget.min ? (value - widget.min) / (widget.max - widget.min) : 0.0; + } + + // Returns a new range value with the start and end unlerped. + RangeValues _unlerpRangeValues(RangeValues values) { + return RangeValues(_unlerp(values.start), _unlerp(values.end)); + } + + // Finds closest thumb. If the thumbs are close to each other, no thumb is + // immediately selected while the drag velocity is zero. If the first + // non-zero velocity is negative, then the left thumb is selected, and if its + // positive, then the right thumb is selected. + static final RangeThumbSelector _defaultRangeThumbSelector = ( + TextDirection textDirection, + RangeValues values, + double tapValue, + Size thumbSize, + Size trackSize, + double dx, // drag velocity + ) { + final double touchRadius = math.max(thumbSize.width, RangeSlider._minTouchTargetWidth) / 2; + final bool inStartTouchTarget = (tapValue - values.start).abs() * trackSize.width < touchRadius; + final bool inEndTouchTarget = (tapValue - values.end).abs() * trackSize.width < touchRadius; + + // Use the velocity if the thumb touch targets overlap. If dx is 0 and the + // the drag position is in both touch targets, no thumb is selected because + // it is ambiguous to which thumb should be selected. Once the drag + // velocity is non-zero, the thumb selection can be determined. + if (inStartTouchTarget && inEndTouchTarget) { + bool towardsStart; + bool towardsEnd; + switch (textDirection) { + case TextDirection.ltr: + towardsStart = dx < 0; + towardsEnd = dx > 0; + break; + case TextDirection.rtl: + towardsStart = dx > 0; + towardsEnd = dx < 0; + break; + } + if (towardsStart) + return Thumb.start; + if (towardsEnd) + return Thumb.end; + } else { + // Snap position on the track if its in the inactive range. + if (tapValue < values.start || inStartTouchTarget) + return Thumb.start; + if (tapValue > values.end || inEndTouchTarget) + return Thumb.end; + } + return null; + }; + + static const double _defaultTrackHeight = 2; + static const RangeSliderTrackShape _defaultTrackShape = RoundedRectRangeSliderTrackShape(); + static const RangeSliderTickMarkShape _defaultTickMarkShape = RoundRangeSliderTickMarkShape(); + static const SliderComponentShape _defaultOverlayShape = RoundSliderOverlayShape(); + static const RangeSliderThumbShape _defaultThumbShape = RoundRangeSliderThumbShape(); + static const RangeSliderValueIndicatorShape _defaultValueIndicatorShape = PaddleRangeSliderValueIndicatorShape(); + static const ShowValueIndicator _defaultShowValueIndicator = ShowValueIndicator.onlyForDiscrete; + static const double _defaultMinThumbSeparation = 8; + + @override + Widget build(BuildContext context) { + assert(debugCheckHasMaterial(context)); + assert(debugCheckHasMediaQuery(context)); + + final ThemeData theme = Theme.of(context); + SliderThemeData sliderTheme = SliderTheme.of(context); + + // If the widget has active or inactive colors specified, then we plug them + // in to the slider theme as best we can. If the developer wants more + // control than that, then they need to use a SliderTheme. The default + // colors come from the ThemeData.colorScheme. These colors, along with + // the default shapes and text styles are aligned to the Material + // Guidelines. + sliderTheme = sliderTheme.copyWith( + trackHeight: sliderTheme.trackHeight ?? _defaultTrackHeight, + activeTrackColor: widget.activeColor ?? sliderTheme.activeTrackColor ?? theme.colorScheme.primary, + inactiveTrackColor: widget.inactiveColor ?? sliderTheme.inactiveTrackColor ?? theme.colorScheme.primary.withOpacity(0.24), + disabledActiveTrackColor: sliderTheme.disabledActiveTrackColor ?? theme.colorScheme.onSurface.withOpacity(0.32), + disabledInactiveTrackColor: sliderTheme.disabledInactiveTrackColor ?? theme.colorScheme.onSurface.withOpacity(0.12), + activeTickMarkColor: widget.inactiveColor ?? sliderTheme.activeTickMarkColor ?? theme.colorScheme.onPrimary.withOpacity(0.54), + inactiveTickMarkColor: widget.activeColor ?? sliderTheme.inactiveTickMarkColor ?? theme.colorScheme.primary.withOpacity(0.54), + disabledActiveTickMarkColor: sliderTheme.disabledActiveTickMarkColor ?? theme.colorScheme.onPrimary.withOpacity(0.12), + disabledInactiveTickMarkColor: sliderTheme.disabledInactiveTickMarkColor ?? theme.colorScheme.onSurface.withOpacity(0.12), + thumbColor: widget.activeColor ?? sliderTheme.thumbColor ?? theme.colorScheme.primary, + overlappingShapeStrokeColor: sliderTheme.overlappingShapeStrokeColor ?? theme.colorScheme.surface, + disabledThumbColor: sliderTheme.disabledThumbColor ?? theme.colorScheme.onSurface.withOpacity(0.38), + overlayColor: widget.activeColor?.withOpacity(0.12) ?? sliderTheme.overlayColor ?? theme.colorScheme.primary.withOpacity(0.12), + valueIndicatorColor: widget.activeColor ?? sliderTheme.valueIndicatorColor ?? theme.colorScheme.primary, + rangeTrackShape: sliderTheme.rangeTrackShape ?? _defaultTrackShape, + rangeTickMarkShape: sliderTheme.rangeTickMarkShape ?? _defaultTickMarkShape, + rangeThumbShape: sliderTheme.rangeThumbShape ?? _defaultThumbShape, + overlayShape: sliderTheme.overlayShape ?? _defaultOverlayShape, + rangeValueIndicatorShape: sliderTheme.rangeValueIndicatorShape ?? _defaultValueIndicatorShape, + showValueIndicator: sliderTheme.showValueIndicator ?? _defaultShowValueIndicator, + valueIndicatorTextStyle: sliderTheme.valueIndicatorTextStyle ?? theme.textTheme.body2.copyWith( + color: theme.colorScheme.onPrimary, + ), + minThumbSeparation: sliderTheme.minThumbSeparation ?? _defaultMinThumbSeparation, + thumbSelector: sliderTheme.thumbSelector ?? _defaultRangeThumbSelector, + ); + + return _RangeSliderRenderObjectWidget( + values: _unlerpRangeValues(widget.values), + divisions: widget.divisions, + labels: widget.labels, + sliderTheme: sliderTheme, + textScaleFactor: MediaQuery.of(context).textScaleFactor, + onChanged: (widget.onChanged != null) && (widget.max > widget.min) ? _handleChanged : null, + onChangeStart: widget.onChangeStart != null ? _handleDragStart : null, + onChangeEnd: widget.onChangeEnd != null ? _handleDragEnd : null, + state: this, + semanticFormatterCallback: widget.semanticFormatterCallback, + ); + } +} + +class _RangeSliderRenderObjectWidget extends LeafRenderObjectWidget { + const _RangeSliderRenderObjectWidget({ + Key key, + this.values, + this.divisions, + this.labels, + this.sliderTheme, + this.textScaleFactor, + this.onChanged, + this.onChangeStart, + this.onChangeEnd, + this.state, + this.semanticFormatterCallback, + }) : super(key: key); + + final RangeValues values; + final int divisions; + final RangeLabels labels; + final SliderThemeData sliderTheme; + final double textScaleFactor; + final ValueChanged onChanged; + final ValueChanged onChangeStart; + final ValueChanged onChangeEnd; + final RangeSemanticFormatterCallback semanticFormatterCallback; + final _RangeSliderState state; + + @override + _RenderRangeSlider createRenderObject(BuildContext context) { + return _RenderRangeSlider( + values: values, + divisions: divisions, + labels: labels, + sliderTheme: sliderTheme, + theme: Theme.of(context), + textScaleFactor: textScaleFactor, + onChanged: onChanged, + onChangeStart: onChangeStart, + onChangeEnd: onChangeEnd, + state: state, + textDirection: Directionality.of(context), + semanticFormatterCallback: semanticFormatterCallback, + platform: Theme.of(context).platform, + ); + } + + @override + void updateRenderObject(BuildContext context, _RenderRangeSlider renderObject) { + renderObject + ..values = values + ..divisions = divisions + ..labels = labels + ..sliderTheme = sliderTheme + ..theme = Theme.of(context) + ..textScaleFactor = textScaleFactor + ..onChanged = onChanged + ..onChangeStart = onChangeStart + ..onChangeEnd = onChangeEnd + ..textDirection = Directionality.of(context) + ..semanticFormatterCallback = semanticFormatterCallback + ..platform = Theme.of(context).platform; + } +} + +class _RenderRangeSlider extends RenderBox { + _RenderRangeSlider({ + @required RangeValues values, + int divisions, + RangeLabels labels, + SliderThemeData sliderTheme, + ThemeData theme, + double textScaleFactor, + TargetPlatform platform, + ValueChanged onChanged, + RangeSemanticFormatterCallback semanticFormatterCallback, + this.onChangeStart, + this.onChangeEnd, + @required _RangeSliderState state, + @required TextDirection textDirection, + }) : assert(values != null), + assert(values.start >= 0.0 && values.start <= 1.0), + assert(values.end >= 0.0 && values.end <= 1.0), + assert(state != null), + assert(textDirection != null), + _platform = platform, + _semanticFormatterCallback = semanticFormatterCallback, + _labels = labels, + _values = values, + _divisions = divisions, + _sliderTheme = sliderTheme, + _theme = theme, + _textScaleFactor = textScaleFactor, + _onChanged = onChanged, + _state = state, + _textDirection = textDirection { + _updateLabelPainters(); + final GestureArenaTeam team = GestureArenaTeam(); + _drag = HorizontalDragGestureRecognizer() + ..team = team + ..onStart = _handleDragStart + ..onUpdate = _handleDragUpdate + ..onEnd = _handleDragEnd + ..onCancel = _handleDragCancel; + _tap = TapGestureRecognizer() + ..team = team + ..onTapDown = _handleTapDown + ..onTapUp = _handleTapUp + ..onTapCancel = _handleTapCancel; + _overlayAnimation = CurvedAnimation( + parent: _state.overlayController, + curve: Curves.fastOutSlowIn, + ); + _valueIndicatorAnimation = CurvedAnimation( + parent: _state.valueIndicatorController, + curve: Curves.fastOutSlowIn, + ); + _enableAnimation = CurvedAnimation( + parent: _state.enableController, + curve: Curves.easeInOut, + ); + } + + // Keep track of the last selected thumb so they can be drawn in the + // right order. + Thumb _lastThumbSelection; + + static const Duration _positionAnimationDuration = Duration(milliseconds: 75); + + // This value is the touch target, 48, multiplied by 3. + static const double _minPreferredTrackWidth = 144.0; + + // Compute the largest width and height needed to paint the slider shapes, + // other than the track shape. It is assumed that these shapes are vertically + // centered on the track. + double get _maxSliderPartWidth => _sliderPartSizes.map((Size size) => size.width).reduce(math.max); + double get _maxSliderPartHeight => _sliderPartSizes.map((Size size) => size.height).reduce(math.max); + List get _sliderPartSizes => [ + _sliderTheme.overlayShape.getPreferredSize(isEnabled, isDiscrete), + _sliderTheme.rangeThumbShape.getPreferredSize(isEnabled, isDiscrete), + _sliderTheme.rangeTickMarkShape.getPreferredSize(isEnabled: isEnabled, sliderTheme: sliderTheme), + ]; + double get _minPreferredTrackHeight => _sliderTheme.trackHeight; + + // This rect is used in gesture calculations, where the gesture coordinates + // are relative to the sliders origin. Therefore, the offset is passed as + // (0,0). + Rect get _trackRect => _sliderTheme.rangeTrackShape.getPreferredRect( + parentBox: this, + offset: Offset.zero, + sliderTheme: _sliderTheme, + isDiscrete: false, + ); + + static const Duration _minimumInteractionTime = Duration(milliseconds: 500); + + _RangeSliderState _state; + Animation _overlayAnimation; + Animation _valueIndicatorAnimation; + Animation _enableAnimation; + final TextPainter _startLabelPainter = TextPainter(); + final TextPainter _endLabelPainter = TextPainter(); + HorizontalDragGestureRecognizer _drag; + TapGestureRecognizer _tap; + bool _active = false; + + bool get isEnabled => onChanged != null; + + bool get isDiscrete => divisions != null && divisions > 0; + + RangeValues get values => _values; + RangeValues _values; + set values(RangeValues newValues) { + assert(newValues != null); + assert(newValues.start != null && newValues.start >= 0.0 && newValues.start <= 1.0); + assert(newValues.end != null && newValues.end >= 0.0 && newValues.end <= 1.0); + assert(newValues.start <= newValues.end); + final RangeValues convertedValues = isDiscrete ? _discretizeRangeValues(newValues) : newValues; + if (convertedValues == _values) { + return; + } + _values = convertedValues; + if (isDiscrete) { + // Reset the duration to match the distance that we're traveling, so that + // whatever the distance, we still do it in _positionAnimationDuration, + // and if we get re-targeted in the middle, it still takes that long to + // get to the new location. + final double startDistance = (_values.start - _state.startPositionController.value).abs(); + _state.startPositionController.duration = startDistance != 0.0 ? _positionAnimationDuration * (1.0 / startDistance) : Duration.zero; + _state.startPositionController.animateTo(_values.start, curve: Curves.easeInOut); + final double endDistance = (_values.end - _state.endPositionController.value).abs(); + _state.endPositionController.duration = endDistance != 0.0 ? _positionAnimationDuration * (1.0 / endDistance) : Duration.zero; + _state.endPositionController.animateTo(_values.end, curve: Curves.easeInOut); + } else { + _state.startPositionController.value = convertedValues.start; + _state.endPositionController.value = convertedValues.end; + } + markNeedsSemanticsUpdate(); + } + + TargetPlatform _platform; + TargetPlatform get platform => _platform; + set platform(TargetPlatform value) { + if (_platform == value) + return; + _platform = value; + markNeedsSemanticsUpdate(); + } + + RangeSemanticFormatterCallback _semanticFormatterCallback; + RangeSemanticFormatterCallback get semanticFormatterCallback => _semanticFormatterCallback; + set semanticFormatterCallback(RangeSemanticFormatterCallback value) { + if (_semanticFormatterCallback == value) + return; + _semanticFormatterCallback = value; + markNeedsSemanticsUpdate(); + } + + int get divisions => _divisions; + int _divisions; + set divisions(int value) { + if (value == _divisions) { + return; + } + _divisions = value; + markNeedsPaint(); + } + + RangeLabels get labels => _labels; + RangeLabels _labels; + set labels(RangeLabels labels) { + if (labels == _labels) + return; + _labels = labels; + _updateLabelPainters(); + } + + SliderThemeData get sliderTheme => _sliderTheme; + SliderThemeData _sliderTheme; + set sliderTheme(SliderThemeData value) { + if (value == _sliderTheme) + return; + _sliderTheme = value; + markNeedsPaint(); + } + + ThemeData get theme => _theme; + ThemeData _theme; + set theme(ThemeData value) { + if (value == _theme) + return; + _theme = value; + markNeedsPaint(); + } + + double get textScaleFactor => _textScaleFactor; + double _textScaleFactor; + set textScaleFactor(double value) { + if (value == _textScaleFactor) + return; + _textScaleFactor = value; + _updateLabelPainters(); + } + + ValueChanged get onChanged => _onChanged; + ValueChanged _onChanged; + set onChanged(ValueChanged value) { + if (value == _onChanged) + return; + final bool wasEnabled = isEnabled; + _onChanged = value; + if (wasEnabled != isEnabled) { + markNeedsPaint(); + markNeedsSemanticsUpdate(); + } + } + + ValueChanged onChangeStart; + ValueChanged onChangeEnd; + + TextDirection get textDirection => _textDirection; + TextDirection _textDirection; + set textDirection(TextDirection value) { + assert(value != null); + if (value == _textDirection) + return; + _textDirection = value; + _updateLabelPainters(); + } + + bool get showValueIndicator { + bool showValueIndicator; + switch (_sliderTheme.showValueIndicator) { + case ShowValueIndicator.onlyForDiscrete: + showValueIndicator = isDiscrete; + break; + case ShowValueIndicator.onlyForContinuous: + showValueIndicator = !isDiscrete; + break; + case ShowValueIndicator.always: + showValueIndicator = true; + break; + case ShowValueIndicator.never: + showValueIndicator = false; + break; + } + return showValueIndicator; + } + + Size get _thumbSize => _sliderTheme.rangeThumbShape.getPreferredSize(isEnabled, isDiscrete); + + double get _adjustmentUnit { + switch (_platform) { + case TargetPlatform.iOS: + // Matches iOS implementation of material slider. + return 0.1; + case TargetPlatform.android: + case TargetPlatform.fuchsia: + default: + // Matches Android implementation of material slider. + return 0.05; + } + } + + void _updateLabelPainters() { + _updateLabelPainter(Thumb.start); + _updateLabelPainter(Thumb.end); + } + + void _updateLabelPainter(Thumb thumb) { + if (labels == null) + return; + + String text; + TextPainter labelPainter; + switch (thumb) { + case Thumb.start: + text = labels.start; + labelPainter = _startLabelPainter; + break; + case Thumb.end: + text = labels.end; + labelPainter = _endLabelPainter; + break; + } + + if (labels != null) { + labelPainter + ..text = TextSpan( + style: _sliderTheme.valueIndicatorTextStyle, + text: text, + ) + ..textDirection = textDirection + ..textScaleFactor = textScaleFactor + ..layout(); + } else { + labelPainter.text = null; + } + // Changing the textDirection can result in the layout changing, because the + // bidi algorithm might line up the glyphs differently which can result in + // different ligatures, different shapes, etc. So we always markNeedsLayout. + markNeedsLayout(); + } + + @override + void attach(PipelineOwner owner) { + super.attach(owner); + _overlayAnimation.addListener(markNeedsPaint); + _valueIndicatorAnimation.addListener(markNeedsPaint); + _enableAnimation.addListener(markNeedsPaint); + _state.startPositionController.addListener(markNeedsPaint); + _state.endPositionController.addListener(markNeedsPaint); + } + + @override + void detach() { + _overlayAnimation.removeListener(markNeedsPaint); + _valueIndicatorAnimation.removeListener(markNeedsPaint); + _enableAnimation.removeListener(markNeedsPaint); + _state.startPositionController.removeListener(markNeedsPaint); + _state.endPositionController.removeListener(markNeedsPaint); + super.detach(); + } + + double _getValueFromVisualPosition(double visualPosition) { + switch (textDirection) { + case TextDirection.rtl: + return 1.0 - visualPosition; + case TextDirection.ltr: + return visualPosition; + } + return null; + } + + double _getValueFromGlobalPosition(Offset globalPosition) { + final double visualPosition = (globalToLocal(globalPosition).dx - _trackRect.left) / _trackRect.width; + return _getValueFromVisualPosition(visualPosition); + } + + double _discretize(double value) { + double result = value.clamp(0.0, 1.0); + if (isDiscrete) { + result = (result * divisions).round() / divisions; + } + return result; + } + + RangeValues _discretizeRangeValues(RangeValues values) { + return RangeValues(_discretize(values.start), _discretize(values.end)); + } + + void _startInteraction(Offset globalPosition) { + final double tapValue = _getValueFromGlobalPosition(globalPosition).clamp(0.0, 1.0); + _lastThumbSelection = sliderTheme.thumbSelector(textDirection, values, tapValue, _thumbSize, size, 0); + + if (_lastThumbSelection != null) { + _active = true; + // We supply the *current* values as the start locations, so that if we have + // a tap, it consists of a call to onChangeStart with the previous value and + // a call to onChangeEnd with the new value. + final RangeValues currentValues = _discretizeRangeValues(values); + RangeValues newValues; + if (_lastThumbSelection == Thumb.start) { + newValues = RangeValues(tapValue, currentValues.end); + } else if (_lastThumbSelection == Thumb.end) { + newValues = RangeValues(currentValues.start, tapValue); + } + _updateLabelPainter(_lastThumbSelection); + + if (onChangeStart != null) { + onChangeStart(currentValues); + } + + onChanged(_discretizeRangeValues(newValues)); + + _state.overlayController.forward(); + if (showValueIndicator) { + _state.valueIndicatorController.forward(); + _state.interactionTimer?.cancel(); + _state.interactionTimer = + Timer(_minimumInteractionTime * timeDilation, () { + _state.interactionTimer = null; + if (!_active && _state.valueIndicatorController.status == AnimationStatus.completed) { + _state.valueIndicatorController.reverse(); + } + }); + } + } + } + + void _handleDragUpdate(DragUpdateDetails details) { + final double dragValue = _getValueFromGlobalPosition(details.globalPosition); + + // If no selection has been made yet, use the velocity of the drag to + // determine which thumb should be selected. + if (_lastThumbSelection == null) { + _lastThumbSelection = sliderTheme.thumbSelector(textDirection, values, dragValue, _thumbSize, size, details.delta.dx); + if (_lastThumbSelection != null) { + _state.overlayController.forward(); + if (showValueIndicator) { + _state.valueIndicatorController.forward(); + } + } + } + + if (isEnabled && _lastThumbSelection != null) { + final RangeValues currentValues = _discretizeRangeValues(values); + final double currentDragValue = _discretize(dragValue); + + RangeValues newValues; + final double minThumbSeparationValue = isDiscrete ? 0 : sliderTheme.minThumbSeparation / _trackRect.width; + if (_lastThumbSelection == Thumb.start) { + newValues = RangeValues(math.min(currentDragValue, currentValues.end - minThumbSeparationValue), currentValues.end); + } else if (_lastThumbSelection == Thumb.end) { + newValues = RangeValues(currentValues.start, math.max(currentDragValue, currentValues.start + minThumbSeparationValue)); + } + onChanged(newValues); + } + } + + void _endInteraction() { + _state.overlayController.reverse(); + if (showValueIndicator && _state.interactionTimer == null) { + _state.valueIndicatorController.reverse(); + } + + if (_active && _state.mounted) { + if (_lastThumbSelection == null) + return; + final RangeValues discreteValues = _discretizeRangeValues(values); + if (onChangeEnd != null) { + onChangeEnd(discreteValues); + } + _active = false; + } + } + + void _handleDragStart(DragStartDetails details) { + _startInteraction(details.globalPosition); + } + + void _handleDragEnd(DragEndDetails details) { + _endInteraction(); + } + + void _handleDragCancel() { + _endInteraction(); + } + + void _handleTapDown(TapDownDetails details) { + _startInteraction(details.globalPosition); + } + + void _handleTapUp(TapUpDetails details) { + _endInteraction(); + } + + void _handleTapCancel() { + _endInteraction(); + } + + @override + bool hitTestSelf(Offset position) => true; + + @override + void handleEvent(PointerEvent event, HitTestEntry entry) { + assert(debugHandleEvent(event, entry)); + if (event is PointerDownEvent && isEnabled) { + // We need to add the drag first so that it has priority. + _drag.addPointer(event); + _tap.addPointer(event); + } + } + + @override + double computeMinIntrinsicWidth(double height) => _minPreferredTrackWidth + _maxSliderPartWidth; + + @override + double computeMaxIntrinsicWidth(double height) => _minPreferredTrackWidth + _maxSliderPartWidth; + + @override + double computeMinIntrinsicHeight(double width) => math.max(_minPreferredTrackHeight, _maxSliderPartHeight); + + @override + double computeMaxIntrinsicHeight(double width) => math.max(_minPreferredTrackHeight, _maxSliderPartHeight); + + @override + bool get sizedByParent => true; + + @override + void performResize() { + size = Size( + constraints.hasBoundedWidth ? constraints.maxWidth : _minPreferredTrackWidth + _maxSliderPartWidth, + constraints.hasBoundedHeight ? constraints.maxHeight : math.max(_minPreferredTrackHeight, _maxSliderPartHeight), + ); + } + + @override + void paint(PaintingContext context, Offset offset) { + final double startValue = _state.startPositionController.value; + final double endValue = _state.endPositionController.value; + + // The visual position is the position of the thumb from 0 to 1 from left + // to right. In left to right, this is the same as the value, but it is + // reversed for right to left text. + double startVisualPosition; + double endVisualPosition; + switch (textDirection) { + case TextDirection.rtl: + startVisualPosition = 1.0 - startValue; + endVisualPosition = 1.0 - endValue; + break; + case TextDirection.ltr: + startVisualPosition = startValue; + endVisualPosition = endValue; + break; + } + + final Rect trackRect = _sliderTheme.rangeTrackShape.getPreferredRect( + parentBox: this, + offset: offset, + sliderTheme: _sliderTheme, + isDiscrete: isDiscrete + ); + final Offset startThumbCenter = Offset(trackRect.left + startVisualPosition * trackRect.width, trackRect.center.dy); + final Offset endThumbCenter = Offset(trackRect.left + endVisualPosition * trackRect.width, trackRect.center.dy); + + _sliderTheme.rangeTrackShape.paint( + context, + offset, + parentBox: this, + sliderTheme: _sliderTheme, + enableAnimation: _enableAnimation, + textDirection: _textDirection, + startThumbCenter: startThumbCenter, + endThumbCenter: endThumbCenter, + isDiscrete: isDiscrete, + isEnabled: isEnabled + ); + + if (!_overlayAnimation.isDismissed) { + if (_lastThumbSelection == Thumb.start) { + _sliderTheme.overlayShape.paint( + context, + startThumbCenter, + activationAnimation: _overlayAnimation, + enableAnimation: _enableAnimation, + isDiscrete: isDiscrete, + labelPainter: _startLabelPainter, + parentBox: this, + sliderTheme: _sliderTheme, + textDirection: _textDirection, + value: startValue, + ); + } + if (_lastThumbSelection == Thumb.end) { + _sliderTheme.overlayShape.paint( + context, + endThumbCenter, + activationAnimation: _overlayAnimation, + enableAnimation: _enableAnimation, + isDiscrete: isDiscrete, + labelPainter: _endLabelPainter, + parentBox: this, + sliderTheme: _sliderTheme, + textDirection: _textDirection, + value: endValue, + ); + } + } + + if (isDiscrete) { + final double tickMarkWidth = _sliderTheme.rangeTickMarkShape.getPreferredSize( + isEnabled: isEnabled, + sliderTheme: _sliderTheme, + ).width; + final double adjustedTrackWidth = trackRect.width - tickMarkWidth; + // If the tick marks would be too dense, don't bother painting them. + if (adjustedTrackWidth / divisions >= 3.0 * tickMarkWidth) { + final double dy = trackRect.center.dy; + for (int i = 0; i <= divisions; i++) { + final double value = i / divisions; + // The ticks are mapped to be within the track, so the tick mark width + // must be subtracted from the track width. + final double dx = trackRect.left + value * adjustedTrackWidth + tickMarkWidth / 2; + final Offset tickMarkOffset = Offset(dx, dy); + _sliderTheme.rangeTickMarkShape.paint( + context, + tickMarkOffset, + parentBox: this, + sliderTheme: _sliderTheme, + enableAnimation: _enableAnimation, + textDirection: _textDirection, + startThumbCenter: startThumbCenter, + endThumbCenter: endThumbCenter, + isEnabled: isEnabled, + ); + } + } + } + + final double thumbDelta = (endThumbCenter.dx - startThumbCenter.dx).abs(); + + final bool isLastThumbStart = _lastThumbSelection == Thumb.start; + final Thumb bottomThumb = isLastThumbStart ? Thumb.end : Thumb.start; + final Offset bottomThumbCenter = isLastThumbStart ? endThumbCenter : startThumbCenter; + final Offset topThumbCenter = isLastThumbStart ? startThumbCenter : endThumbCenter; + final TextPainter bottomLabelPainter = isLastThumbStart ? _endLabelPainter : _startLabelPainter; + final TextPainter topLabelPainter = isLastThumbStart ? _startLabelPainter : _endLabelPainter; + final double bottomValue = isLastThumbStart ? endValue : startValue; + final double topValue = isLastThumbStart ? startValue : endValue; + + if (isEnabled && labels != null && !_valueIndicatorAnimation.isDismissed && showValueIndicator) { + _sliderTheme.rangeValueIndicatorShape.paint( + context, + bottomThumbCenter, + activationAnimation: _valueIndicatorAnimation, + enableAnimation: _enableAnimation, + isDiscrete: isDiscrete, + isOnTop: false, + labelPainter: bottomLabelPainter, + parentBox: this, + sliderTheme: _sliderTheme, + textDirection: _textDirection, + thumb: bottomThumb, + value: bottomValue, + ); + _sliderTheme.rangeValueIndicatorShape.paint( + context, + topThumbCenter, + activationAnimation: _valueIndicatorAnimation, + enableAnimation: _enableAnimation, + isDiscrete: isDiscrete, + isOnTop: thumbDelta < sliderTheme.rangeValueIndicatorShape.getPreferredSize(isEnabled, isDiscrete, labelPainter: topLabelPainter).width, + labelPainter: topLabelPainter, + parentBox: this, + sliderTheme: _sliderTheme, + textDirection: _textDirection, + thumb: _lastThumbSelection, + value: topValue, + ); + } + + _sliderTheme.rangeThumbShape.paint( + context, + bottomThumbCenter, + activationAnimation: _valueIndicatorAnimation, + enableAnimation: _enableAnimation, + isDiscrete: isDiscrete, + isOnTop: false, + sliderTheme: _sliderTheme, + thumb: bottomThumb, + ); + _sliderTheme.rangeThumbShape.paint( + context, + topThumbCenter, + activationAnimation: _valueIndicatorAnimation, + enableAnimation: _enableAnimation, + isDiscrete: isDiscrete, + isOnTop: thumbDelta < sliderTheme.rangeThumbShape.getPreferredSize(isEnabled, isDiscrete).width, + sliderTheme: _sliderTheme, + thumb: _lastThumbSelection, + ); + } + + @override + void describeSemanticsConfiguration(SemanticsConfiguration config) { + super.describeSemanticsConfiguration(config); + + config.isSemanticBoundary = isEnabled; + if (isEnabled) { + config.textDirection = textDirection; + config.customSemanticsActions = { + _decreaseStart: _decreaseStartAction, + _increaseStart: _increaseStartAction, + _decreaseEnd: _decreaseEndAction, + _increaseEnd: _increaseEndAction, + }; + if (semanticFormatterCallback != null) { + config.value = semanticFormatterCallback(_state._lerpRangeValues(values)); + } else { + config.value = values.toString(); + } + } + } + + final CustomSemanticsAction _decreaseStart = const CustomSemanticsAction(label: 'Decrease Min'); + final CustomSemanticsAction _increaseStart = const CustomSemanticsAction(label: 'Increase Min'); + final CustomSemanticsAction _decreaseEnd = const CustomSemanticsAction(label: 'Decrease Max'); + final CustomSemanticsAction _increaseEnd = const CustomSemanticsAction(label: 'Increase Max'); + + double get _semanticActionUnit => divisions != null ? 1.0 / divisions : _adjustmentUnit; + + void _increaseStartAction() { + if (isEnabled) { + onChanged(RangeValues(_increaseValue(values.start), values.end)); + } + } + + void _decreaseStartAction() { + if (isEnabled) { + onChanged(RangeValues(_decreaseValue(values.start), values.end)); + } + } + + void _increaseEndAction() { + if (isEnabled) { + onChanged(RangeValues(values.start, _increaseValue(values.end))); + } + } + + void _decreaseEndAction() { + if (isEnabled) { + onChanged(RangeValues(values.start, _decreaseValue(values.end))); + } + } + + double _increaseValue(double value) { + return (value + _semanticActionUnit).clamp(0.0, 1.0); + } + + double _decreaseValue(double value) { + return (value - _semanticActionUnit).clamp(0.0, 1.0); + } +} \ No newline at end of file diff --git a/packages/flutter/lib/src/material/slider_theme.dart b/packages/flutter/lib/src/material/slider_theme.dart index 8105ed7764..34caf58781 100644 --- a/packages/flutter/lib/src/material/slider_theme.dart +++ b/packages/flutter/lib/src/material/slider_theme.dart @@ -9,9 +9,106 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; +import 'colors.dart'; import 'theme.dart'; import 'theme_data.dart'; +/// {@template flutter.material.slider.seeAlso.sliderComponentShape} +/// * [SliderComponentShape], which can be used to create custom shapes for +/// the [Slider]'s thumb, overlay, and value indicator and the +/// [RangeSlider]'s overlay. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.sliderTrackShape} +/// * [SliderTrackShape], which can be used to create custom shapes for the +/// [Slider]'s track. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.sliderTickMarkShape} +/// * [SliderTickMarkShape], which can be used to create custom shapes for the +/// [Slider]'s tick marks. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.rangeSliderThumbShape} +/// * [RangeSliderThumbShape], which can be used to create custom shapes for +/// the [RangeSlider]'s thumb. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.rangeSliderValueIndicatorShape} +/// * [RangeSliderValueIndicatorShape], which can be used to create custom +/// shapes for the [RangeSlider]'s value indicator. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.rangeSliderTrackShape} +/// * [RangeSliderTrackShape], which can be used to create custom shapes for +/// the [RangeSlider]'s track. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.rangeSliderTickMarkShape} +/// * [RangeSliderTickMarkShape], which can be used to create custom shapes for +/// the [RangeSlider]'s tick marks. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.roundSliderThumbShape} +/// * [RoundSliderThumbShape], which is the default [Slider]'s thumb shape that +/// paints a solid circle. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.roundSliderOverlayShape} +/// * [RoundSliderOverlayShape], which is the default [Slider] and +/// [RangeSlider]'s overlay shape that paints a transparent circle. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.paddleSliderValueIndicatorShape} +/// * [PaddleSliderValueIndicatorShape], which is the default [Slider]'s value +/// indicator shape that paints a custom path with text in it. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.roundSliderTickMarkShape} +/// * [RoundSliderTickMarkShape], which is the the default [Slider]'s tick mark +/// shape that paints a solid circle. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.roundedRectSliderTrackShape} +/// * [RoundedRectSliderTrackShape] for the default [Slider]'s track shape that +/// paints a stadium-like track. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.roundRangeSliderThumbShape} +/// * [RoundRangeSliderThumbShape] for the default [RangeSlider]'s thumb shape +/// that paints a solid circle. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.paddleRangeSliderValueIndicatorShape} +/// * [PaddleRangeSliderValueIndicatorShape] for the default [RangeSlider]'s +/// value indicator shape that paints a custom path with text in it. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.roundRangeSliderTickMarkShape} +/// * [RoundRangeSliderTickMarkShape] for the default [RangeSlider]'s tick mark +/// shape that paints a solid circle. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.seeAlso.roundedRectRangeSliderTrackShape} +/// * [RoundedRectRangeSliderTrackShape] for the default [RangeSlider]'s track +/// shape that paints a stadium-like track. +/// {@endtemplate} +/// +/// {@template flutter.material.slider.trackSegment} +/// The track segment between the start of the slider and the thumb is the +/// active track segment. The track segment between the thumb and the end of the +/// slider is the inactive track segment. In [TextDirection.ltr], the start of +/// the slider is on the left, and in [TextDirection.rtl], the start of the +/// slider is on the right. +/// {@endtemplate} +/// +/// {@template flutter.material.rangeSlider.trackSegment} +/// The track segment between the two thumbs is the active track segment. The +/// track segments between the thumb and each end of the slider are the inactive +/// track segments. In [TextDirection.ltr], the start of the slider is on the +/// left, and in [TextDirection.rtl], the start of the slider is on the right. +/// {@endtemplate} + /// Applies a slider theme to descendant [Slider] widgets. /// /// A slider theme describes the colors and shape choices of the slider @@ -30,12 +127,13 @@ import 'theme_data.dart'; /// /// * [SliderThemeData], which describes the actual configuration of a slider /// theme. -/// * [SliderComponentShape], which can be used to create custom shapes for -/// the slider thumb, overlay, and value indicator. -/// * [SliderTrackShape], which can be used to create custom shapes for the -/// slider track. -/// * [SliderTickMarkShape], which can be used to create custom shapes for the -/// slider tick marks. +/// {@macro flutter.material.slider.seeAlso.sliderComponentShape} +/// {@macro flutter.material.slider.seeAlso.sliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.sliderTickMarkShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderThumbShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderValueIndicatorShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTickMarkShape} class SliderTheme extends InheritedWidget { /// Applies the given theme [data] to [child]. /// @@ -119,6 +217,21 @@ enum ShowValueIndicator { never, } +/// Identifier for a thumb. +/// +/// There are 2 thumbs in a [RangeSlider], [start] and [end]. +/// +/// For [TextDirection.ltr], the [start] thumb is the left-most thumb and the +/// [end] thumb is the right-most thumb. For [TextDirection.rtl] the [start] +/// thumb is the right-most thumb, and the [end] thumb is the left-most thumb. +enum Thumb { + /// Left-most thumb for [TextDirection.ltr], otherwise, right-most thumb. + start, + + /// Right-most thumb for [TextDirection.ltr], otherwise, left-most thumb. + end, +} + /// Holds the color, shape, and typography values for a material design slider /// theme. /// @@ -127,6 +240,14 @@ enum ShowValueIndicator { /// /// To obtain the current ambient slider theme, use [SliderTheme.of]. /// +/// This theme is for both the [Slider] and the [RangeSlider]. The properties +/// that are only for the [Slider] are: [tickMarkShape], [thumbShape], +/// [trackShape], and [valueIndicatorShape]. The properties that are only for +/// the [RangeSlider] are [rangeTickMarkShape], [rangeThumbShape], +/// [rangeTrackShape], [rangeValueIndicatorShape], +/// [overlappingShapeStrokeColor], [minThumbSeparation], and [thumbSelector]. +/// All other properties are used by both the [Slider] and the [RangeSlider]. +/// /// The parts of a slider are: /// /// * The "thumb", which is a shape that slides horizontally when the user @@ -171,9 +292,13 @@ enum ShowValueIndicator { /// * [Theme] widget, which performs a similar function to [SliderTheme], /// but for overall themes. /// * [ThemeData], which has a default [SliderThemeData]. -/// * [SliderTrackShape], to define custom slider track shapes. -/// * [SliderComponentShape], to define custom slider component shapes. -/// * [SliderTickMarkShape], to define custom slider tick mark shapes. +/// {@macro flutter.material.slider.seeAlso.sliderComponentShape} +/// {@macro flutter.material.slider.seeAlso.sliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.sliderTickMarkShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderThumbShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderValueIndicatorShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTickMarkShape} class SliderThemeData extends Diagnosticable { /// Create a [SliderThemeData] given a set of exact values. All the values /// must be specified. @@ -220,16 +345,23 @@ class SliderThemeData extends Diagnosticable { this.disabledActiveTickMarkColor, this.disabledInactiveTickMarkColor, this.thumbColor, + this.overlappingShapeStrokeColor, this.disabledThumbColor, this.overlayColor, this.valueIndicatorColor, - this.trackShape, + this.overlayShape, this.tickMarkShape, this.thumbShape, - this.overlayShape, + this.trackShape, this.valueIndicatorShape, + this.rangeTickMarkShape, + this.rangeThumbShape, + this.rangeTrackShape, + this.rangeValueIndicatorShape, this.showValueIndicator, this.valueIndicatorTextStyle, + this.minThumbSeparation, + this.thumbSelector, }); /// Generates a SliderThemeData from three main colors. @@ -278,14 +410,19 @@ class SliderThemeData extends Diagnosticable { disabledActiveTickMarkColor: primaryColorLight.withAlpha(disabledActiveTickMarkAlpha), disabledInactiveTickMarkColor: primaryColorDark.withAlpha(disabledInactiveTickMarkAlpha), thumbColor: primaryColor.withAlpha(thumbAlpha), + overlappingShapeStrokeColor: Colors.white, disabledThumbColor: primaryColorDark.withAlpha(disabledThumbAlpha), overlayColor: primaryColor.withAlpha(overlayAlpha), valueIndicatorColor: primaryColor.withAlpha(valueIndicatorAlpha), - trackShape: const RoundedRectSliderTrackShape(), + overlayShape: const RoundSliderOverlayShape(), tickMarkShape: const RoundSliderTickMarkShape(), thumbShape: const RoundSliderThumbShape(), - overlayShape: const RoundSliderOverlayShape(), + trackShape: const RectangularSliderTrackShape(), valueIndicatorShape: const PaddleSliderValueIndicatorShape(), + rangeTickMarkShape: const RoundRangeSliderTickMarkShape(), + rangeThumbShape: const RoundRangeSliderThumbShape(), + rangeTrackShape: const RoundedRectRangeSliderTrackShape(), + rangeValueIndicatorShape: const PaddleRangeSliderValueIndicatorShape(), valueIndicatorTextStyle: valueIndicatorTextStyle, showValueIndicator: ShowValueIndicator.onlyForDiscrete, ); @@ -330,6 +467,11 @@ class SliderThemeData extends Diagnosticable { /// The color given to the [thumbShape] to draw itself with. final Color thumbColor; + /// The color given to the perimeter of the top [rangeThumbShape] when the + /// thumbs are overlapping and the top [rangeValueIndicatorShape] when the + /// value indicators are overlapping. + final Color overlappingShapeStrokeColor; + /// The color given to the [thumbShape] to draw itself with when the /// [Slider] is disabled. final Color disabledThumbColor; @@ -342,15 +484,13 @@ class SliderThemeData extends Diagnosticable { /// The color given to the [valueIndicatorShape] to draw itself with. final Color valueIndicatorColor; - /// The shape that will be used to draw the [Slider]'s track. + /// The shape that will be used to draw the [Slider]'s overlay. /// - /// The [SliderTrackShape.getPreferredRect] method is used to map - /// slider-relative gesture coordinates to the correct thumb position on the - /// track. It is also used to horizontally position tick marks, when he slider - /// is discrete. + /// Both the [overlayColor] and a non default [overlayShape] may be specified. + /// The default [overlayShape] refers to the [overlayColor]. /// - /// The default value is [RectangularSliderTrackShape]. - final SliderTrackShape trackShape; + /// The default value is [RoundSliderOverlayShape]. + final SliderComponentShape overlayShape; /// The shape that will be used to draw the [Slider]'s tick marks. /// @@ -359,24 +499,107 @@ class SliderThemeData extends Diagnosticable { /// be at least this big. /// /// The default value is [RoundSliderTickMarkShape]. + /// + /// See also: + /// + /// * [RoundRangeSliderTickMarkShape], which is the the default tick mark + /// shape for the range slider. final SliderTickMarkShape tickMarkShape; - /// The shape that will be used to draw the [Slider]'s overlay. - /// - /// Both the [overlayColor] and a non default [overlayShape] may be specified. - /// In this case, the [overlayColor] is only used if the [overlayShape] - /// explicitly does so. - /// - /// The default value is [RoundSliderOverlayShape]. - final SliderComponentShape overlayShape; - /// The shape that will be used to draw the [Slider]'s thumb. + /// + /// The default value is [RoundSliderThumbShape]. + /// + /// See also: + /// + /// * [RoundRangeSliderThumbShape], which is the the default thumb shape for + /// the [RangeSlider]. final SliderComponentShape thumbShape; + /// The shape that will be used to draw the [Slider]'s track. + /// + /// The [SliderTrackShape.getPreferredRect] method is used to map + /// slider-relative gesture coordinates to the correct thumb position on the + /// track. It is also used to horizontally position tick marks, when the + /// slider is discrete. + /// + /// The default value is [RoundedRectSliderTrackShape]. + /// + /// See also: + /// + /// * [RoundedRectRangeSliderTrackShape], which is the the default track + /// shape for the [RangeSlider]. + final SliderTrackShape trackShape; + /// The shape that will be used to draw the [Slider]'s value /// indicator. + /// + /// The default value is [PaddleSliderValueIndicatorShape]. + /// + /// See also: + /// + /// * [PaddleRangeSliderValueIndicatorShape], which is the the default value + /// indicator shape for the [RangeSlider]. final SliderComponentShape valueIndicatorShape; + /// The shape that will be used to draw the [RangeSlider]'s tick marks. + /// + /// The [RangeSliderTickMarkShape.getPreferredSize] is used to help determine + /// the location of each tick mark on the track. The slider's minimum size + /// will be at least this big. + /// + /// The default value is [RoundRangeSliderTickMarkShape]. + /// + /// See also: + /// + /// * [RoundSliderTickMarkShape], which is the the default tick mark shape + /// for the [Slider]. + final RangeSliderTickMarkShape rangeTickMarkShape; + + /// The shape that will be used for the [RangeSlider]'s thumbs. + /// + /// By default the same shape is used for both thumbs, but strokes the top + /// thumb when it overlaps the bottom thumb. The top thumb is always the last + /// selected thumb. + /// + /// The default value is [RoundRangeSliderThumbShape]. + /// + /// See also: + /// + /// * [RoundSliderThumbShape], which is the the default thumb shape for the + /// [Slider]. + final RangeSliderThumbShape rangeThumbShape; + + /// The shape that will be used to draw the [RangeSlider]'s track. + /// + /// The [SliderTrackShape.getPreferredRect] method is used to to map + /// slider-relative gesture coordinates to the correct thumb position on the + /// track. It is also used to horizontally position the tick marks, when the + /// slider is discrete. + /// + /// The default value is [RoundedRectRangeSliderTrackShape]. + /// + /// See also: + /// + /// * [RoundedRectSliderTrackShape], which is the the default track + /// shape for the [Slider]. + final RangeSliderTrackShape rangeTrackShape; + + /// The shape that will be used for the [RangeSlider]'s value indicators. + /// + /// The default shape uses the same value indicator for each thumb, but + /// strokes the top value indicator when it overlaps the bottom value + /// indicator. The top indicator corresponds to the top thumb, which is always + /// the most recently selected thumb. + /// + /// The default value is [PaddleRangeSliderValueIndicatorShape]. + /// + /// See also: + /// + /// * [PaddleSliderValueIndicatorShape], which is the the default value + /// indicator shape for the [Slider]. + final RangeSliderValueIndicatorShape rangeValueIndicatorShape; + /// Whether the value indicator should be shown for different types of /// sliders. /// @@ -390,6 +613,26 @@ class SliderThemeData extends Diagnosticable { /// By default this is the [ThemeData.accentTextTheme.body2] text theme. final TextStyle valueIndicatorTextStyle; + /// Limits the thumb's separation distance. + /// + /// Use this only if you want to control the visual appearance of the thumbs + /// in terms of a logical pixel value. This can be done when you want a + /// specific look for thumbs when they are close together. To limit with the + /// real values, rather than logical pixels, the [values] can be restricted by + /// the parent. + final double minThumbSeparation; + + /// Determines which thumb should be selected when the slider is interacted + /// with. + /// + /// If null, the default thumb selector finds the closest thumb, excluding + /// taps that are between the thumbs and not within any one touch target. + /// When the selection is within the touch target bounds of both thumbs, no + /// thumb is selected until the selection is moved. + /// + /// Override this for custom thumb selection. + final RangeThumbSelector thumbSelector; + /// Creates a copy of this object but with the given fields replaced with the /// new values. SliderThemeData copyWith({ @@ -403,16 +646,23 @@ class SliderThemeData extends Diagnosticable { Color disabledActiveTickMarkColor, Color disabledInactiveTickMarkColor, Color thumbColor, + Color overlappingShapeStrokeColor, Color disabledThumbColor, Color overlayColor, Color valueIndicatorColor, - SliderTrackShape trackShape, + SliderComponentShape overlayShape, SliderTickMarkShape tickMarkShape, SliderComponentShape thumbShape, - SliderComponentShape overlayShape, + SliderTrackShape trackShape, SliderComponentShape valueIndicatorShape, + RangeSliderTickMarkShape rangeTickMarkShape, + RangeSliderThumbShape rangeThumbShape, + RangeSliderTrackShape rangeTrackShape, + RangeSliderValueIndicatorShape rangeValueIndicatorShape, ShowValueIndicator showValueIndicator, TextStyle valueIndicatorTextStyle, + double minThumbSeparation, + RangeThumbSelector thumbSelector, }) { return SliderThemeData( trackHeight: trackHeight ?? this.trackHeight, @@ -425,16 +675,23 @@ class SliderThemeData extends Diagnosticable { disabledActiveTickMarkColor: disabledActiveTickMarkColor ?? this.disabledActiveTickMarkColor, disabledInactiveTickMarkColor: disabledInactiveTickMarkColor ?? this.disabledInactiveTickMarkColor, thumbColor: thumbColor ?? this.thumbColor, + overlappingShapeStrokeColor: overlappingShapeStrokeColor ?? this.overlappingShapeStrokeColor, disabledThumbColor: disabledThumbColor ?? this.disabledThumbColor, overlayColor: overlayColor ?? this.overlayColor, valueIndicatorColor: valueIndicatorColor ?? this.valueIndicatorColor, - trackShape: trackShape ?? this.trackShape, + overlayShape: overlayShape ?? this.overlayShape, tickMarkShape: tickMarkShape ?? this.tickMarkShape, thumbShape: thumbShape ?? this.thumbShape, - overlayShape: overlayShape ?? this.overlayShape, + trackShape: trackShape ?? this.trackShape, valueIndicatorShape: valueIndicatorShape ?? this.valueIndicatorShape, + rangeTickMarkShape: rangeTickMarkShape ?? this.rangeTickMarkShape, + rangeThumbShape: rangeThumbShape ?? this.rangeThumbShape, + rangeTrackShape: rangeTrackShape ?? this.rangeTrackShape, + rangeValueIndicatorShape: rangeValueIndicatorShape ?? this.rangeValueIndicatorShape, showValueIndicator: showValueIndicator ?? this.showValueIndicator, valueIndicatorTextStyle: valueIndicatorTextStyle ?? this.valueIndicatorTextStyle, + minThumbSeparation: minThumbSeparation ?? this.minThumbSeparation, + thumbSelector: thumbSelector ?? this.thumbSelector, ); } @@ -458,22 +715,29 @@ class SliderThemeData extends Diagnosticable { disabledActiveTickMarkColor: Color.lerp(a.disabledActiveTickMarkColor, b.disabledActiveTickMarkColor, t), disabledInactiveTickMarkColor: Color.lerp(a.disabledInactiveTickMarkColor, b.disabledInactiveTickMarkColor, t), thumbColor: Color.lerp(a.thumbColor, b.thumbColor, t), + overlappingShapeStrokeColor: Color.lerp(a.overlappingShapeStrokeColor, b.overlappingShapeStrokeColor, t), disabledThumbColor: Color.lerp(a.disabledThumbColor, b.disabledThumbColor, t), overlayColor: Color.lerp(a.overlayColor, b.overlayColor, t), valueIndicatorColor: Color.lerp(a.valueIndicatorColor, b.valueIndicatorColor, t), - trackShape: t < 0.5 ? a.trackShape : b.trackShape, + overlayShape: t < 0.5 ? a.overlayShape : b.overlayShape, tickMarkShape: t < 0.5 ? a.tickMarkShape : b.tickMarkShape, thumbShape: t < 0.5 ? a.thumbShape : b.thumbShape, - overlayShape: t < 0.5 ? a.overlayShape : b.overlayShape, + trackShape: t < 0.5 ? a.trackShape : b.trackShape, valueIndicatorShape: t < 0.5 ? a.valueIndicatorShape : b.valueIndicatorShape, + rangeTickMarkShape: t < 0.5 ? a.rangeTickMarkShape : b.rangeTickMarkShape, + rangeThumbShape: t < 0.5 ? a.rangeThumbShape : b.rangeThumbShape, + rangeTrackShape: t < 0.5 ? a.rangeTrackShape : b.rangeTrackShape, + rangeValueIndicatorShape: t < 0.5 ? a.rangeValueIndicatorShape : b.rangeValueIndicatorShape, showValueIndicator: t < 0.5 ? a.showValueIndicator : b.showValueIndicator, valueIndicatorTextStyle: TextStyle.lerp(a.valueIndicatorTextStyle, b.valueIndicatorTextStyle, t), + minThumbSeparation: lerpDouble(a.minThumbSeparation, b.minThumbSeparation, t), + thumbSelector: t < 0.5 ? a.thumbSelector : b.thumbSelector, ); } @override int get hashCode { - return hashValues( + return hashList([ trackHeight, activeTrackColor, inactiveTrackColor, @@ -484,17 +748,24 @@ class SliderThemeData extends Diagnosticable { disabledActiveTickMarkColor, disabledInactiveTickMarkColor, thumbColor, + overlappingShapeStrokeColor, disabledThumbColor, overlayColor, valueIndicatorColor, - trackShape, + overlayShape, tickMarkShape, thumbShape, - overlayShape, + trackShape, valueIndicatorShape, + rangeTickMarkShape, + rangeThumbShape, + rangeTrackShape, + rangeValueIndicatorShape, showValueIndicator, valueIndicatorTextStyle, - ); + minThumbSeparation, + thumbSelector, + ]); } @override @@ -516,70 +787,67 @@ class SliderThemeData extends Diagnosticable { && otherData.disabledActiveTickMarkColor == disabledActiveTickMarkColor && otherData.disabledInactiveTickMarkColor == disabledInactiveTickMarkColor && otherData.thumbColor == thumbColor + && otherData.overlappingShapeStrokeColor == overlappingShapeStrokeColor && otherData.disabledThumbColor == disabledThumbColor && otherData.overlayColor == overlayColor && otherData.valueIndicatorColor == valueIndicatorColor - && otherData.trackShape == trackShape + && otherData.overlayShape == overlayShape && otherData.tickMarkShape == tickMarkShape && otherData.thumbShape == thumbShape - && otherData.overlayShape == overlayShape + && otherData.trackShape == trackShape && otherData.valueIndicatorShape == valueIndicatorShape + && otherData.rangeTickMarkShape == rangeTickMarkShape + && otherData.rangeThumbShape == rangeThumbShape + && otherData.rangeTrackShape == rangeTrackShape + && otherData.rangeValueIndicatorShape == rangeValueIndicatorShape && otherData.showValueIndicator == showValueIndicator - && otherData.valueIndicatorTextStyle == valueIndicatorTextStyle; + && otherData.valueIndicatorTextStyle == valueIndicatorTextStyle + && otherData.minThumbSeparation == minThumbSeparation + && otherData.thumbSelector == thumbSelector; } @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); - final ThemeData defaultTheme = ThemeData.fallback(); - final SliderThemeData defaultData = SliderThemeData.fromPrimaryColors( - primaryColor: defaultTheme.primaryColor, - primaryColorDark: defaultTheme.primaryColorDark, - primaryColorLight: defaultTheme.primaryColorLight, - valueIndicatorTextStyle: defaultTheme.accentTextTheme.body2, - ); + const SliderThemeData defaultData = SliderThemeData(); + properties.add(DiagnosticsProperty('trackHeight', trackHeight, defaultValue: defaultData.trackHeight)); properties.add(DiagnosticsProperty('activeTrackColor', activeTrackColor, defaultValue: defaultData.activeTrackColor)); properties.add(DiagnosticsProperty('activeTrackColor', activeTrackColor, defaultValue: defaultData.activeTrackColor)); properties.add(DiagnosticsProperty('inactiveTrackColor', inactiveTrackColor, defaultValue: defaultData.inactiveTrackColor)); - properties.add(DiagnosticsProperty('disabledActiveTrackColor', disabledActiveTrackColor, defaultValue: defaultData.disabledActiveTrackColor, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('disabledInactiveTrackColor', disabledInactiveTrackColor, defaultValue: defaultData.disabledInactiveTrackColor, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('activeTickMarkColor', activeTickMarkColor, defaultValue: defaultData.activeTickMarkColor, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('inactiveTickMarkColor', inactiveTickMarkColor, defaultValue: defaultData.inactiveTickMarkColor, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('disabledActiveTickMarkColor', disabledActiveTickMarkColor, defaultValue: defaultData.disabledActiveTickMarkColor, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('disabledInactiveTickMarkColor', disabledInactiveTickMarkColor, defaultValue: defaultData.disabledInactiveTickMarkColor, level: DiagnosticLevel.debug)); + properties.add(DiagnosticsProperty('disabledActiveTrackColor', disabledActiveTrackColor, defaultValue: defaultData.disabledActiveTrackColor)); + properties.add(DiagnosticsProperty('disabledInactiveTrackColor', disabledInactiveTrackColor, defaultValue: defaultData.disabledInactiveTrackColor)); + properties.add(DiagnosticsProperty('activeTickMarkColor', activeTickMarkColor, defaultValue: defaultData.activeTickMarkColor)); + properties.add(DiagnosticsProperty('inactiveTickMarkColor', inactiveTickMarkColor, defaultValue: defaultData.inactiveTickMarkColor)); + properties.add(DiagnosticsProperty('disabledActiveTickMarkColor', disabledActiveTickMarkColor, defaultValue: defaultData.disabledActiveTickMarkColor)); + properties.add(DiagnosticsProperty('disabledInactiveTickMarkColor', disabledInactiveTickMarkColor, defaultValue: defaultData.disabledInactiveTickMarkColor)); properties.add(DiagnosticsProperty('thumbColor', thumbColor, defaultValue: defaultData.thumbColor)); - properties.add(DiagnosticsProperty('disabledThumbColor', disabledThumbColor, defaultValue: defaultData.disabledThumbColor, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('overlayColor', overlayColor, defaultValue: defaultData.overlayColor, level: DiagnosticLevel.debug)); + properties.add(DiagnosticsProperty('overlappingShapeStrokeColor', overlappingShapeStrokeColor, defaultValue: defaultData.overlappingShapeStrokeColor)); + properties.add(DiagnosticsProperty('disabledThumbColor', disabledThumbColor, defaultValue: defaultData.disabledThumbColor)); + properties.add(DiagnosticsProperty('overlayColor', overlayColor, defaultValue: defaultData.overlayColor)); properties.add(DiagnosticsProperty('valueIndicatorColor', valueIndicatorColor, defaultValue: defaultData.valueIndicatorColor)); - properties.add(DiagnosticsProperty('trackShape', trackShape, defaultValue: defaultData.trackShape, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('tickMarkShape', tickMarkShape, defaultValue: defaultData.tickMarkShape, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('thumbShape', thumbShape, defaultValue: defaultData.thumbShape, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('overlayShape', overlayShape, defaultValue: defaultData.overlayShape, level: DiagnosticLevel.debug)); - properties.add(DiagnosticsProperty('valueIndicatorShape', valueIndicatorShape, defaultValue: defaultData.valueIndicatorShape, level: DiagnosticLevel.debug)); + properties.add(DiagnosticsProperty('overlayShape', overlayShape, defaultValue: defaultData.overlayShape)); + properties.add(DiagnosticsProperty('tickMarkShape', tickMarkShape, defaultValue: defaultData.tickMarkShape)); + properties.add(DiagnosticsProperty('thumbShape', thumbShape, defaultValue: defaultData.thumbShape)); + properties.add(DiagnosticsProperty('trackShape', trackShape, defaultValue: defaultData.trackShape)); + properties.add(DiagnosticsProperty('valueIndicatorShape', valueIndicatorShape, defaultValue: defaultData.valueIndicatorShape)); + properties.add(DiagnosticsProperty('rangeTickMarkShape', rangeTickMarkShape, defaultValue: defaultData.rangeTickMarkShape)); + properties.add(DiagnosticsProperty('rangeThumbShape', rangeThumbShape, defaultValue: defaultData.rangeThumbShape)); + properties.add(DiagnosticsProperty('rangeTrackShape', rangeTrackShape, defaultValue: defaultData.rangeTrackShape)); + properties.add(DiagnosticsProperty('rangeValueIndicatorShape', rangeValueIndicatorShape, defaultValue: defaultData.rangeValueIndicatorShape)); properties.add(EnumProperty('showValueIndicator', showValueIndicator, defaultValue: defaultData.showValueIndicator)); properties.add(DiagnosticsProperty('valueIndicatorTextStyle', valueIndicatorTextStyle, defaultValue: defaultData.valueIndicatorTextStyle)); + properties.add(DiagnosticsProperty('minThumbSeparation', minThumbSeparation, defaultValue: defaultData.minThumbSeparation)); + properties.add(DiagnosticsProperty('thumbSelector', thumbSelector, defaultValue: defaultData.thumbSelector)); } } -// TEMPLATES FOR ALL SHAPES - -/// {@template flutter.material.slider.shape.context} -/// [context] is the same context for the render box of the [Slider]. -/// {@endtemplate} -/// /// {@template flutter.material.slider.shape.center} -/// [center] is the offset of the center where this shape should be painted. +/// [center] is the offset for where this shape's center should be painted. /// This offset is relative to the origin of the [context] canvas. /// {@endtemplate} /// -/// {@template flutter.material.slider.shape.sliderTheme} -/// [sliderTheme] is the theme assigned to the [Slider] that this shape -/// belongs to. -/// {@endtemplate} -/// -/// {@template flutter.material.slider.shape.isEnabled} -/// [isEnabled] has the same value as [Slider.isInteractive]. If true, the -/// slider will respond to input. +/// {@template flutter.material.slider.shape.context} +/// [context] is the same as the one that includes the [Slider]'s render box. /// {@endtemplate} /// /// {@template flutter.material.slider.shape.enableAnimation} @@ -594,113 +862,161 @@ class SliderThemeData extends Diagnosticable { /// slider will render tick marks on top of the track. /// {@endtemplate} /// +/// {@template flutter.material.slider.shape.isEnabled} +/// [isEnabled] has the same value as [Slider.isInteractive]. If true, the +/// slider will respond to input. +/// {@endtemplate} +/// /// {@template flutter.material.slider.shape.parentBox} /// [parentBox] is the [RenderBox] of the [Slider]. Its attributes, such as /// size, can be used to assist in painting this shape. /// {@endtemplate} +// +/// {@template flutter.material.slider.shape.sliderTheme} +/// [sliderTheme] is the theme assigned to the [Slider] that this shape +/// belongs to. +/// {@endtemplate} +/// +/// {@template flutter.material.rangeSlider.shape.activationAnimation} +/// [activationAnimation] is an animation triggered when the user begins +/// to interact with the [RangeSlider]. It reverses when the user stops +/// interacting with the slider. +/// {@endtemplate} +/// +/// {@template flutter.material.rangeSlider.shape.context} +/// [context] is the same as the one that includes the [RangeSlider]'s render +/// box. +/// {@endtemplate} +/// +/// {@template flutter.material.rangeSlider.shape.enableAnimation} +/// [enableAnimation] is an animation triggered when the [RangeSlider] is +/// enabled, and it reverses when the slider is disabled. Enabled is the +/// [RangeSlider.isEnabled] state. Use this to paint intermediate frames for +/// this shape when the slider changes enabled state. +/// {@endtemplate} +/// +/// {@template flutter.material.rangeSlider.shape.isDiscrete} +/// [isDiscrete] is true if [RangeSlider.divisions] is non-null. If true, the +/// slider will render tick marks on top of the track. +/// {@endtemplate} +/// +/// {@template flutter.material.rangeSlider.shape.isEnabled} +/// [isEnabled] has the same value as [RangeSlider.isEnabled]. If true, the +/// slider will respond to input. +/// {@endtemplate} +/// +/// {@template flutter.material.rangeSlider.shape.parentBox} +/// [parentBox] is the [RenderBox] of the [RangeSlider]. Its attributes, such as +/// size, can be used to assist in painting this shape. +/// {@endtemplate} +/// +/// {@template flutter.material.rangeSlider.shape.sliderTheme} +/// [sliderTheme] is the theme assigned to the [RangeSlider] that this shape +/// belongs to. +/// {@endtemplate} +/// +/// {@template flutter.material.rangeSlider.shape.thumb} +/// [thumb] Is the specifier for which of the two thumbs this method should +/// paint, start or end. +/// {@endtemplate} -/// Base class for slider track shapes. +/// Base class for slider thumb, thumb overlay, and value indicator shapes. /// -/// The slider's thumb moves along the track. A discrete slider's tick marks -/// are drawn after the track, but before the thumb, and are aligned with the -/// track. +/// Create a subclass of this if you would like a custom shape. /// -/// The [getPreferredRect] helps position the slider thumb and tick marks -/// relative to the track. +/// All shapes are painted to the same canvas and ordering is important. +/// The overlay is painted first, then the value indicator, then the thumb. +/// +/// The thumb painting can be skipped by specifying [noThumb] for +/// [SliderThemeData.thumbShape]. +/// +/// The overlay painting can be skipped by specifying [noOverlay] for +/// [SliderThemeData.overlayShape]. /// /// See also: /// -/// * [RectangularSliderTrackShape], which is the default track shape. -/// * [SliderTickMarkShape], which is the default tick mark shape. -/// * [SliderComponentShape], which is the base class for custom a component -/// shape. -abstract class SliderTrackShape { - /// Abstract const constructor. This constructor enables subclasses to provide +/// {@macro flutter.material.slider.seeAlso.roundSliderThumbShape} +/// {@macro flutter.material.slider.seeAlso.roundSliderOverlayShape} +/// {@macro flutter.material.slider.seeAlso.paddleSliderValueIndicatorShape} +abstract class SliderComponentShape { + /// This abstract const constructor enables subclasses to provide /// const constructors so that they can be used in const expressions. - const SliderTrackShape(); + const SliderComponentShape(); - /// Returns the preferred bounds of the shape. - /// - /// It is used to provide horizontal boundaries for the thumb's position, and - /// to help position the slider thumb and tick marks relative to the track. - /// - /// [parentBox] can be used to help determine the preferredRect relative to - /// attributes of the render box of the slider itself, such as size. - /// - /// [offset] is relative to the caller's bounding box. It can be used to - /// convert gesture coordinates from global to slider-relative coordinates. - /// - /// {@macro flutter.material.slider.shape.sliderTheme} - /// - /// {@macro flutter.material.slider.shape.isEnabled} - /// - /// {@macro flutter.material.slider.shape.isDiscrete} - Rect getPreferredRect({ - RenderBox parentBox, - Offset offset = Offset.zero, - SliderThemeData sliderTheme, - bool isEnabled, - bool isDiscrete, - }); + /// Returns the preferred size of the shape, based on the given conditions. + Size getPreferredSize(bool isEnabled, bool isDiscrete); - /// Paints the track shape based on the state passed to it. + /// Paints the shape, taking into account the state passed to it. /// /// {@macro flutter.material.slider.shape.context} /// - /// [offset] is the offset of the origin of the [parentBox] to the origin of - /// its [context] canvas. This shape must be painted relative to this - /// offset. See [PaintingContextCallback]. + /// {@macro flutter.material.slider.shape.center} + /// + /// [activationAnimation] is an animation triggered when the user begins + /// to interact with the slider. It reverses when the user stops interacting + /// with the slider. + /// + /// {@macro flutter.material.slider.shape.enableAnimation} + /// + /// {@macro flutter.material.slider.shape.isDiscrete} + /// + /// If [labelPainter] is non-null, then [labelPainter.paint] should be + /// called with the location that the label should appear. If the labelPainter + /// parameter is null, then no label was supplied to the [Slider]. /// /// {@macro flutter.material.slider.shape.parentBox} /// /// {@macro flutter.material.slider.shape.sliderTheme} /// - /// {@macro flutter.material.slider.shape.enableAnimation} + /// [textDirection] can be used to determine how any extra text or graphics, + /// besides the text painted by the [labelPainter] should be positioned. The + /// [labelPainter] already has the [textDirection] set. /// - /// [thumbCenter] is the offset of the center of the thumb relative to the - /// origin of the [PaintingContext.canvas]. It can be used as the point that - /// divides the track into 2 segments. - /// - /// {@macro flutter.material.slider.shape.isEnabled} - /// - /// {@macro flutter.material.slider.shape.isDiscrete} - /// - /// [textDirection] can be used to determine how the track segments are - /// painted depending on whether they are active or not. The track segment - /// between the start of the slider and the thumb is the active track segment. - /// The track segment between the thumb and the end of the slider is the - /// inactive track segment. In LTR text direction, the start of the slider is - /// on the left, and in RTL text direction, the start of the slider is on the - /// right. + /// [value] is the current parametric value (from 0.0 to 1.0) of the slider. void paint( PaintingContext context, - Offset offset, { + Offset center, { + Animation activationAnimation, + Animation enableAnimation, + bool isDiscrete, + TextPainter labelPainter, RenderBox parentBox, SliderThemeData sliderTheme, - Animation enableAnimation, - Offset thumbCenter, - bool isEnabled, - bool isDiscrete, TextDirection textDirection, + double value, }); + + /// Special instance of [SliderComponentShape] to skip the thumb drawing. + /// + /// See also: + /// + /// * [SliderThemeData.thumbShape], which is the shape that the [Slider] + /// uses when painting the thumb. + static final SliderComponentShape noThumb = _EmptySliderComponentShape(); + + /// Special instance of [SliderComponentShape] to skip the overlay drawing. + /// + /// See also: + /// + /// * [SliderThemeData.overlayShape], which is the shape that the [Slider] + /// uses when painting the overlay. + static final SliderComponentShape noOverlay = _EmptySliderComponentShape(); } -/// Base class for slider tick mark shapes. +/// Base class for [Slider] tick mark shapes. /// /// Create a subclass of this if you would like a custom slider tick mark shape. -/// This is a simplified version of [SliderComponentShape] with a -/// [SliderThemeData] passed when getting the preferred size. /// /// The tick mark painting can be skipped by specifying [noTickMark] for /// [SliderThemeData.tickMarkShape]. /// /// See also: /// -/// * [RoundSliderTickMarkShape] for a simple example of a tick mark shape. -/// * [SliderTrackShape] for the base class for custom a track shape. -/// * [SliderComponentShape] for the base class for custom a component shape. +/// {@macro flutter.material.slider.seeAlso.roundSliderTickMarkShape} +/// {@macro flutter.material.slider.seeAlso.sliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.sliderComponentShape} abstract class SliderTickMarkShape { - /// Abstract const constructor. This constructor enables subclasses to provide + /// This abstract const constructor enables subclasses to provide /// const constructors so that they can be used in const expressions. const SliderTickMarkShape(); @@ -752,154 +1068,358 @@ abstract class SliderTickMarkShape { /// /// See also: /// - /// * [SliderThemeData.tickMarkShape], which is the shape that the [Slider] - /// uses when painting tick marks. + /// * [SliderThemeData.tickMarkShape], which is the shape that the [Slider] + /// uses when painting tick marks. static final SliderTickMarkShape noTickMark = _EmptySliderTickMarkShape(); } -/// A special version of [SliderTickMarkShape] that has a zero size and paints -/// nothing. +/// Base class for slider track shapes. /// -/// This class is used to create a special instance of a [SliderTickMarkShape] -/// that will not paint any tick mark shape. A static reference is stored in -/// [SliderTickMarkShape.noTickMark]. When this value is specified for -/// [SliderThemeData.tickMarkShape], the tick mark painting is skipped. -class _EmptySliderTickMarkShape extends SliderTickMarkShape { - @override - Size getPreferredSize({ +/// The slider's thumb moves along the track. A discrete slider's tick marks +/// are drawn after the track, but before the thumb, and are aligned with the +/// track. +/// +/// The [getPreferredRect] helps position the slider thumb and tick marks +/// relative to the track. +/// +/// See also: +/// +/// {@macro flutter.material.slider.seeAlso.roundedRectSliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.sliderTickMarkShape} +/// {@macro flutter.material.slider.seeAlso.sliderComponentShape} +abstract class SliderTrackShape { + /// This abstract const constructor enables subclasses to provide + /// const constructors so that they can be used in const expressions. + const SliderTrackShape(); + + /// Returns the preferred bounds of the shape. + /// + /// It is used to provide horizontal boundaries for the thumb's position, and + /// to help position the slider thumb and tick marks relative to the track. + /// + /// The [parentBox] argument can be used to help determine the preferredRect relative to + /// attributes of the render box of the slider itself, such as size. + /// + /// The [offset] argument is relative to the caller's bounding box. It can be used to + /// convert gesture coordinates from global to slider-relative coordinates. + /// + /// {@macro flutter.material.slider.shape.sliderTheme} + /// + /// {@macro flutter.material.slider.shape.isEnabled} + /// + /// {@macro flutter.material.slider.shape.isDiscrete} + Rect getPreferredRect({ + RenderBox parentBox, + Offset offset = Offset.zero, SliderThemeData sliderTheme, bool isEnabled, - }) { - return Size.zero; - } + bool isDiscrete, + }); - @override + /// Paints the track shape based on the state passed to it. + /// + /// {@macro flutter.material.slider.shape.context} + /// + /// [offset] is the offset of the origin of the [parentBox] to the origin of + /// its [context] canvas. This shape must be painted relative to this + /// offset. See [PaintingContextCallback]. + /// + /// {@macro flutter.material.slider.shape.parentBox} + /// + /// {@macro flutter.material.slider.shape.sliderTheme} + /// + /// {@macro flutter.material.slider.shape.enableAnimation} + /// + /// The [thumbCenter] argument is the offset of the center of the thumb relative to the + /// origin of the [PaintingContext.canvas]. It can be used as the point that + /// divides the track into 2 segments. + /// + /// {@macro flutter.material.slider.shape.isEnabled} + /// + /// {@macro flutter.material.slider.shape.isDiscrete} + /// + /// The [textDirection] argument can be used to determine how the track segments are + /// painted depending on whether they are active or not. + /// {@macro flutter.material.slider.trackSegment} void paint( PaintingContext context, - Offset center, { + Offset offset, { RenderBox parentBox, SliderThemeData sliderTheme, Animation enableAnimation, Offset thumbCenter, bool isEnabled, + bool isDiscrete, TextDirection textDirection, - }) { - // no-op. - } + }); } -/// Base class for slider thumb, thumb overlay, and value indicator shapes. -/// -/// Create a subclass of this if you would like a custom shape. -/// -/// All shapes are painted to the same canvas and ordering is important. -/// The overlay is painted first, then the value indicator, then the thumb. -/// -/// The thumb painting can be skipped by specifying [noThumb] for -/// [SliderThemeData.thumbShape]. -/// -/// The overlay painting can be skipped by specifying [noOverlay] for -/// [SliderThemeData.overlayShape]. +/// Base class for [RangeSlider] thumb shapes. /// /// See also: /// -/// * [RoundSliderThumbShape], which is the default thumb shape. -/// * [RoundSliderOverlayShape], which is the default overlay shape. -/// * [PaddleSliderValueIndicatorShape], which is the default value -/// indicator shape. -abstract class SliderComponentShape { - /// Abstract const constructor. This constructor enables subclasses to provide +/// {@macro flutter.material.slider.seeAlso.roundRangeSliderThumbShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTickMarkShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderValueIndicatorShape} +/// {@macro flutter.material.slider.seeAlso.sliderComponentShape} +abstract class RangeSliderThumbShape { + /// This abstract const constructor enables subclasses to provide /// const constructors so that they can be used in const expressions. - const SliderComponentShape(); + const RangeSliderThumbShape(); /// Returns the preferred size of the shape, based on the given conditions. + /// + /// {@macro flutter.material.rangeSlider.shape.isDiscrete} + /// + /// {@macro flutter.material.rangeSlider.shape.isEnabled} Size getPreferredSize(bool isEnabled, bool isDiscrete); - /// Paints the shape, taking into account the state passed to it. + /// Paints the thumb shape based on the state passed to it. /// - /// {@macro flutter.material.slider.shape.context} + /// {@macro flutter.material.rangeSlider.shape.context} /// /// {@macro flutter.material.slider.shape.center} /// - /// [activationAnimation] is an animation triggered when the user beings - /// to interact with the slider. It reverses when the user stops interacting - /// with the slider. + /// {@macro flutter.material.rangeSlider.shape.activationAnimation} /// - /// {@macro flutter.material.slider.shape.enableAnimation} + /// {@macro flutter.material.rangeSlider.shape.enableAnimation} /// - /// {@macro flutter.material.slider.shape.isDiscrete} + /// {@macro flutter.material.rangeSlider.shape.isDiscrete} /// - /// If [labelPainter] is non-null, then [labelPainter.paint] should be - /// called with the location that the label should appear. If the labelPainter - /// passed is null, then no label was supplied to the [Slider]. + /// {@macro flutter.material.rangeSlider.shape.isEnabled} /// - /// {@macro flutter.material.slider.shape.parentBox} + /// If [isOnTop] is true this thumb is painted on top of the other slider + /// thumb because this thumb is the one that was most recently selected. /// - /// {@macro flutter.material.slider.shape.sliderTheme} + /// {@macro flutter.material.rangeSlider.shape.sliderTheme} + /// + /// {@macro flutter.material.rangeSlider.shape.thumb} + void paint( + PaintingContext context, + Offset center, { + Animation activationAnimation, + Animation enableAnimation, + bool isDiscrete, + bool isEnabled, + bool isOnTop, + SliderThemeData sliderTheme, + Thumb thumb, + }); +} + +/// Base class for [RangeSlider] value indicator shapes. +/// +/// See also: +/// +/// {@macro flutter.material.slider.seeAlso.paddleRangeSliderValueIndicatorShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTickMarkShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderThumbShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.sliderComponentShape} +abstract class RangeSliderValueIndicatorShape { + /// This abstract const constructor enables subclasses to provide + /// const constructors so that they can be used in const expressions. + const RangeSliderValueIndicatorShape(); + + /// Returns the preferred size of the shape, based on the given conditions. + /// + /// {@macro flutter.material.rangeSlider.shape.isEnabled} + /// + /// {@macro flutter.material.rangeSlider.shape.isDiscrete} + /// + /// [labelPainter] helps determine the width of the shape. It is variable + /// width because it is derived from a formatted string. + Size getPreferredSize(bool isEnabled, bool isDiscrete, { TextPainter labelPainter }); + + /// Paints the value indicator shape based on the state passed to it. + /// + /// {@macro flutter.material.rangeSlider.shape.context} + /// + /// {@macro flutter.material.slider.shape.center} + /// + /// {@macro flutter.material.rangeSlider.shape.activationAnimation} + /// + /// {@macro flutter.material.rangeSlider.shape.enableAnimation} + /// + /// {@macro flutter.material.rangeSlider.shape.isDiscrete} + /// + /// The [isOnTop] argument is the top-most value indicator between the two value + /// indicators, which is always the indicator for the most recently selected thumb. In + /// the default case, this is used to paint a stroke around the top indicator + /// for better visibility between the two indicators. + /// + /// {@macro flutter.material.rangeSlider.shape.sliderTheme} /// /// [textDirection] can be used to determine how any extra text or graphics, /// besides the text painted by the [labelPainter] should be positioned. The /// [labelPainter] already has the [textDirection] set. /// /// [value] is the current parametric value (from 0.0 to 1.0) of the slider. + /// + /// {@macro flutter.material.rangeSlider.shape.thumb} void paint( PaintingContext context, Offset center, { Animation activationAnimation, Animation enableAnimation, bool isDiscrete, + bool isOnTop, TextPainter labelPainter, RenderBox parentBox, SliderThemeData sliderTheme, TextDirection textDirection, double value, + Thumb thumb, }); - - /// Special instance of [SliderComponentShape] to skip the thumb drawing. - /// - /// See also: - /// - /// * [SliderThemeData.thumbShape], which is the shape that the [Slider] - /// uses when painting the thumb. - static final SliderComponentShape noThumb = _EmptySliderComponentShape(); - - /// Special instance of [SliderComponentShape] to skip the overlay drawing. - /// - /// See also: - /// - /// * [SliderThemeData.overlayShape], which is the shape that the [Slider] - /// uses when painting the overlay. - static final SliderComponentShape noOverlay = _EmptySliderComponentShape(); } -/// A special version of [SliderComponentShape] that has a zero size and paints -/// nothing. +/// Base class for [RangeSlider] tick mark shapes. /// -/// This class is used to create a special instance of a [SliderComponentShape] -/// that will not paint any component shape. A static reference is stored in -/// [SliderTickMarkShape.noThumb] and [SliderTickMarkShape.noOverlay]. When this value -/// is specified for [SliderThemeData.thumbShape], the thumb painting is -/// skipped. When this value is specified for [SliderThemeData.overlaySHape], -/// the overlay painting is skipped. -class _EmptySliderComponentShape extends SliderComponentShape { - @override - Size getPreferredSize(bool isEnabled, bool isDiscrete) => Size.zero; +/// This is a simplified version of [SliderComponentShape] with a +/// [SliderThemeData] passed when getting the preferred size. +/// +/// See also: +/// +/// {@macro flutter.material.slider.seeAlso.roundRangeSliderTickMarkShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderThumbShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderValueIndicatorShape} +/// {@macro flutter.material.slider.seeAlso.sliderComponentShape} +abstract class RangeSliderTickMarkShape { + /// This abstract const constructor enables subclasses to provide + /// const constructors so that they can be used in const expressions. + const RangeSliderTickMarkShape(); - @override + /// Returns the preferred size of the shape. + /// + /// It is used to help position the tick marks within the slider. + /// + /// {@macro flutter.material.rangeSlider.shape.sliderTheme} + /// + /// {@macro flutter.material.rangeSlider.shape.isEnabled} + Size getPreferredSize({ + SliderThemeData sliderTheme, + bool isEnabled, + }); + + /// Paints the slider track. + /// + /// {@macro flutter.material.rangeSlider.shape.context} + /// + /// {@macro flutter.material.slider.shape.center} + /// + /// {@macro flutter.material.rangeSlider.shape.parentBox} + /// + /// {@macro flutter.material.rangeSlider.shape.sliderTheme} + /// + /// {@macro flutter.material.rangeSlider.shape.enableAnimation} + /// + /// {@macro flutter.material.rangeSlider.shape.isEnabled} + /// + /// The [textDirection] argument can be used to determine how the tick marks are painted + /// depending on whether they are on an active track segment or not. + /// {@macro flutter.material.rangeSlider.trackSegment} void paint( PaintingContext context, Offset center, { - Animation activationAnimation, - Animation enableAnimation, - bool isDiscrete, - TextPainter labelPainter, RenderBox parentBox, SliderThemeData sliderTheme, + Animation enableAnimation, + Offset startThumbCenter, + Offset endThumbCenter, + bool isEnabled, TextDirection textDirection, - double value, - }) { - // no-op. - } + }); +} + +/// Base class for [RangeSlider] track shapes. +/// +/// The slider's thumbs move along the track. A discrete slider's tick marks +/// are drawn after the track, but before the thumb, and are aligned with the +/// track. +/// +/// The [getPreferredRect] helps position the slider thumbs and tick marks +/// relative to the track. +/// +/// See also: +/// +/// {@macro flutter.material.slider.seeAlso.roundedRectRangeSliderTrackShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderTickMarkShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderThumbShape} +/// {@macro flutter.material.slider.seeAlso.rangeSliderValueIndicatorShape} +/// {@macro flutter.material.slider.seeAlso.sliderComponentShape} +abstract class RangeSliderTrackShape { + /// This abstract const constructor enables subclasses to provide + /// const constructors so that they can be used in const expressions. + const RangeSliderTrackShape(); + + /// Returns the preferred bounds of the shape. + /// + /// It is used to provide horizontal boundaries for the position of the + /// thumbs, and to help position the slider thumbs and tick marks relative to + /// the track. + /// + /// The [parentBox] argument can be used to help determine the preferredRect relative to + /// attributes of the render box of the slider itself, such as size. + /// + /// The [offset] argument is relative to the caller's bounding box. It can be used to + /// convert gesture coordinates from global to slider-relative coordinates. + /// + /// {@macro flutter.material.rangeSlider.shape.sliderTheme} + /// + /// {@macro flutter.material.rangeSlider.shape.isEnabled} + /// + /// {@macro flutter.material.rangeSlider.shape.isDiscrete} + Rect getPreferredRect({ + RenderBox parentBox, + Offset offset = Offset.zero, + SliderThemeData sliderTheme, + bool isEnabled, + bool isDiscrete, + }); + + /// Paints the track shape based on the state passed to it. + /// + /// {@macro flutter.material.slider.shape.context} + /// + /// [offset] is the offset of the origin of the [parentBox] to the origin of + /// its [context] canvas. This shape must be painted relative to this + /// offset. See [PaintingContextCallback]. + /// + /// {@macro flutter.material.rangeSlider.shape.parentBox} + /// + /// {@macro flutter.material.rangeSlider.shape.sliderTheme} + /// + /// {@macro flutter.material.rangeSlider.shape.enableAnimation} + /// + /// [startThumbCenter] is the offset of the center of the start thumb relative + /// to the origin of the [PaintingContext.canvas]. It can be used as one point + /// that divides the track between inactive and active. + /// + /// [endThumbCenter] is the offset of the center of the end thumb relative + /// to the origin of the [PaintingContext.canvas]. It can be used as one point + /// that divides the track between inactive and active. + /// + /// {@macro flutter.material.rangeSlider.shape.isEnabled} + /// + /// {@macro flutter.material.rangeSlider.shape.isDiscrete} + /// + /// [textDirection] can be used to determine how the track segments are + /// painted depending on whether they are on an active track segment or not. + /// {@macro flutter.material.rangeSlider.trackSegment} + void paint( + PaintingContext context, + Offset offset, { + RenderBox parentBox, + SliderThemeData sliderTheme, + Animation enableAnimation, + Offset startThumbCenter, + Offset endThumbCenter, + bool isEnabled, + bool isDiscrete, + TextDirection textDirection, + }); } /// Base track shape that provides an implementation of [getPreferredRect] for @@ -907,10 +1427,10 @@ class _EmptySliderComponentShape extends SliderComponentShape { /// /// See also: /// -/// * [RectangularSliderTrackShape], which is a track shape with sharp -/// rectangular edges -/// * [RoundedRectSliderTrackShape], which is a track shape with round -/// stadium-like edges. +/// * [RectangularSliderTrackShape], which is a track shape with sharp +/// rectangular edges +/// * [RoundedRectSliderTrackShape], which is a track shape with round +/// stadium-like edges. /// /// The height is set from [SliderThemeData.trackHeight] and the width of the /// parent box less the larger of the widths of [SliderThemeData.thumbShape] and @@ -919,8 +1439,8 @@ abstract class BaseSliderTrackShape { /// Returns a rect that represents the track bounds that fits within the /// [Slider]. /// - /// The width is the width of the [Slider], but padded by the max - /// of the overlay and thumb radius. The height is defined by the + /// The width is the width of the [Slider] or [RangeSlider], but padded by + /// the max of the overlay and thumb radius. The height is defined by the /// [SliderThemeData.trackHeight]. /// /// The [Rect] is centered both horizontally and vertically within the slider @@ -928,11 +1448,14 @@ abstract class BaseSliderTrackShape { Rect getPreferredRect({ @required RenderBox parentBox, Offset offset = Offset.zero, - SliderThemeData sliderTheme, + @required SliderThemeData sliderTheme, bool isEnabled = false, bool isDiscrete = false, }) { + assert(isEnabled != null); + assert(isDiscrete != null); assert(parentBox != null); + assert(sliderTheme != null); final double thumbWidth = sliderTheme.thumbShape.getPreferredSize(isEnabled, isDiscrete).width; final double overlayWidth = sliderTheme.overlayShape.getPreferredSize(isEnabled, isDiscrete).width; final double trackHeight = sliderTheme.trackHeight; @@ -948,30 +1471,39 @@ abstract class BaseSliderTrackShape { } } -/// This is the default shape of a [Slider]'s track. +/// A [Slider] track that's a simple rectangle. /// -/// It paints a solid colored rectangle with rounded edges, vertically centered -/// in the [parentBox]. The track rectangle extends to the bounds of the -/// [parentBox], but is padded by the larger of [RoundSliderOverlayShape]'s -/// radius and [RoundSliderThumbShape]'s radius. The height is defined by the -/// [SliderThemeData.trackHeight]. The color is determined by the [Slider]'s -/// enabled state and the track segment's active state which are defined by: +/// It paints a solid colored rectangle, vertically centered in the +/// [parentBox]. The track rectangle extends to the bounds of the [parentBox], +/// but is padded by the [RoundSliderOverlayShape] radius. The height is defined +/// by the [SliderThemeData.trackHeight]. The color is determined by the +/// [Slider]'s enabled state and the track segments's active state which are +/// defined by: /// [SliderThemeData.activeTrackColor], /// [SliderThemeData.inactiveTrackColor], /// [SliderThemeData.disabledActiveTrackColor], /// [SliderThemeData.disabledInactiveTrackColor]. /// +/// {@macro flutter.material.slider.trackSegment} +/// /// See also: /// /// * [Slider], for the component that is meant to display this shape. /// * [SliderThemeData], where an instance of this class is set to inform the /// slider of the visual details of the its track. -/// * [SliderTrackShape], which is the base component for creating other -/// custom track shapes. -/// * [RectangularSliderTrackShape], for a similar track with sharp edges. -class RoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackShape { - /// Create a slider track that draws two rectangles with rounded outer edges. - const RoundedRectSliderTrackShape(); +/// {@macro flutter.material.slider.seeAlso.sliderTrackShape} +/// * [RoundedRectSliderTrackShape], for a similar track with rounded edges. +class RectangularSliderTrackShape extends SliderTrackShape with BaseSliderTrackShape { + /// Creates a slider track that draws 2 rectangles. + const RectangularSliderTrackShape({ this.disabledThumbGapWidth = 2.0 }); + + /// Horizontal spacing, or gap, between the disabled thumb and the track. + /// + /// This is only used when the slider is disabled. There is no gap around + /// the thumb and any part of the track when the slider is enabled. The + /// Material spec defaults this gap width 2, which is half of the disabled + /// thumb radius. + final double disabledThumbGapWidth; @override void paint( @@ -989,6 +1521,107 @@ class RoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackS assert(offset != null); assert(parentBox != null); assert(sliderTheme != null); + assert(sliderTheme.disabledActiveTrackColor != null); + assert(sliderTheme.disabledInactiveTrackColor != null); + assert(sliderTheme.activeTrackColor != null); + assert(sliderTheme.inactiveTrackColor != null); + assert(sliderTheme.thumbShape != null); + assert(enableAnimation != null); + assert(textDirection != null); + assert(thumbCenter != null); + assert(isEnabled != null); + assert(isDiscrete != null); + // If the slider track height is less than or equal to 0, then it makes no + // difference whether the track is painted or not, therefore the painting + // can be a no-op. + if (sliderTheme.trackHeight <= 0) { + return; + } + + // Assign the track segment paints, which are left: active, right: inactive, + // but reversed for right to left text. + final ColorTween activeTrackColorTween = ColorTween(begin: sliderTheme.disabledActiveTrackColor , end: sliderTheme.activeTrackColor); + final ColorTween inactiveTrackColorTween = ColorTween(begin: sliderTheme.disabledInactiveTrackColor , end: sliderTheme.inactiveTrackColor); + final Paint activePaint = Paint()..color = activeTrackColorTween.evaluate(enableAnimation); + final Paint inactivePaint = Paint()..color = inactiveTrackColorTween.evaluate(enableAnimation); + Paint leftTrackPaint; + Paint rightTrackPaint; + switch (textDirection) { + case TextDirection.ltr: + leftTrackPaint = activePaint; + rightTrackPaint = inactivePaint; + break; + case TextDirection.rtl: + leftTrackPaint = inactivePaint; + rightTrackPaint = activePaint; + break; + } + + final Rect trackRect = getPreferredRect( + parentBox: parentBox, + offset: offset, + sliderTheme: sliderTheme, + isEnabled: isEnabled, + isDiscrete: isDiscrete, + ); + + final Size thumbSize = sliderTheme.thumbShape.getPreferredSize(isEnabled, isDiscrete); + final Rect leftTrackSegment = Rect.fromLTRB(trackRect.left + trackRect.height / 2, trackRect.top, thumbCenter.dx - thumbSize.width / 2, trackRect.bottom); + if (!leftTrackSegment.isEmpty) + context.canvas.drawRect(leftTrackSegment, leftTrackPaint); + final Rect rightTrackSegment = Rect.fromLTRB(thumbCenter.dx + thumbSize.width / 2, trackRect.top, trackRect.right, trackRect.bottom); + if (!rightTrackSegment.isEmpty) + context.canvas.drawRect(rightTrackSegment, rightTrackPaint); + } +} + +/// The default shape of a [Slider]'s track. +/// +/// It paints a solid colored rectangle with rounded edges, vertically centered +/// in the [parentBox]. The track rectangle extends to the bounds of the +/// [parentBox], but is padded by the larger of [RoundSliderOverlayShape]'s +/// radius and [RoundSliderThumbShape]'s radius. The height is defined by the +/// [SliderThemeData.trackHeight]. The color is determined by the [Slider]'s +/// enabled state and the track segment's active state which are defined by: +/// [SliderThemeData.activeTrackColor], +/// [SliderThemeData.inactiveTrackColor], +/// [SliderThemeData.disabledActiveTrackColor], +/// [SliderThemeData.disabledInactiveTrackColor]. +/// +/// {@macro flutter.material.slider.trackSegment} +/// +/// See also: +/// +/// * [Slider], for the component that is meant to display this shape. +/// * [SliderThemeData], where an instance of this class is set to inform the +/// slider of the visual details of the its track. +/// {@macro flutter.material.slider.seeAlso.sliderTrackShape} +/// * [RectangularSliderTrackShape], for a similar track with sharp edges. +class RoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackShape { + /// Create a slider track that draws two rectangles with rounded outer edges. + const RoundedRectSliderTrackShape(); + + @override + void paint( + PaintingContext context, + Offset offset, { + @required RenderBox parentBox, + @required SliderThemeData sliderTheme, + @required Animation enableAnimation, + @required TextDirection textDirection, + @required Offset thumbCenter, + bool isDiscrete = false, + bool isEnabled = false, + }) { + assert(context != null); + assert(offset != null); + assert(parentBox != null); + assert(sliderTheme != null); + assert(sliderTheme.disabledActiveTrackColor != null); + assert(sliderTheme.disabledInactiveTrackColor != null); + assert(sliderTheme.activeTrackColor != null); + assert(sliderTheme.inactiveTrackColor != null); + assert(sliderTheme.thumbShape != null); assert(enableAnimation != null); assert(textDirection != null); assert(thumbCenter != null); @@ -1028,24 +1661,28 @@ class RoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackS // The arc rects create a semi-circle with radius equal to track height. final Rect leftTrackArcRect = Rect.fromLTWH(trackRect.left, trackRect.top, trackRect.height, trackRect.height); - context.canvas.drawArc(leftTrackArcRect, math.pi / 2, math.pi, false, leftTrackPaint); + if (!leftTrackArcRect.isEmpty) + context.canvas.drawArc(leftTrackArcRect, math.pi / 2, math.pi, false, leftTrackPaint); final Rect rightTrackArcRect = Rect.fromLTWH(trackRect.right - trackRect.height / 2, trackRect.top, trackRect.height, trackRect.height); - context.canvas.drawArc(rightTrackArcRect, -math.pi / 2, math.pi, false, rightTrackPaint); + if (!rightTrackArcRect.isEmpty) + context.canvas.drawArc(rightTrackArcRect, -math.pi / 2, math.pi, false, rightTrackPaint); final Size thumbSize = sliderTheme.thumbShape.getPreferredSize(isEnabled, isDiscrete); final Rect leftTrackSegment = Rect.fromLTRB(trackRect.left + trackRect.height / 2, trackRect.top, thumbCenter.dx - thumbSize.width / 2, trackRect.bottom); - context.canvas.drawRect(leftTrackSegment, leftTrackPaint); + if (!leftTrackSegment.isEmpty) + context.canvas.drawRect(leftTrackSegment, leftTrackPaint); final Rect rightTrackSegment = Rect.fromLTRB(thumbCenter.dx + thumbSize.width / 2, trackRect.top, trackRect.right, trackRect.bottom); - context.canvas.drawRect(rightTrackSegment, rightTrackPaint); + if (!rightTrackSegment.isEmpty) + context.canvas.drawRect(rightTrackSegment, rightTrackPaint); } } -/// A [Slider] track that's a simple rectangle. +/// A [RangeSlider] track that's a simple rectangle. /// /// It paints a solid colored rectangle, vertically centered in the /// [parentBox]. The track rectangle extends to the bounds of the [parentBox], -/// but is padded by the [RoundSliderOverlayShape] radius. The height is defined -/// by the [SliderThemeData.trackHeight]. The color is determined by the +/// but is padded by the [RoundSliderOverlayShape] radius. The height is +/// defined by the [SliderThemeData.trackHeight]. The color is determined by the /// [Slider]'s enabled state and the track segments's active state which are /// defined by: /// [SliderThemeData.activeTrackColor], @@ -1053,25 +1690,22 @@ class RoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackS /// [SliderThemeData.disabledActiveTrackColor], /// [SliderThemeData.disabledInactiveTrackColor]. /// +/// {@macro flutter.material.rangeSlider.trackSegment} +/// /// See also: /// -/// * [Slider], for the component that is meant to display this shape. +/// * [RangeSlider], for the component that is meant to display this shape. /// * [SliderThemeData], where an instance of this class is set to inform the /// slider of the visual details of the its track. -/// * [SliderTrackShape], which is the base component for creating other -/// custom track shapes. -/// * [RoundedRectSliderTrackShape], for a similar track with rounded edges. -class RectangularSliderTrackShape extends SliderTrackShape { - /// Create a slider track that draws 2 rectangles. - const RectangularSliderTrackShape({ this.disabledThumbGapWidth = 2.0 }); - - /// Horizontal spacing, or gap, between the disabled thumb and the track. +/// {@macro flutter.material.slider.seeAlso.rangeSliderTrackShape} +/// * [RoundedRectRangeSliderTrackShape], for a similar track with rounded +/// edges. +class RectangularRangeSliderTrackShape extends RangeSliderTrackShape { + /// Create a slider track with rectangular outer edges. /// - /// This is only used when the slider is disabled. There is no gap around - /// the thumb and any part of the track when the slider is enabled. The - /// Material spec defaults this gap width 2, which is half of the disabled - /// thumb radius. - final double disabledThumbGapWidth; + /// The middle track segment is the selected range and is active, and the two + /// outer track segments are inactive. + const RectangularRangeSliderTrackShape(); @override Rect getPreferredRect({ @@ -1082,8 +1716,11 @@ class RectangularSliderTrackShape extends SliderTrackShape { bool isDiscrete = false, }) { assert(parentBox != null); + assert(offset != null); assert(sliderTheme != null); - final double thumbWidth = sliderTheme.thumbShape.getPreferredSize(isEnabled, isDiscrete).width; + assert(sliderTheme.overlayShape != null); + assert(isEnabled != null); + assert(isDiscrete != null); final double overlayWidth = sliderTheme.overlayShape.getPreferredSize(isEnabled, isDiscrete).width; final double trackHeight = sliderTheme.trackHeight; assert(overlayWidth >= 0); @@ -1093,7 +1730,7 @@ class RectangularSliderTrackShape extends SliderTrackShape { final double trackLeft = offset.dx + overlayWidth / 2; final double trackTop = offset.dy + (parentBox.size.height - trackHeight) / 2; - final double trackWidth = parentBox.size.width - math.max(thumbWidth, overlayWidth); + final double trackWidth = parentBox.size.width - overlayWidth; return Rect.fromLTWH(trackLeft, trackTop, trackWidth, trackHeight); } @@ -1104,43 +1741,48 @@ class RectangularSliderTrackShape extends SliderTrackShape { @required RenderBox parentBox, @required SliderThemeData sliderTheme, @required Animation enableAnimation, - @required TextDirection textDirection, - @required Offset thumbCenter, - bool isDiscrete = false, + @required Offset startThumbCenter, + @required Offset endThumbCenter, bool isEnabled = false, + bool isDiscrete = false, + @required TextDirection textDirection, }) { assert(context != null); assert(offset != null); assert(parentBox != null); assert(sliderTheme != null); + assert(sliderTheme.disabledActiveTrackColor != null); + assert(sliderTheme.disabledInactiveTrackColor != null); + assert(sliderTheme.activeTrackColor != null); + assert(sliderTheme.inactiveTrackColor != null); + assert(sliderTheme.rangeThumbShape != null); assert(enableAnimation != null); + assert(startThumbCenter != null); + assert(endThumbCenter != null); + assert(isEnabled != null); + assert(isDiscrete != null); assert(textDirection != null); - assert(thumbCenter != null); - // If the slider track height is less than or equal to 0, then it makes no - // difference whether the track is painted or not, therefore the painting - // can be a no-op. - if (sliderTheme.trackHeight <= 0) { - return; - } - // Assign the track segment paints, which are left: active, right: inactive, // but reversed for right to left text. final ColorTween activeTrackColorTween = ColorTween(begin: sliderTheme.disabledActiveTrackColor , end: sliderTheme.activeTrackColor); final ColorTween inactiveTrackColorTween = ColorTween(begin: sliderTheme.disabledInactiveTrackColor , end: sliderTheme.inactiveTrackColor); final Paint activePaint = Paint()..color = activeTrackColorTween.evaluate(enableAnimation); final Paint inactivePaint = Paint()..color = inactiveTrackColorTween.evaluate(enableAnimation); - Paint leftTrackPaint; - Paint rightTrackPaint; + + Offset leftThumbOffset; + Offset rightThumbOffset; switch (textDirection) { case TextDirection.ltr: - leftTrackPaint = activePaint; - rightTrackPaint = inactivePaint; + leftThumbOffset = startThumbCenter; + rightThumbOffset = endThumbCenter; break; case TextDirection.rtl: - leftTrackPaint = inactivePaint; - rightTrackPaint = activePaint; + leftThumbOffset = endThumbCenter; + rightThumbOffset = startThumbCenter; break; } + final Size thumbSize = sliderTheme.rangeThumbShape.getPreferredSize(isEnabled, isDiscrete); + final double thumbRadius = thumbSize.width / 2; final Rect trackRect = getPreferredRect( parentBox: parentBox, @@ -1149,19 +1791,160 @@ class RectangularSliderTrackShape extends SliderTrackShape { isEnabled: isEnabled, isDiscrete: isDiscrete, ); - - final Size thumbSize = sliderTheme.thumbShape.getPreferredSize(isEnabled, isDiscrete); - final Rect leftTrackSegment = Rect.fromLTRB(trackRect.left + trackRect.height / 2, trackRect.top, thumbCenter.dx - thumbSize.width / 2, trackRect.bottom); - context.canvas.drawRect(leftTrackSegment, leftTrackPaint); - final Rect rightTrackSegment = Rect.fromLTRB(thumbCenter.dx + thumbSize.width / 2, trackRect.top, trackRect.right, trackRect.bottom); - context.canvas.drawRect(rightTrackSegment, rightTrackPaint); + final Rect leftTrackSegment = Rect.fromLTRB(trackRect.left, trackRect.top, leftThumbOffset.dx - thumbRadius, trackRect.bottom); + if (!leftTrackSegment.isEmpty) + context.canvas.drawRect(leftTrackSegment, inactivePaint); + final Rect middleTrackSegment = Rect.fromLTRB(leftThumbOffset.dx + thumbRadius, trackRect.top, rightThumbOffset.dx - thumbRadius, trackRect.bottom); + if (!middleTrackSegment.isEmpty) + context.canvas.drawRect(middleTrackSegment, activePaint); + final Rect rightTrackSegment = Rect.fromLTRB(rightThumbOffset.dx + thumbRadius, trackRect.top, trackRect.right, trackRect.bottom); + if (!rightTrackSegment.isEmpty) + context.canvas.drawRect(rightTrackSegment, inactivePaint); } } -/// This is the default shape of each [Slider] tick mark. +/// The default shape of a [RangeSlider]'s track. +/// +/// It paints a solid colored rectangle with rounded edges, vertically centered +/// in the [parentBox]. The track rectangle extends to the bounds of the +/// [parentBox], but is padded by the larger of [RoundSliderOverlayShape]'s +/// radius and [RoundRangeSliderThumbShape]'s radius. The height is defined by +/// the [SliderThemeData.trackHeight]. The color is determined by the +/// [RangeSlider]'s enabled state and the track segment's active state which are +/// defined by: +/// [SliderThemeData.activeTrackColor], +/// [SliderThemeData.inactiveTrackColor], +/// [SliderThemeData.disabledActiveTrackColor], +/// [SliderThemeData.disabledInactiveTrackColor]. +/// +/// {@macro flutter.material.rangeSlider.trackSegment} +/// +/// See also: +/// +/// * [RangeSlider], for the component that is meant to display this shape. +/// * [SliderThemeData], where an instance of this class is set to inform the +/// slider of the visual details of the its track. +/// {@macro flutter.material.slider.seeAlso.rangeSliderTrackShape} +/// * [RectangularRangeSliderTrackShape], for a similar track with sharp edges. +class RoundedRectRangeSliderTrackShape extends RangeSliderTrackShape { + /// Create a slider track with rounded outer edges. + /// + /// The middle track segment is the selected range and is active, and the two + /// outer track segments are inactive. + const RoundedRectRangeSliderTrackShape(); + + @override + Rect getPreferredRect({ + @required RenderBox parentBox, + Offset offset = Offset.zero, + @required SliderThemeData sliderTheme, + bool isEnabled = false, + bool isDiscrete = false, + }) { + assert(parentBox != null); + assert(offset != null); + assert(sliderTheme != null); + assert(sliderTheme.overlayShape != null); + assert(sliderTheme.trackHeight != null); + assert(isEnabled != null); + assert(isDiscrete != null); + final double overlayWidth = sliderTheme.overlayShape.getPreferredSize(isEnabled, isDiscrete).width; + final double trackHeight = sliderTheme.trackHeight; + assert(overlayWidth >= 0); + assert(trackHeight >= 0); + assert(parentBox.size.width >= overlayWidth); + assert(parentBox.size.height >= trackHeight); + + final double trackLeft = offset.dx + overlayWidth / 2; + final double trackTop = offset.dy + (parentBox.size.height - trackHeight) / 2; + final double trackWidth = parentBox.size.width - overlayWidth; + return Rect.fromLTWH(trackLeft, trackTop, trackWidth, trackHeight); + } + + @override + void paint( + PaintingContext context, + Offset offset, { + @required RenderBox parentBox, + @required SliderThemeData sliderTheme, + @required Animation enableAnimation, + @required Offset startThumbCenter, + @required Offset endThumbCenter, + bool isEnabled = false, + bool isDiscrete = false, + @required TextDirection textDirection, + }) { + assert(context != null); + assert(offset != null); + assert(parentBox != null); + assert(sliderTheme != null); + assert(sliderTheme.disabledActiveTrackColor != null); + assert(sliderTheme.disabledInactiveTrackColor != null); + assert(sliderTheme.activeTrackColor != null); + assert(sliderTheme.inactiveTrackColor != null); + assert(sliderTheme.rangeThumbShape != null); + assert(enableAnimation != null); + assert(startThumbCenter != null); + assert(endThumbCenter != null); + assert(isEnabled != null); + assert(isDiscrete != null); + assert(textDirection != null); + // Assign the track segment paints, which are left: active, right: inactive, + // but reversed for right to left text. + final ColorTween activeTrackColorTween = ColorTween(begin: sliderTheme.disabledActiveTrackColor , end: sliderTheme.activeTrackColor); + final ColorTween inactiveTrackColorTween = ColorTween(begin: sliderTheme.disabledInactiveTrackColor , end: sliderTheme.inactiveTrackColor); + final Paint activePaint = Paint()..color = activeTrackColorTween.evaluate(enableAnimation); + final Paint inactivePaint = Paint()..color = inactiveTrackColorTween.evaluate(enableAnimation); + + Offset leftThumbOffset; + Offset rightThumbOffset; + switch (textDirection) { + case TextDirection.ltr: + leftThumbOffset = startThumbCenter; + rightThumbOffset = endThumbCenter; + break; + case TextDirection.rtl: + leftThumbOffset = endThumbCenter; + rightThumbOffset = startThumbCenter; + break; + } + final Size thumbSize = sliderTheme.rangeThumbShape.getPreferredSize(isEnabled, isDiscrete); + final double thumbRadius = thumbSize.width / 2; + assert(thumbRadius > 0); + + final Rect trackRect = getPreferredRect( + parentBox: parentBox, + offset: offset, + sliderTheme: sliderTheme, + isEnabled: isEnabled, + isDiscrete: isDiscrete, + ); + final double trackRadius = trackRect.height / 2; + + final Rect leftTrackArcRect = Rect.fromLTWH(trackRect.left, trackRect.top, trackRect.height, trackRect.height); + if (!leftTrackArcRect.isEmpty) + context.canvas.drawArc(leftTrackArcRect, math.pi / 2, math.pi, false, inactivePaint); + + final Rect leftTrackSegment = Rect.fromLTRB(trackRect.left + trackRadius, trackRect.top, leftThumbOffset.dx - thumbRadius, trackRect.bottom); + if (!leftTrackSegment.isEmpty) + context.canvas.drawRect(leftTrackSegment, inactivePaint); + final Rect middleTrackSegment = Rect.fromLTRB(leftThumbOffset.dx + thumbRadius, trackRect.top, rightThumbOffset.dx - thumbRadius, trackRect.bottom); + if (!middleTrackSegment.isEmpty) + context.canvas.drawRect(middleTrackSegment, activePaint); + final Rect rightTrackSegment = Rect.fromLTRB(rightThumbOffset.dx + thumbRadius, trackRect.top, trackRect.right - trackRadius, trackRect.bottom); + if (!rightTrackSegment.isEmpty) + context.canvas.drawRect(rightTrackSegment, inactivePaint); + + final Rect rightTrackArcRect = Rect.fromLTWH(trackRect.right - trackRect.height, trackRect.top, trackRect.height, trackRect.height); + if (!rightTrackArcRect.isEmpty) + context.canvas.drawArc(rightTrackArcRect, -math.pi / 2, math.pi, false, inactivePaint); + } +} + +/// The default shape of each [Slider] tick mark. /// /// Tick marks are only displayed if the slider is discrete, which can be done -/// by setting the [Slider.divisions] as non-null. +/// by setting the [Slider.divisions] to an integer value. /// /// It paints a solid circle, centered in the on the track. /// The color is determined by the [Slider]'s enabled state and track's active @@ -1187,9 +1970,12 @@ class RoundSliderTickMarkShape extends SliderTickMarkShape { @override Size getPreferredSize({ - bool isEnabled, - SliderThemeData sliderTheme, + @required SliderThemeData sliderTheme, + bool isEnabled = false, }) { + assert(sliderTheme != null); + assert(sliderTheme.trackHeight != null); + assert(isEnabled != null); // The tick marks are tiny circles. If no radius is provided, then they are // defaulted to be the same height as the track. return Size.fromRadius(tickMarkRadius ?? sliderTheme.trackHeight / 2); @@ -1210,9 +1996,14 @@ class RoundSliderTickMarkShape extends SliderTickMarkShape { assert(center != null); assert(parentBox != null); assert(sliderTheme != null); + assert(sliderTheme.disabledActiveTickMarkColor != null); + assert(sliderTheme.disabledInactiveTickMarkColor != null); + assert(sliderTheme.activeTickMarkColor != null); + assert(sliderTheme.inactiveTickMarkColor != null); assert(enableAnimation != null); assert(textDirection != null); assert(thumbCenter != null); + assert(isEnabled != null); // The paint color of the tick mark depends on its position relative // to the thumb and the text direction. Color begin; @@ -1236,11 +2027,162 @@ class RoundSliderTickMarkShape extends SliderTickMarkShape { isEnabled: isEnabled, sliderTheme: sliderTheme, ).width / 2; - context.canvas.drawCircle(center, tickMarkRadius, paint); + if (tickMarkRadius > 0) { + context.canvas.drawCircle(center, tickMarkRadius, paint); + } } } -/// This is the default shape of a [Slider]'s thumb. +/// The default shape of each [RangeSlider] tick mark. +/// +/// Tick marks are only displayed if the slider is discrete, which can be done +/// by setting the [RangeSlider.divisions] to an integer value. +/// +/// It paints a solid circle, centered on the track. +/// The color is determined by the [Slider]'s enabled state and track's active +/// states. These colors are defined in: +/// [SliderThemeData.activeTrackColor], +/// [SliderThemeData.inactiveTrackColor], +/// [SliderThemeData.disabledActiveTrackColor], +/// [SliderThemeData.disabledInactiveTrackColor]. +/// +/// See also: +/// +/// * [RangeSlider], which includes tick marks defined by this shape. +/// * [SliderTheme], which can be used to configure the tick mark shape of all +/// sliders in a widget subtree. +class RoundRangeSliderTickMarkShape extends RangeSliderTickMarkShape { + /// Create a range slider tick mark that draws a circle. + const RoundRangeSliderTickMarkShape({ this.tickMarkRadius }); + + /// The preferred radius of the round tick mark. + /// + /// If it is not provided, then half of the track height is used. + final double tickMarkRadius; + + @override + Size getPreferredSize({ + @required SliderThemeData sliderTheme, + bool isEnabled = false, + }) { + assert(sliderTheme != null); + assert(sliderTheme.trackHeight != null); + assert(isEnabled != null); + return Size.fromRadius(tickMarkRadius ?? sliderTheme.trackHeight / 2); + } + + @override + void paint( + PaintingContext context, + Offset center, { + @required RenderBox parentBox, + @required SliderThemeData sliderTheme, + @required Animation enableAnimation, + @required Offset startThumbCenter, + @required Offset endThumbCenter, + bool isEnabled = false, + @required TextDirection textDirection, + }) { + assert(context != null); + assert(center != null); + assert(parentBox != null); + assert(sliderTheme != null); + assert(sliderTheme.disabledActiveTickMarkColor != null); + assert(sliderTheme.disabledInactiveTickMarkColor != null); + assert(sliderTheme.activeTickMarkColor != null); + assert(sliderTheme.inactiveTickMarkColor != null); + assert(enableAnimation != null); + assert(startThumbCenter != null); + assert(endThumbCenter != null); + assert(isEnabled != null); + assert(textDirection != null); + + bool isBetweenThumbs; + switch (textDirection) { + case TextDirection.ltr: + isBetweenThumbs = startThumbCenter.dx < center.dx && center.dx < endThumbCenter.dx; + break; + case TextDirection.rtl: + isBetweenThumbs = endThumbCenter.dx < center.dx && center.dx < startThumbCenter.dx; + break; + } + final Color begin = isBetweenThumbs ? sliderTheme.disabledActiveTickMarkColor : sliderTheme.disabledInactiveTickMarkColor; + final Color end = isBetweenThumbs ? sliderTheme.activeTickMarkColor : sliderTheme.inactiveTickMarkColor; + final Paint paint = Paint()..color = ColorTween(begin: begin, end: end).evaluate(enableAnimation); + + // The tick marks are tiny circles that are the same height as the track. + final double tickMarkRadius = getPreferredSize( + isEnabled: isEnabled, + sliderTheme: sliderTheme, + ).width / 2; + if (tickMarkRadius > 0) { + context.canvas.drawCircle(center, tickMarkRadius, paint); + } + } +} + +/// A special version of [SliderTickMarkShape] that has a zero size and paints +/// nothing. +/// +/// This class is used to create a special instance of a [SliderTickMarkShape] +/// that will not paint any tick mark shape. A static reference is stored in +/// [SliderTickMarkShape.noTickMark]. When this value is specified for +/// [SliderThemeData.tickMarkShape], the tick mark painting is skipped. +class _EmptySliderTickMarkShape extends SliderTickMarkShape { + @override + Size getPreferredSize({ + SliderThemeData sliderTheme, + bool isEnabled, + }) { + return Size.zero; + } + + @override + void paint( + PaintingContext context, + Offset center, { + RenderBox parentBox, + SliderThemeData sliderTheme, + Animation enableAnimation, + Offset thumbCenter, + bool isEnabled, + TextDirection textDirection, + }) { + // no-op. + } +} + +/// A special version of [SliderComponentShape] that has a zero size and paints +/// nothing. +/// +/// This class is used to create a special instance of a [SliderComponentShape] +/// that will not paint any component shape. A static reference is stored in +/// [SliderTickMarkShape.noThumb] and [SliderTickMarkShape.noOverlay]. When this value +/// is specified for [SliderThemeData.thumbShape], the thumb painting is +/// skipped. When this value is specified for [SliderThemeData.overlaySHape], +/// the overlay painting is skipped. +class _EmptySliderComponentShape extends SliderComponentShape { + @override + Size getPreferredSize(bool isEnabled, bool isDiscrete) => Size.zero; + + @override + void paint( + PaintingContext context, + Offset center, { + Animation activationAnimation, + Animation enableAnimation, + bool isDiscrete, + TextPainter labelPainter, + RenderBox parentBox, + SliderThemeData sliderTheme, + TextDirection textDirection, + double value, + }) { + // no-op. + } +} + +/// The default shape of a [Slider]'s thumb. /// /// See also: /// @@ -1268,31 +2210,29 @@ class RoundSliderThumbShape extends SliderComponentShape { @override Size getPreferredSize(bool isEnabled, bool isDiscrete) { - return Size.fromRadius(isEnabled ? enabledThumbRadius : _disabledThumbRadius); + return Size.fromRadius(isEnabled == true ? enabledThumbRadius : _disabledThumbRadius); } @override void paint( PaintingContext context, Offset center, { - @required Animation activationAnimation, + Animation activationAnimation, @required Animation enableAnimation, - bool isDiscrete = false, - @required TextPainter labelPainter, - @required RenderBox parentBox, + bool isDiscrete, + TextPainter labelPainter, + RenderBox parentBox, @required SliderThemeData sliderTheme, - @required TextDirection textDirection, - @required double value, + TextDirection textDirection, + double value, }) { assert(context != null); assert(center != null); - assert(activationAnimation != null); assert(enableAnimation != null); - assert(labelPainter != null); - assert(parentBox != null); assert(sliderTheme != null); - assert(textDirection != null); - assert(value != null); + assert(sliderTheme.disabledThumbColor != null); + assert(sliderTheme.thumbColor != null); + final Canvas canvas = context.canvas; final Tween radiusTween = Tween( begin: _disabledThumbRadius, @@ -1310,7 +2250,111 @@ class RoundSliderThumbShape extends SliderComponentShape { } } -/// This is the default shape of a [Slider]'s thumb overlay. +/// The default shape of a [RangeSlider]'s thumbs. +/// +/// See also: +/// +/// * [RangeSlider], which includes thumbs defined by this shape. +/// * [SliderTheme], which can be used to configure the thumb shapes of all +/// range sliders in a widget subtree. +class RoundRangeSliderThumbShape extends RangeSliderThumbShape { + /// Create a slider thumb that draws a circle. + const RoundRangeSliderThumbShape({ + this.enabledThumbRadius = 10.0, + this.disabledThumbRadius, + }) : assert(enabledThumbRadius != null); + + /// The preferred radius of the round thumb shape when the slider is enabled. + /// + /// If it is not provided, then the material default of 10 is used. + final double enabledThumbRadius; + + /// The preferred radius of the round thumb shape when the slider is disabled. + /// + /// If no disabledRadius is provided, then it is equal to the + /// [enabledThumbRadius]. + final double disabledThumbRadius; + double get _disabledThumbRadius => disabledThumbRadius ?? enabledThumbRadius; + + @override + Size getPreferredSize(bool isEnabled, bool isDiscrete) { + return Size.fromRadius(isEnabled == true ? enabledThumbRadius : _disabledThumbRadius); + } + + @override + void paint( + PaintingContext context, + Offset center, { + @required Animation activationAnimation, + @required Animation enableAnimation, + bool isDiscrete = false, + bool isEnabled = false, + bool isOnTop, + @required SliderThemeData sliderTheme, + Thumb thumb, + }) { + assert(context != null); + assert(center != null); + assert(activationAnimation != null); + assert(sliderTheme != null); + assert(sliderTheme.showValueIndicator != null); + assert(sliderTheme.overlappingShapeStrokeColor != null); + assert(enableAnimation != null); + final Canvas canvas = context.canvas; + final Tween radiusTween = Tween( + begin: _disabledThumbRadius, + end: enabledThumbRadius, + ); + final ColorTween colorTween = ColorTween( + begin: sliderTheme.disabledThumbColor, + end: sliderTheme.thumbColor, + ); + final double radius = radiusTween.evaluate(enableAnimation); + + // Add a stroke of 1dp around the circle if this thumb would overlap + // the other thumb. + if (isOnTop == true) { + bool showValueIndicator; + switch (sliderTheme.showValueIndicator) { + case ShowValueIndicator.onlyForDiscrete: + showValueIndicator = isDiscrete; + break; + case ShowValueIndicator.onlyForContinuous: + showValueIndicator = !isDiscrete; + break; + case ShowValueIndicator.always: + showValueIndicator = true; + break; + case ShowValueIndicator.never: + showValueIndicator = false; + break; + } + + final Paint strokePaint = Paint() + ..color = sliderTheme.overlappingShapeStrokeColor + ..strokeWidth = 1.0 + ..style = PaintingStyle.stroke; + if (showValueIndicator && activationAnimation.value > 0) { + // If there is a value indicator, then it is already stroked and a gap + // needs to be left on the thumb where the value indicator intersects + // the thumb. + const double startAngle = -math.pi / 2 + 0.2; + const double sweepAngle = math.pi * 2 - 0.4; + canvas.drawArc(Rect.fromCircle(center: center, radius: radius), startAngle, sweepAngle, false, strokePaint); + } else { + canvas.drawCircle(center, radius, strokePaint); + } + } + + canvas.drawCircle( + center, + radius, + Paint()..color = colorTween.evaluate(enableAnimation), + ); + } +} + +/// The default shape of a [Slider]'s thumb overlay. /// /// The shape of the overlay is a circle with the same center as the thumb, but /// with a larger radius. It animates to full size when the thumb is pressed, @@ -1361,6 +2405,7 @@ class RoundSliderOverlayShape extends SliderComponentShape { assert(sliderTheme != null); assert(textDirection != null); assert(value != null); + final Canvas canvas = context.canvas; final Tween radiusTween = Tween( begin: 0.0, @@ -1375,7 +2420,7 @@ class RoundSliderOverlayShape extends SliderComponentShape { } } -/// This is the default shape of a [Slider]'s value indicator. +/// The default shape of a [Slider]'s value indicator. /// /// See also: /// @@ -1386,6 +2431,111 @@ class PaddleSliderValueIndicatorShape extends SliderComponentShape { /// Create a slider value indicator in the shape of an upside-down pear. const PaddleSliderValueIndicatorShape(); + static const _PaddleSliderTrackShapePathPainter _pathPainter = _PaddleSliderTrackShapePathPainter(); + + @override + Size getPreferredSize(bool isEnabled, bool isDiscrete, { @required TextPainter labelPainter }) { + assert(labelPainter != null); + return _pathPainter.getPreferredSize(isEnabled, isDiscrete, labelPainter); + } + + @override + void paint( + PaintingContext context, + Offset center, { + @required Animation activationAnimation, + @required Animation enableAnimation, + bool isDiscrete, + @required TextPainter labelPainter, + @required RenderBox parentBox, + @required SliderThemeData sliderTheme, + TextDirection textDirection, + double value, + }) { + assert(context != null); + assert(center != null); + assert(activationAnimation != null); + assert(enableAnimation != null); + assert(labelPainter != null); + assert(parentBox != null); + assert(sliderTheme != null); + final ColorTween enableColor = ColorTween( + begin: sliderTheme.disabledThumbColor, + end: sliderTheme.valueIndicatorColor, + ); + _pathPainter.drawValueIndicator( + parentBox, + context.canvas, + center, + Paint()..color = enableColor.evaluate(enableAnimation), + activationAnimation.value, + labelPainter, + null, + ); + } +} + +/// The default shape of a [RangeSlider]'s value indicators. +/// +/// See also: +/// +/// * [RangeSlider], which includes value indicators defined by this shape. +/// * [SliderTheme], which can be used to configure the range slider value +/// indicator of all sliders in a widget subtree. +class PaddleRangeSliderValueIndicatorShape extends RangeSliderValueIndicatorShape { + /// Create a slider value indicator in the shape of an upside-down pear. + const PaddleRangeSliderValueIndicatorShape(); + + static const _PaddleSliderTrackShapePathPainter _pathPainter = _PaddleSliderTrackShapePathPainter(); + + @override + Size getPreferredSize(bool isEnabled, bool isDiscrete, { @required TextPainter labelPainter }) { + assert(labelPainter != null); + return _pathPainter.getPreferredSize(isEnabled, isDiscrete, labelPainter); + } + + @override + void paint( + PaintingContext context, + Offset center, { + @required Animation activationAnimation, + @required Animation enableAnimation, + bool isDiscrete, + bool isOnTop = false, + @required TextPainter labelPainter, + @required RenderBox parentBox, + @required SliderThemeData sliderTheme, + TextDirection textDirection, + Thumb thumb, + double value, + }) { + assert(context != null); + assert(center != null); + assert(activationAnimation != null); + assert(enableAnimation != null); + assert(labelPainter != null); + assert(parentBox != null); + assert(sliderTheme != null); + final ColorTween enableColor = ColorTween( + begin: sliderTheme.disabledThumbColor, + end: sliderTheme.valueIndicatorColor, + ); + // Add a stroke of 1dp around the top paddle. + _pathPainter.drawValueIndicator( + parentBox, + context.canvas, + center, + Paint()..color = enableColor.evaluate(enableAnimation), + activationAnimation.value, + labelPainter, + isOnTop ? sliderTheme.overlappingShapeStrokeColor : null, + ); + } +} + +class _PaddleSliderTrackShapePathPainter { + const _PaddleSliderTrackShapePathPainter(); + // These constants define the shape of the default value indicator. // The value indicator changes shape based on the size of // the label: The top lobe spreads horizontally, and the @@ -1418,7 +2568,7 @@ class PaddleSliderValueIndicatorShape extends SliderComponentShape { static const double _twoSeventyDegrees = 3.0 * math.pi / 2.0; static const double _ninetyDegrees = math.pi / 2.0; static const double _thirtyDegrees = math.pi / 6.0; - static const Size _preferredSize = Size.fromHeight(_distanceBetweenTopBottomCenters + _topLobeRadius + _bottomLobeRadius); + static const double _preferredHeight = _distanceBetweenTopBottomCenters + _topLobeRadius + _bottomLobeRadius; // Set to true if you want a rectangle to be drawn around the label bubble. // This helps with building tests that check that the label draws in the right // place (because it prints the rect in the failed test output). It should not @@ -1428,8 +2578,15 @@ class PaddleSliderValueIndicatorShape extends SliderComponentShape { static Path _bottomLobePath; // Initialized by _generateBottomLobe static Offset _bottomLobeEnd; // Initialized by _generateBottomLobe - @override - Size getPreferredSize(bool isEnabled, bool isDiscrete) => _preferredSize; + Size getPreferredSize( + bool isEnabled, + bool isDiscrete, + TextPainter labelPainter, + ) { + assert(labelPainter != null); + final double labelHalfWidth = labelPainter.width / 2.0; + return Size((labelHalfWidth + _topLobeRadius) * 2, _preferredHeight); + } // Adds an arc to the path that has the attributes passed in. This is // a convenience to make adding arcs have less boilerplate. @@ -1532,13 +2689,14 @@ class PaddleSliderValueIndicatorShape extends SliderComponentShape { return shift; } - void _drawValueIndicator( + void drawValueIndicator( RenderBox parentBox, Canvas canvas, Offset center, Paint paint, double scale, TextPainter labelPainter, + Color strokePaintColor, ) { canvas.save(); canvas.translate(center.dx, center.dy); @@ -1649,6 +2807,15 @@ class PaddleSliderValueIndicatorShape extends SliderComponentShape { rightNeckArcAngle, math.pi, ); + + if (strokePaintColor != null) { + final Paint strokePaint = Paint() + ..color = strokePaintColor + ..strokeWidth = 1.0 + ..style = PaintingStyle.stroke; + canvas.drawPath(path, strokePaint); + } + canvas.drawPath(path, paint); // Draw the label. @@ -1659,40 +2826,106 @@ class PaddleSliderValueIndicatorShape extends SliderComponentShape { canvas.restore(); canvas.restore(); } +} + +/// A callback that formats the numeric values from a [RangeSlider] widget. +/// +/// See also: +/// +/// * [RangeSlider.semanticFormatterCallback], which shows an example use case. +typedef RangeSemanticFormatterCallback = String Function(RangeValues values); + +/// Decides which thumbs (if any) should be selected. +/// +/// The default finds the closest thumb, but if the thumbs are close to each +/// other, it waits for movement defined by [dx] to determine the selected +/// thumb. +/// +/// Override [RangeSlider.thumbSelector] for custom thumb selection. +typedef RangeThumbSelector = Thumb Function( + TextDirection textDirection, + RangeValues values, + double tapValue, + Size thumbSize, + Size trackSize, + double dx + ); + +/// Object for representing range slider thumb values. +/// +/// This object is passed into [RangeSlider.values] to set its values, and it +/// is emitted in [RangeSlider.onChange], [RangeSlider.onChangeStart], and +/// [RangeSlider.onChangeEnd] when the values change. +class RangeValues { + /// Creates pair of start and end values. + const RangeValues(this.start, this.end); + + /// The value of the start thumb. + /// + /// For LTR text direction, the start is the left thumb, and for RTL text + /// direction, the start is the right thumb. + final double start; + + /// The value of the end thumb. + /// + /// For LTR text direction, the end is the right thumb, and for RTL text + /// direction, the end is the left thumb. + final double end; @override - void paint( - PaintingContext context, - Offset center, { - @required Animation activationAnimation, - @required Animation enableAnimation, - bool isDiscrete = false, - @required TextPainter labelPainter, - @required RenderBox parentBox, - @required SliderThemeData sliderTheme, - @required TextDirection textDirection, - @required double value, - }) { - assert(context != null); - assert(center != null); - assert(activationAnimation != null); - assert(enableAnimation != null); - assert(labelPainter != null); - assert(parentBox != null); - assert(sliderTheme != null); - assert(textDirection != null); - assert(value != null); - final ColorTween enableColor = ColorTween( - begin: sliderTheme.disabledThumbColor, - end: sliderTheme.valueIndicatorColor, - ); - _drawValueIndicator( - parentBox, - context.canvas, - center, - Paint()..color = enableColor.evaluate(enableAnimation), - activationAnimation.value, - labelPainter, - ); + bool operator ==(Object other) { + if (other.runtimeType != runtimeType) + return false; + final RangeValues typedOther = other; + return typedOther.start == start + && typedOther.end == end; + } + + @override + int get hashCode => hashValues(start, end); + + @override + String toString() { + return '$runtimeType($start, $end)'; } } + +/// Object for setting range slider label values that appear in the value +/// indicator for each thumb. +/// +/// Used in combination with [RangeSlider.showValueIndicator] to display +/// labels above the thumbs. +class RangeLabels { + /// Creates pair of start and end labels. + const RangeLabels(this.start, this.end); + + /// The label of the start thumb. + /// + /// For LTR text direction, the start is the left thumb, and for RTL text + /// direction, the start is the right thumb. + final String start; + + /// The label of the end thumb. + /// + /// For LTR text direction, the end is the right thumb, and for RTL text + /// direction, the end is the left thumb. + final String end; + + @override + bool operator ==(Object other) { + if (other.runtimeType != runtimeType) + return false; + final RangeLabels typedOther = other; + return typedOther.start == start + && typedOther.end == end; + } + + @override + int get hashCode => hashValues(start, end); + + @override + String toString() { + return '$runtimeType($start, $end)'; + } +} + diff --git a/packages/flutter/test/material/range_slider_test.dart b/packages/flutter/test/material/range_slider_test.dart new file mode 100644 index 0000000000..453a24d375 --- /dev/null +++ b/packages/flutter/test/material/range_slider_test.dart @@ -0,0 +1,1326 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:ui'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../rendering/mock_canvas.dart'; + +void main() { + testWidgets('Range Slider can move when tapped (continuous LTR)', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // No thumbs get select when tapping between the thumbs outside the touch + // boundaries + expect(values, equals(const RangeValues(0.3, 0.7))); + // taps at 0.5 + await tester.tap(find.byType(RangeSlider)); + await tester.pump(); + expect(values, equals(const RangeValues(0.3, 0.7))); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + + // The start thumb is selected when tapping the left inactive track. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.1; + await tester.tapAt(leftTarget); + expect(values.start, closeTo(0.1, 0.01)); + expect(values.end, equals(0.7)); + + // The end thumb is selected when tapping the right inactive track. + await tester.pump(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.9; + await tester.tapAt(rightTarget); + expect(values.start, closeTo(0.1, 0.01)); + expect(values.end, closeTo(0.9, 0.01)); + }); + + testWidgets('Range Slider can move when tapped (continuous RTL)', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.rtl, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // No thumbs get select when tapping between the thumbs outside the touch + // boundaries + expect(values, equals(const RangeValues(0.3, 0.7))); + // taps at 0.5 + await tester.tap(find.byType(RangeSlider)); + await tester.pump(); + expect(values, equals(const RangeValues(0.3, 0.7))); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + + // The end thumb is selected when tapping the left inactive track. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.1; + await tester.tapAt(leftTarget); + expect(values.start, 0.3); + expect(values.end, closeTo(0.9, 0.01)); + + // The start thumb is selected when tapping the right inactive track. + await tester.pump(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.9; + await tester.tapAt(rightTarget); + expect(values.start, closeTo(0.1, 0.01)); + expect(values.end, closeTo(0.9, 0.01)); + }); + + testWidgets('Range Slider can move when tapped (discrete LTR)', (WidgetTester tester) async { + RangeValues values = const RangeValues(30, 70); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + min: 0.0, + max: 100.0, + divisions: 10, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // No thumbs get select when tapping between the thumbs outside the touch + // boundaries + expect(values, equals(const RangeValues(30, 70))); + // taps at 0.5 + await tester.tap(find.byType(RangeSlider)); + await tester.pumpAndSettle(); + expect(values, equals(const RangeValues(30, 70))); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + + // The start thumb is selected when tapping the left inactive track. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.1; + await tester.tapAt(leftTarget); + await tester.pumpAndSettle(); + expect(values.start.round(), equals(10)); + expect(values.end.round(), equals(70)); + + // The end thumb is selected when tapping the right inactive track. + await tester.pump(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.9; + await tester.tapAt(rightTarget); + await tester.pumpAndSettle(); + expect(values.start.round(), equals(10)); + expect(values.end.round(), equals(90)); + }); + + testWidgets('Range Slider can move when tapped (discrete RTL)', (WidgetTester tester) async { + RangeValues values = const RangeValues(30, 70); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.rtl, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + min: 0, + max: 100, + divisions: 10, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // No thumbs get select when tapping between the thumbs outside the touch + // boundaries + expect(values, equals(const RangeValues(30, 70))); + // taps at 0.5 + await tester.tap(find.byType(RangeSlider)); + await tester.pumpAndSettle(); + expect(values, equals(const RangeValues(30, 70))); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + + // The start thumb is selected when tapping the left inactive track. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.1; + await tester.tapAt(leftTarget); + await tester.pumpAndSettle(); + expect(values.start.round(), equals(30)); + expect(values.end.round(), equals(90)); + + // The end thumb is selected when tapping the right inactive track. + await tester.pump(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.9; + await tester.tapAt(rightTarget); + await tester.pumpAndSettle(); + expect(values.start.round(), equals(10)); + expect(values.end.round(), equals(90)); + }); + + testWidgets('Range Slider thumbs can be dragged to the min and max (continous LTR)', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + + // Drag the start thumb to the min. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, topLeft + (bottomRight - topLeft) * -0.4); + expect(values.start, equals(0)); + + // Drag the end thumb to the max. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, topLeft + (bottomRight - topLeft) * 0.4); + expect(values.end, equals(1)); + }); + + testWidgets('Range Slider thumbs can be dragged to the min and max (continous RTL)', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.rtl, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + + // Drag the end thumb to the max. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, topLeft + (bottomRight - topLeft) * -0.4); + expect(values.end, equals(1)); + + // Drag the start thumb to the min. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, topLeft + (bottomRight - topLeft) * 0.4); + expect(values.start, equals(0)); + }); + + testWidgets('Range Slider thumbs can be dragged to the min and max (discrete LTR)', (WidgetTester tester) async { + RangeValues values = const RangeValues(30, 70); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + min: 0, + max: 100, + divisions: 10, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + + // Drag the start thumb to the min. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, topLeft + (bottomRight - topLeft) * -0.4); + expect(values.start, equals(0)); + + // Drag the end thumb to the max. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, topLeft + (bottomRight - topLeft) * 0.4); + expect(values.end, equals(100)); + }); + + testWidgets('Range Slider thumbs can be dragged to the min and max (discrete RTL)', (WidgetTester tester) async { + RangeValues values = const RangeValues(30, 70); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.rtl, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + min: 0, + max: 100, + divisions: 10, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + + // Drag the end thumb to the max. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, topLeft + (bottomRight - topLeft) * -0.4); + expect(values.end, equals(100)); + + // Drag the start thumb to the min. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, topLeft + (bottomRight - topLeft) * 0.4); + expect(values.start, equals(0)); + }); + + testWidgets('Range Slider thumbs can be dragged together and the start thumb can be dragged apart (continous LTR)', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the start thumb towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + expect(values.start, closeTo(0.5, 0.05)); + + // Drag the end thumb towards the center. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + expect(values.end, closeTo(0.5, 0.05)); + + // Drag the start thumb apart. + await tester.pumpAndSettle(); + await tester.dragFrom(middle, -(bottomRight - topLeft) * 0.3); + expect(values.start, closeTo(0.2, 0.05)); + }); + + testWidgets('Range Slider thumbs can be dragged together and the start thumb can be dragged apart (continous RTL)', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.rtl, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the end thumb towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + expect(values.end, closeTo(0.5, 0.05)); + + // Drag the start thumb towards the center. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + expect(values.start, closeTo(0.5, 0.05)); + + // Drag the start thumb apart. + await tester.pumpAndSettle(); + await tester.dragFrom(middle, (bottomRight - topLeft) * 0.3); + expect(values.start, closeTo(0.2, 0.05)); + }); + + testWidgets('Range Slider thumbs can be dragged together and the start thumb can be dragged apart (discrete LTR)', (WidgetTester tester) async { + RangeValues values = const RangeValues(30, 70); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + min: 0, + max: 100, + divisions: 10, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the start thumb towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + expect(values.start, closeTo(50, 0.01)); + + // Drag the end thumb towards the center. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + expect(values.end, closeTo(50, 0.01)); + + // Drag the start thumb apart. + await tester.pumpAndSettle(); + await tester.dragFrom(middle, -(bottomRight - topLeft) * 0.3); + expect(values.start, closeTo(20, 0.01)); + }); + + testWidgets('Range Slider thumbs can be dragged together and the start thumb can be dragged apart (discrete RTL)', (WidgetTester tester) async { + RangeValues values = const RangeValues(30, 70); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.rtl, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + min: 0, + max: 100, + divisions: 10, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the end thumb towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + expect(values.end, closeTo(50, 0.01)); + + // Drag the start thumb towards the center. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + expect(values.start, closeTo(50, 0.01)); + + // Drag the start thumb apart. + await tester.pumpAndSettle(); + await tester.dragFrom(middle, (bottomRight - topLeft) * 0.3); + expect(values.start, closeTo(20, 0.01)); + }); + + testWidgets('Range Slider thumbs can be dragged together and the end thumb can be dragged apart (continous LTR)', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the start thumb towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + expect(values.start, closeTo(0.5, 0.05)); + + // Drag the end thumb towards the center. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + expect(values.end, closeTo(0.5, 0.05)); + + // Drag the end thumb apart. + await tester.pumpAndSettle(); + await tester.dragFrom(middle, (bottomRight - topLeft) * 0.3); + expect(values.end, closeTo(0.8, 0.05)); + }); + + testWidgets('Range Slider thumbs can be dragged together and the end thumb can be dragged apart (continous RTL)', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.rtl, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the end thumb towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + expect(values.end, closeTo(0.5, 0.05)); + + // Drag the start thumb towards the center. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + expect(values.start, closeTo(0.5, 0.05)); + + // Drag the end thumb apart. + await tester.pumpAndSettle(); + await tester.dragFrom(middle, -(bottomRight - topLeft) * 0.3); + expect(values.end, closeTo(0.8, 0.05)); + }); + + testWidgets('Range Slider thumbs can be dragged together and the end thumb can be dragged apart (discrete LTR)', (WidgetTester tester) async { + RangeValues values = const RangeValues(30, 70); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + min: 0, + max: 100, + divisions: 10, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the start thumb towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + expect(values.start, closeTo(50, 0.01)); + + // Drag the end thumb towards the center. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + expect(values.end, closeTo(50, 0.01)); + + // Drag the end thumb apart. + await tester.pumpAndSettle(); + await tester.dragFrom(middle, (bottomRight - topLeft) * 0.3); + expect(values.end, closeTo(80, 0.01)); + }); + + testWidgets('Range Slider thumbs can be dragged together and the end thumb can be dragged apart (discrete RTL)', (WidgetTester tester) async { + RangeValues values = const RangeValues(30, 70); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.rtl, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: RangeSlider( + values: values, + min: 0, + max: 100, + divisions: 10, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ); + }, + ), + ), + ); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the end thumb towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + expect(values.end, closeTo(50, 0.01)); + + // Drag the start thumb towards the center. + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + expect(values.start, closeTo(50, 0.01)); + + // Drag the end thumb apart. + await tester.pumpAndSettle(); + await tester.dragFrom(middle, -(bottomRight - topLeft) * 0.3); + expect(values.end, closeTo(80, 0.01)); + }); + + ThemeData _buildTheme() { + return ThemeData( + platform: TargetPlatform.android, + primarySwatch: Colors.blue, + sliderTheme: const SliderThemeData( + disabledThumbColor: Color(0xff000001), + disabledActiveTickMarkColor: Color(0xff000002), + disabledActiveTrackColor: Color(0xff000003), + disabledInactiveTickMarkColor: Color(0xff000004), + disabledInactiveTrackColor: Color(0xff000005), + activeTrackColor: Color(0xff000006), + activeTickMarkColor: Color(0xff000007), + inactiveTrackColor: Color(0xff000008), + inactiveTickMarkColor: Color(0xff000009), + overlayColor: Color(0xff000010), + thumbColor: Color(0xff000011), + valueIndicatorColor: Color(0xff000012), + ) + ); + } + + Widget _buildThemedApp({ + ThemeData theme, + Color activeColor, + Color inactiveColor, + int divisions, + bool enabled = true, + }) { + RangeValues values = const RangeValues(0.5, 0.75); + final ValueChanged onChanged = !enabled ? null : (RangeValues newValues) { + values = newValues; + }; + return Directionality( + textDirection: TextDirection.ltr, + child: MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: Theme( + data: theme, + child: RangeSlider( + values: values, + labels: RangeLabels(values.start.toStringAsFixed(2), values.end.toStringAsFixed(2)), + divisions: divisions, + activeColor: activeColor, + inactiveColor: inactiveColor, + onChanged: onChanged, + ), + ), + ), + ), + ), + ); + } + + testWidgets('Range Slider uses the right theme colors for the right shapes for a default enabled slider', (WidgetTester tester) async { + final ThemeData theme = _buildTheme(); + final SliderThemeData sliderTheme = theme.sliderTheme; + + await tester.pumpWidget(_buildThemedApp(theme: theme)); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + // Check default theme for enabled widget. + expect(sliderBox, paints + ..rect(color: sliderTheme.inactiveTrackColor) + ..rect(color: sliderTheme.activeTrackColor) + ..rect(color: sliderTheme.inactiveTrackColor)); + expect(sliderBox, paints + ..circle(color: sliderTheme.thumbColor) + ..circle(color: sliderTheme.thumbColor)); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor))); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.activeTickMarkColor))); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.inactiveTickMarkColor))); + }); + + testWidgets('Range Slider uses the right theme colors for the right shapes when setting the active color', (WidgetTester tester) async { + const Color activeColor = Color(0xcafefeed); + final ThemeData theme = _buildTheme(); + final SliderThemeData sliderTheme = theme.sliderTheme; + + await tester.pumpWidget(_buildThemedApp(theme: theme, activeColor: activeColor)); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + expect( + sliderBox, + paints + ..rect(color: sliderTheme.inactiveTrackColor) + ..rect(color: activeColor) + ..rect(color: sliderTheme.inactiveTrackColor)); + expect( + sliderBox, + paints + ..circle(color: activeColor) + ..circle(color: activeColor)); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor))); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor))); + }); + + testWidgets('Range Slider uses the right theme colors for the right shapes when setting the inactive color', (WidgetTester tester) async { + const Color inactiveColor = Color(0xdeadbeef); + final ThemeData theme = _buildTheme(); + final SliderThemeData sliderTheme = theme.sliderTheme; + + await tester.pumpWidget(_buildThemedApp(theme: theme, inactiveColor: inactiveColor)); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + expect( + sliderBox, + paints + ..rect(color: inactiveColor) + ..rect(color: sliderTheme.activeTrackColor) + ..rect(color: inactiveColor)); + expect( + sliderBox, + paints + ..circle(color: sliderTheme.thumbColor) + ..circle(color: sliderTheme.thumbColor)); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor))); + }); + + testWidgets('Range Slider uses the right theme colors for the right shapes with active and inactive colors', (WidgetTester tester) async { + const Color activeColor = Color(0xcafefeed); + const Color inactiveColor = Color(0xdeadbeef); + final ThemeData theme = _buildTheme(); + final SliderThemeData sliderTheme = theme.sliderTheme; + + await tester.pumpWidget(_buildThemedApp( + theme: theme, + activeColor: activeColor, + inactiveColor: inactiveColor, + )); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + expect( + sliderBox, + paints + ..rect(color: inactiveColor) + ..rect(color: activeColor) + ..rect(color: inactiveColor)); + expect( + sliderBox, + paints + ..circle(color: activeColor) + ..circle(color: activeColor)); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor))); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor))); + }); + + testWidgets('Range Slider uses the right theme colors for the right shapes for a discrete slider', (WidgetTester tester) async { + final ThemeData theme = _buildTheme(); + final SliderThemeData sliderTheme = theme.sliderTheme; + + await tester.pumpWidget(_buildThemedApp(theme: theme, divisions: 3)); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + expect( + sliderBox, + paints + ..rect(color: sliderTheme.inactiveTrackColor) + ..rect(color: sliderTheme.activeTrackColor) + ..rect(color: sliderTheme.inactiveTrackColor)); + expect( + sliderBox, + paints + ..circle(color: sliderTheme.inactiveTickMarkColor) + ..circle(color: sliderTheme.inactiveTickMarkColor) + ..circle(color: sliderTheme.activeTickMarkColor) + ..circle(color: sliderTheme.inactiveTickMarkColor) + ..circle(color: sliderTheme.thumbColor) + ..circle(color: sliderTheme.thumbColor)); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor))); + }); + + testWidgets('Range Slider uses the right theme colors for the right shapes for a discrete slider with active and inactive colors', (WidgetTester tester) async { + const Color activeColor = Color(0xcafefeed); + const Color inactiveColor = Color(0xdeadbeef); + final ThemeData theme = _buildTheme(); + final SliderThemeData sliderTheme = theme.sliderTheme; + + + await tester.pumpWidget(_buildThemedApp( + theme: theme, + activeColor: activeColor, + inactiveColor: inactiveColor, + divisions: 3, + )); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + expect( + sliderBox, + paints + ..rect(color: inactiveColor) + ..rect(color: activeColor) + ..rect(color: inactiveColor)); + expect( + sliderBox, + paints + ..circle(color: activeColor) + ..circle(color: activeColor) + ..circle(color: inactiveColor) + ..circle(color: activeColor) + ..circle(color: activeColor) + ..circle(color: activeColor)); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor))); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.disabledThumbColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledActiveTrackColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.disabledInactiveTrackColor))); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.activeTickMarkColor))); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.inactiveTickMarkColor))); + }); + + testWidgets('Range Slider uses the right theme colors for the right shapes for a default disabled slider', (WidgetTester tester) async { + final ThemeData theme = _buildTheme(); + final SliderThemeData sliderTheme = theme.sliderTheme; + + await tester.pumpWidget(_buildThemedApp(theme: theme, enabled: false)); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + expect( + sliderBox, + paints + ..rect(color: sliderTheme.disabledInactiveTrackColor) + ..rect(color: sliderTheme.disabledActiveTrackColor) + ..rect(color: sliderTheme.disabledInactiveTrackColor)); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.activeTrackColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.inactiveTrackColor))); + + }); + + testWidgets('Range Slider uses the right theme colors for the right shapes for a disabled slider with active and inactive colors', (WidgetTester tester) async { + const Color activeColor = Color(0xcafefeed); + const Color inactiveColor = Color(0xdeadbeef); + final ThemeData theme = _buildTheme(); + final SliderThemeData sliderTheme = theme.sliderTheme; + + await tester.pumpWidget(_buildThemedApp( + theme: theme, + activeColor: activeColor, + inactiveColor: inactiveColor, + enabled: false, + )); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + expect( + sliderBox, + paints + ..rect(color: sliderTheme.disabledInactiveTrackColor) + ..rect(color: sliderTheme.disabledActiveTrackColor) + ..rect(color: sliderTheme.disabledInactiveTrackColor)); + expect(sliderBox, isNot(paints..circle(color: sliderTheme.thumbColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.activeTrackColor))); + expect(sliderBox, isNot(paints..rect(color: sliderTheme.inactiveTrackColor))); + }); + + testWidgets('Range Slider uses the right theme colors for the right shapes when the value indicators are showing', (WidgetTester tester) async { + const Color customColor1 = Color(0xcafefeed); + const Color customColor2 = Color(0xdeadbeef); + final ThemeData theme = _buildTheme(); + final SliderThemeData sliderTheme = theme.sliderTheme; + RangeValues values = const RangeValues(0.5, 0.75); + + Widget buildApp({ + Color activeColor, + Color inactiveColor, + int divisions, + bool enabled = true, + }) { + final ValueChanged onChanged = !enabled ? null : (RangeValues newValues) { + values = newValues; + }; + return Directionality( + textDirection: TextDirection.ltr, + child: MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: Theme( + data: theme, + child: RangeSlider( + values: values, + labels: RangeLabels(values.start.toStringAsFixed(2), values.end.toStringAsFixed(2)), + divisions: divisions, + activeColor: activeColor, + inactiveColor: inactiveColor, + onChanged: onChanged, + ), + ), + ), + ), + ), + ); + } + + await tester.pumpWidget(buildApp(divisions: 3)); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + final Offset topRight = tester.getTopRight(find.byType(RangeSlider)).translate(-24, 0); + TestGesture gesture = await tester.startGesture(topRight); + // Wait for value indicator animation to finish. + await tester.pumpAndSettle(); + expect(values.end, equals(1)); + expect( + sliderBox, + paints + ..path(color: sliderTheme.valueIndicatorColor) + ..path(color: sliderTheme.valueIndicatorColor) + ); + await gesture.up(); + // Wait for value indicator animation to finish. + await tester.pumpAndSettle(); + + // Testing the custom colors are used for the indicator. + await tester.pumpWidget(buildApp( + divisions: 3, + activeColor: customColor1, + inactiveColor: customColor2, + )); + gesture = await tester.startGesture(topRight); + // Wait for value indicator animation to finish. + await tester.pumpAndSettle(); + expect(values.end, equals(1)); + expect( + sliderBox, + paints + ..path(color: customColor1) + ..path(color: customColor1) + ); + await gesture.up(); + }); + + testWidgets('Range Slider top thumb gets stroked when overlapping', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + final ThemeData theme = ThemeData( + platform: TargetPlatform.android, + primarySwatch: Colors.blue, + sliderTheme: const SliderThemeData( + thumbColor: Color(0xff000001), + overlappingShapeStrokeColor: Color(0xff000002) + ) + ); + final SliderThemeData sliderTheme = theme.sliderTheme; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: Theme( + data: theme, + child: RangeSlider( + values: values, + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ), + ); + }, + ), + ), + ); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the the thumbs towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + expect(values.start, closeTo(0.5, 0.03)); + expect(values.end, closeTo(0.5, 0.03)); + await tester.pumpAndSettle(); + + expect( + sliderBox, + paints + ..circle(color: sliderTheme.thumbColor) + ..circle(color: sliderTheme.overlappingShapeStrokeColor) + ..circle(color: sliderTheme.thumbColor) + ); + }); + + testWidgets('Range Slider top value indicator gets stroked when overlapping', (WidgetTester tester) async { + RangeValues values = const RangeValues(0.3, 0.7); + + final ThemeData theme = ThemeData( + platform: TargetPlatform.android, + primarySwatch: Colors.blue, + sliderTheme: const SliderThemeData( + valueIndicatorColor: Color(0xff000001), + overlappingShapeStrokeColor: Color(0xff000002), + showValueIndicator: ShowValueIndicator.always, + ) + ); + final SliderThemeData sliderTheme = theme.sliderTheme; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MediaQuery( + data: MediaQueryData.fromWindow(window), + child: Material( + child: Center( + child: Theme( + data: theme, + child: RangeSlider( + values: values, + labels: RangeLabels(values.start.toStringAsFixed(2), values.end.toStringAsFixed(2)), + onChanged: (RangeValues newValues) { + setState(() { + values = newValues; + }); + }, + ), + ), + ), + ), + ); + }, + ), + ), + ); + + final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); + + // Get the bounds of the track by shifting by the the overlay radius. + final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0); + final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0); + final Offset middle = topLeft + bottomRight / 2; + + // Drag the the thumbs towards the center. + final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.3; + await tester.dragFrom(leftTarget, middle - leftTarget); + await tester.pumpAndSettle(); + final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.7; + await tester.dragFrom(rightTarget, middle - rightTarget); + await tester.pumpAndSettle(); + expect(values.start, closeTo(0.5, 0.03)); + expect(values.end, closeTo(0.5, 0.03)); + final TestGesture gesture = await tester.startGesture(middle); + await tester.pumpAndSettle(); + + expect( + sliderBox, + paints + ..path(color: sliderTheme.valueIndicatorColor) + ..path(color: sliderTheme.overlappingShapeStrokeColor) + ..path(color: sliderTheme.valueIndicatorColor) + ); + + await gesture.up(); + }); +} + diff --git a/packages/flutter/test/material/slider_theme_test.dart b/packages/flutter/test/material/slider_theme_test.dart index 7f5784f7b1..2321a6d529 100644 --- a/packages/flutter/test/material/slider_theme_test.dart +++ b/packages/flutter/test/material/slider_theme_test.dart @@ -12,6 +12,89 @@ import 'package:flutter/painting.dart'; import '../rendering/mock_canvas.dart'; void main() { + test('SliderThemeData copyWith, ==, hashCode basics', () { + expect(const SliderThemeData(), const SliderThemeData().copyWith()); + expect(const SliderThemeData().hashCode, const SliderThemeData().copyWith().hashCode); + }); + + testWidgets('Default SliderThemeData debugFillProperties', (WidgetTester tester) async { + final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder(); + const SliderThemeData().debugFillProperties(builder); + + final List description = builder.properties + .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info)) + .map((DiagnosticsNode node) => node.toString()) + .toList(); + + expect(description, []); + }); + + + testWidgets('SliderThemeData implements debugFillProperties', (WidgetTester tester) async { + final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder(); + const SliderThemeData( + trackHeight: 7.0, + activeTrackColor: Color(0xFF000001), + inactiveTrackColor: Color(0xFF000002), + disabledActiveTrackColor: Color(0xFF000003), + disabledInactiveTrackColor: Color(0xFF000004), + activeTickMarkColor: Color(0xFF000005), + inactiveTickMarkColor: Color(0xFF000006), + disabledActiveTickMarkColor: Color(0xFF000007), + disabledInactiveTickMarkColor: Color(0xFF000008), + thumbColor: Color(0xFF000009), + overlappingShapeStrokeColor: Color(0xFF000010), + disabledThumbColor: Color(0xFF000011), + overlayColor: Color(0xFF000012), + valueIndicatorColor: Color(0xFF000013), + overlayShape: RoundSliderOverlayShape(), + tickMarkShape: RoundSliderTickMarkShape(), + thumbShape: RoundSliderThumbShape(), + trackShape: RoundedRectSliderTrackShape(), + valueIndicatorShape: PaddleSliderValueIndicatorShape(), + rangeTickMarkShape: RoundRangeSliderTickMarkShape(), + rangeThumbShape: RoundRangeSliderThumbShape(), + rangeTrackShape: RoundedRectRangeSliderTrackShape(), + rangeValueIndicatorShape: PaddleRangeSliderValueIndicatorShape(), + showValueIndicator: ShowValueIndicator.always, + valueIndicatorTextStyle: TextStyle(color: Colors.black), + ).debugFillProperties(builder); + + final List description = builder.properties + .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info)) + .map((DiagnosticsNode node) => node.toString()) + .toList(); + + expect(description, [ + 'trackHeight: 7.0', + 'activeTrackColor: Color(0xff000001)', + 'activeTrackColor: Color(0xff000001)', + 'inactiveTrackColor: Color(0xff000002)', + 'disabledActiveTrackColor: Color(0xff000003)', + 'disabledInactiveTrackColor: Color(0xff000004)', + 'activeTickMarkColor: Color(0xff000005)', + 'inactiveTickMarkColor: Color(0xff000006)', + 'disabledActiveTickMarkColor: Color(0xff000007)', + 'disabledInactiveTickMarkColor: Color(0xff000008)', + 'thumbColor: Color(0xff000009)', + 'overlappingShapeStrokeColor: Color(0xff000010)', + 'disabledThumbColor: Color(0xff000011)', + 'overlayColor: Color(0xff000012)', + 'valueIndicatorColor: Color(0xff000013)', + 'overlayShape: Instance of \'RoundSliderOverlayShape\'', + 'tickMarkShape: Instance of \'RoundSliderTickMarkShape\'', + 'thumbShape: Instance of \'RoundSliderThumbShape\'', + 'trackShape: Instance of \'RoundedRectSliderTrackShape\'', + 'valueIndicatorShape: Instance of \'PaddleSliderValueIndicatorShape\'', + 'rangeTickMarkShape: Instance of \'RoundRangeSliderTickMarkShape\'', + 'rangeThumbShape: Instance of \'RoundRangeSliderThumbShape\'', + 'rangeTrackShape: Instance of \'RoundedRectRangeSliderTrackShape\'', + 'rangeValueIndicatorShape: Instance of \'PaddleRangeSliderValueIndicatorShape\'', + 'showValueIndicator: always', + 'valueIndicatorTextStyle: TextStyle(inherit: true, color: Color(0xff000000))' + ]); + }); + testWidgets('Slider uses ThemeData slider theme if present', (WidgetTester tester) async { final ThemeData theme = ThemeData( platform: TargetPlatform.android, @@ -615,6 +698,9 @@ void main() { overlayShape: SliderComponentShape.noOverlay, thumbShape: SliderComponentShape.noThumb, showValueIndicator: ShowValueIndicator.never, + // When the track is hidden to 0 height, a tick mark radius + // must be provided to get a non-zero radius. + tickMarkShape: const RoundSliderTickMarkShape(tickMarkRadius: 1), ), value: 0.5, divisions: 4, @@ -707,6 +793,8 @@ void main() { }); } + + Widget _buildApp( SliderThemeData sliderTheme, { double value = 0.0, @@ -730,3 +818,5 @@ Widget _buildApp( ), ); } + +