toggle_switch_widget.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 FutureVoidCallback = Future<void> Function();
  11. typedef BoolCallBack = bool Function();
  12. class ToggleSwitchWidget extends StatefulWidget {
  13. final BoolCallBack value;
  14. final FutureVoidCallback 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. 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. Padding(
  39. padding: const EdgeInsets.only(right: 2),
  40. child: AnimatedSwitcher(
  41. duration: const Duration(milliseconds: 175),
  42. switchInCurve: Curves.easeInExpo,
  43. switchOutCurve: Curves.easeOutExpo,
  44. child: stateIcon,
  45. ),
  46. ),
  47. SizedBox(
  48. height: 31,
  49. child: FittedBox(
  50. fit: BoxFit.contain,
  51. child: Switch.adaptive(
  52. activeColor: enteColorScheme.primary400,
  53. inactiveTrackColor: enteColorScheme.fillMuted,
  54. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  55. value: toggleValue ?? false,
  56. onChanged: (negationOfToggleValue) async {
  57. setState(() {
  58. toggleValue = negationOfToggleValue;
  59. //start showing inProgress statu icons if toggle takes more than debounce time
  60. _debouncer.run(
  61. () => Future(
  62. () {
  63. setState(() {
  64. executionState = ExecutionState.inProgress;
  65. });
  66. },
  67. ),
  68. );
  69. });
  70. final Stopwatch stopwatch = Stopwatch()..start();
  71. await widget.onChanged.call().onError(
  72. (error, stackTrace) => _debouncer.cancelDebounce(),
  73. );
  74. //for toggle feedback on short unsuccessful onChanged
  75. await _feedbackOnUnsuccessfulToggle(stopwatch);
  76. //debouncer gets canceled if onChanged takes less than debounce time
  77. _debouncer.cancelDebounce();
  78. final newValue = widget.value.call();
  79. setState(() {
  80. if (toggleValue == newValue) {
  81. if (executionState == ExecutionState.inProgress) {
  82. executionState = ExecutionState.successful;
  83. Future.delayed(const Duration(seconds: 2), () {
  84. setState(() {
  85. executionState = ExecutionState.idle;
  86. });
  87. });
  88. }
  89. } else {
  90. toggleValue = !toggleValue!;
  91. executionState = ExecutionState.idle;
  92. }
  93. });
  94. },
  95. ),
  96. ),
  97. ),
  98. ],
  99. );
  100. }
  101. Widget _stateIcon(enteColorScheme) {
  102. if (executionState == ExecutionState.idle) {
  103. return const SizedBox(width: 24);
  104. } else if (executionState == ExecutionState.inProgress) {
  105. return EnteLoadingWidget(
  106. color: enteColorScheme.strokeMuted,
  107. );
  108. } else if (executionState == ExecutionState.successful) {
  109. return Padding(
  110. padding: const EdgeInsets.symmetric(horizontal: 1),
  111. child: Icon(
  112. Icons.check_outlined,
  113. size: 22,
  114. color: enteColorScheme.primary500,
  115. ),
  116. );
  117. } else {
  118. return const SizedBox(width: 24);
  119. }
  120. }
  121. Future<void> _feedbackOnUnsuccessfulToggle(Stopwatch stopwatch) async {
  122. final timeElapsed = stopwatch.elapsedMilliseconds;
  123. if (timeElapsed < 200) {
  124. await Future.delayed(
  125. Duration(milliseconds: 200 - timeElapsed),
  126. );
  127. }
  128. }
  129. }