toggle_switch_widget.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 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. 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,
  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();
  72. //for toggle feedback on short unsuccessful onChanged
  73. await _feedbackOnUnsuccessfulToggle(stopwatch);
  74. //debouncer gets canceled if onChanged takes less than debounce time
  75. _debouncer.cancelDebounce();
  76. final newValue = widget.value.call();
  77. setState(() {
  78. if (toggleValue == newValue) {
  79. if (executionState == ExecutionState.inProgress) {
  80. executionState = ExecutionState.successful;
  81. Future.delayed(const Duration(seconds: 2), () {
  82. setState(() {
  83. executionState = ExecutionState.idle;
  84. });
  85. });
  86. }
  87. } else {
  88. toggleValue = !toggleValue;
  89. executionState = ExecutionState.idle;
  90. }
  91. });
  92. },
  93. ),
  94. ),
  95. ),
  96. ],
  97. );
  98. }
  99. Widget _stateIcon(enteColorScheme) {
  100. if (executionState == ExecutionState.idle) {
  101. return const SizedBox(width: 24);
  102. } else if (executionState == ExecutionState.inProgress) {
  103. return EnteLoadingWidget(
  104. color: enteColorScheme.strokeMuted,
  105. );
  106. } else if (executionState == ExecutionState.successful) {
  107. return Padding(
  108. padding: const EdgeInsets.symmetric(horizontal: 1),
  109. child: Icon(
  110. Icons.check_outlined,
  111. size: 22,
  112. color: enteColorScheme.primary500,
  113. ),
  114. );
  115. } else {
  116. return const SizedBox(width: 24);
  117. }
  118. }
  119. Future<void> _feedbackOnUnsuccessfulToggle(Stopwatch stopwatch) async {
  120. final timeElapsed = stopwatch.elapsedMilliseconds;
  121. if (timeElapsed < 200) {
  122. await Future.delayed(
  123. Duration(milliseconds: 200 - timeElapsed),
  124. );
  125. }
  126. }
  127. }