From c7c1d326360db6425da223cfa06e427a01db2804 Mon Sep 17 00:00:00 2001 From: HelightDev <38019186+helightdev@users.noreply.github.com> Date: Wed, 27 Nov 2024 10:54:42 +0100 Subject: [PATCH] Fix onChanged being called on every controller update (#202) --- lib/src/components/slider.dart | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/src/components/slider.dart b/lib/src/components/slider.dart index 494266c9..c86c6567 100644 --- a/lib/src/components/slider.dart +++ b/lib/src/components/slider.dart @@ -79,15 +79,8 @@ class _ShadSliderState extends State { initialValue: widget.initialValue!, ); - @override - void initState() { - super.initState(); - controller.addListener(onChanged); - } - @override void dispose() { - controller.removeListener(onChanged); // dispose the internal controller if (widget.controller == null) { controller.dispose(); @@ -95,10 +88,6 @@ class _ShadSliderState extends State { super.dispose(); } - void onChanged() { - widget.onChanged?.call(controller.value); - } - @override Widget build(BuildContext context) { final theme = ShadTheme.of(context); @@ -182,7 +171,10 @@ class _ShadSliderState extends State { mouseCursor: widget.enabled ? effectiveMouseCursor : effectiveDisabledMouseCursor, - onChanged: widget.enabled ? (v) => controller.value = v : null, + onChanged: widget.enabled ? (v) { + controller.value = v; + widget.onChanged?.call(v); + } : null, autofocus: widget.autofocus, focusNode: widget.focusNode, onChangeStart: widget.onChangeStart,