Browse Source

show toggle feedback on short unsuccessful onchanged

ashilkn 2 years ago
parent
commit
1b245a5021
1 changed files with 12 additions and 0 deletions
  1. 12 0
      lib/ui/components/toggle_switch_widget.dart

+ 12 - 0
lib/ui/components/toggle_switch_widget.dart

@@ -71,7 +71,10 @@ class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
                     ),
                     ),
                   );
                   );
                 });
                 });
+                final Stopwatch stopwatch = Stopwatch()..start();
                 await widget.onChanged.call();
                 await widget.onChanged.call();
+                //for toggle feedback on short unsuccessful onChanged
+                await _feedbackOnUnsuccessfulToggle(stopwatch);
                 //debouncer gets canceled if onChanged takes less than debounce time
                 //debouncer gets canceled if onChanged takes less than debounce time
                 _debouncer.cancelDebounce();
                 _debouncer.cancelDebounce();
                 setState(() {
                 setState(() {
@@ -115,4 +118,13 @@ class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
       return const SizedBox.shrink();
       return const SizedBox.shrink();
     }
     }
   }
   }
+
+  Future<void> _feedbackOnUnsuccessfulToggle(Stopwatch stopwatch) async {
+    final timeElapsed = stopwatch.elapsedMilliseconds;
+    if (timeElapsed < 200) {
+      await Future.delayed(
+        Duration(milliseconds: 200 - timeElapsed),
+      );
+    }
+  }
 }
 }