Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add focus indicator to ShadSlider #205

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 65 additions & 43 deletions lib/src/components/slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class _ShadSliderState extends State<ShadSlider> {
initialValue: widget.initialValue!,
);

bool isFocused = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove, not needed


@override
void dispose() {
// dispose the internal controller
Expand Down Expand Up @@ -142,51 +144,60 @@ class _ShadSliderState extends State<ShadSlider> {
final effectiveThumbRadius =
widget.thumbRadius ?? theme.sliderTheme.thumbRadius ?? 10.0;

return Theme(
data: mTheme.copyWith(
sliderTheme: mTheme.sliderTheme.copyWith(
trackHeight: effectiveTrackHeight,
thumbShape: ShadSliderThumbShape(
radius: effectiveThumbRadius,
borderColor: effectiveThumbBorderColor,
disabledBorderColor: effectiveDisabledThumbBorderColor,
thumbColor: effectiveThumbColor,
return Focus(
onFocusChange: (focused) {
setState(() {
isFocused = focused;
});
},
skipTraversal: true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the Focus widget, use the ShadFocusable instead. No need to use setState.
Pass the widget.focusNode to it, and in the builder you get the focused value.
Merge the new main to have all the properties like skipTraversal

child: Theme(
data: mTheme.copyWith(
sliderTheme: mTheme.sliderTheme.copyWith(
trackHeight: effectiveTrackHeight,
thumbShape: ShadSliderThumbShape(
radius: effectiveThumbRadius,
borderColor: effectiveThumbBorderColor,
disabledBorderColor: effectiveDisabledThumbBorderColor,
thumbColor: effectiveThumbColor,
disabledThumbColor: effectiveDisabledThumbColor,
focused: isFocused,
),
overlayShape: SliderComponentShape.noOverlay,
activeTrackColor: effectiveActiveTrackColor,
disabledActiveTrackColor: effectiveDisabledActiveTrackColor,
inactiveTrackColor: effectiveInactiveTrackColor,
disabledInactiveTrackColor: effectiveDisabledInactiveTrackColor,
disabledThumbColor: effectiveDisabledThumbColor,
),
overlayShape: SliderComponentShape.noOverlay,
activeTrackColor: effectiveActiveTrackColor,
disabledActiveTrackColor: effectiveDisabledActiveTrackColor,
inactiveTrackColor: effectiveInactiveTrackColor,
disabledInactiveTrackColor: effectiveDisabledInactiveTrackColor,
disabledThumbColor: effectiveDisabledThumbColor,
),
),
child: ValueListenableBuilder(
valueListenable: controller,
builder: (context, value, child) {
return Slider(
value: value,
min: effectiveMin,
max: effectiveMax,
mouseCursor: widget.enabled
? effectiveMouseCursor
: effectiveDisabledMouseCursor,
onChanged: widget.enabled
? (v) {
controller.value = v;
widget.onChanged?.call(v);
}
: null,
autofocus: widget.autofocus,
focusNode: widget.focusNode,
onChangeStart: widget.onChangeStart,
onChangeEnd: widget.onChangeEnd,
divisions: widget.divisions,
label: widget.label,
semanticFormatterCallback: widget.semanticFormatterCallback,
allowedInteraction: widget.allowedInteraction,
);
},
child: ValueListenableBuilder(
valueListenable: controller,
builder: (context, value, child) {
return Slider(
value: value,
min: effectiveMin,
max: effectiveMax,
mouseCursor: widget.enabled
? effectiveMouseCursor
: effectiveDisabledMouseCursor,
onChanged: widget.enabled
? (v) {
controller.value = v;
widget.onChanged?.call(v);
}
: null,
autofocus: widget.autofocus,
focusNode: widget.focusNode,
onChangeStart: widget.onChangeStart,
onChangeEnd: widget.onChangeEnd,
divisions: widget.divisions,
label: widget.label,
semanticFormatterCallback: widget.semanticFormatterCallback,
allowedInteraction: widget.allowedInteraction,
);
},
),
),
);
}
Expand All @@ -199,13 +210,15 @@ class ShadSliderThumbShape extends SliderComponentShape {
required this.disabledBorderColor,
required this.thumbColor,
required this.disabledThumbColor,
this.focused = false,
});

final double radius;
final Color borderColor;
final Color disabledBorderColor;
final Color thumbColor;
final Color disabledThumbColor;
final bool focused;

@override
Size getPreferredSize(bool isEnabled, bool isDiscrete) {
Expand Down Expand Up @@ -238,7 +251,7 @@ class ShadSliderThumbShape extends SliderComponentShape {

canvas.drawCircle(
center,
radius,
focused ? radius + 4 : radius,
Paint()..color = color,
);

Expand All @@ -255,5 +268,14 @@ class ShadSliderThumbShape extends SliderComponentShape {
..style = PaintingStyle.stroke;

canvas.drawCircle(center, radius, paintBorder);

if (focused) {
final paintFocus = Paint()
..color = borderColor
..strokeWidth = 2
..style = PaintingStyle.stroke;

canvas.drawCircle(center, radius + 4, paintFocus);
}
}
}