toggle_switch_widget.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'package:flutter/material.dart';
  2. import 'package:photos/ente_theme_data.dart';
  3. import 'package:photos/ui/common/loading_widget.dart';
  4. import 'package:photos/utils/debouncer.dart';
  5. enum ExecutionState {
  6. idle,
  7. inProgress,
  8. successful,
  9. }
  10. typedef OnChangedCallBack = Future<void> Function();
  11. typedef ValueCallBack = bool Function();
  12. class ToggleSwitchWidget extends StatefulWidget {
  13. final ValueCallBack value;
  14. final OnChangedCallBack onChanged;
  15. const ToggleSwitchWidget({
  16. required this.value,
  17. required this.onChanged,
  18. Key? key,
  19. }) : super(key: key);
  20. @override
  21. State<ToggleSwitchWidget> createState() => _ToggleSwitchWidgetState();
  22. }
  23. class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
  24. late bool toggleValue;
  25. ExecutionState executionState = ExecutionState.idle;
  26. final _debouncer = Debouncer(const Duration(milliseconds: 300));
  27. @override
  28. void initState() {
  29. toggleValue = widget.value.call();
  30. super.initState();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. final enteColorScheme = Theme.of(context).colorScheme.enteTheme.colorScheme;
  35. final Widget stateIcon = _stateIcon(enteColorScheme);
  36. return Row(
  37. children: [
  38. AnimatedSwitcher(
  39. duration: const Duration(milliseconds: 175),
  40. switchInCurve: Curves.easeInExpo,
  41. switchOutCurve: Curves.easeOutExpo,
  42. child: stateIcon,
  43. ),
  44. SizedBox(
  45. height: 32,
  46. child: FittedBox(
  47. fit: BoxFit.contain,
  48. child: Switch.adaptive(
  49. activeColor: enteColorScheme.primary400,
  50. inactiveTrackColor: enteColorScheme.fillMuted,
  51. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  52. value: toggleValue,
  53. onChanged: (negationOfToggleValue) async {
  54. setState(() {
  55. toggleValue = negationOfToggleValue;
  56. //start showing inProgress statu icons if toggle takes more than debounce time
  57. _debouncer.run(
  58. () => Future(
  59. () {
  60. setState(() {
  61. executionState = ExecutionState.inProgress;
  62. });
  63. },
  64. ),
  65. );
  66. });
  67. final Stopwatch stopwatch = Stopwatch()..start();
  68. await widget.onChanged.call();
  69. //for toggle feedback on short unsuccessful onChanged
  70. await _feedbackOnUnsuccessfulToggle(stopwatch);
  71. //debouncer gets canceled if onChanged takes less than debounce time
  72. _debouncer.cancelDebounce();
  73. setState(() {
  74. final newValue = widget.value.call();
  75. //if onchanged on toggle is successful
  76. if (toggleValue == newValue) {
  77. if (executionState == ExecutionState.inProgress) {
  78. executionState = ExecutionState.successful;
  79. Future.delayed(const Duration(seconds: 1), () {
  80. setState(() {
  81. executionState = ExecutionState.idle;
  82. });
  83. });
  84. }
  85. } else {
  86. toggleValue = !toggleValue;
  87. executionState = ExecutionState.idle;
  88. }
  89. });
  90. },
  91. ),
  92. ),
  93. ),
  94. ],
  95. );
  96. }
  97. Widget _stateIcon(enteColorScheme) {
  98. if (executionState == ExecutionState.idle) {
  99. return const SizedBox.shrink();
  100. } else if (executionState == ExecutionState.inProgress) {
  101. return EnteLoadingWidget(
  102. color: enteColorScheme.strokeMuted,
  103. );
  104. } else if (executionState == ExecutionState.successful) {
  105. return Icon(
  106. Icons.check_outlined,
  107. color: enteColorScheme.primary500,
  108. );
  109. } else {
  110. return const SizedBox.shrink();
  111. }
  112. }
  113. Future<void> _feedbackOnUnsuccessfulToggle(Stopwatch stopwatch) async {
  114. final timeElapsed = stopwatch.elapsedMilliseconds;
  115. if (timeElapsed < 200) {
  116. await Future.delayed(
  117. Duration(milliseconds: 200 - timeElapsed),
  118. );
  119. }
  120. }
  121. }