toggle_switch_widget.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 FutureValueCallBack = Future<bool> Function();
  12. class ToggleSwitchWidget extends StatefulWidget {
  13. final FutureValueCallBack value;
  14. final OnChangedCallBack onChanged;
  15. final bool initialValue;
  16. const ToggleSwitchWidget({
  17. required this.value,
  18. required this.onChanged,
  19. required this.initialValue,
  20. Key? key,
  21. }) : super(key: key);
  22. @override
  23. State<ToggleSwitchWidget> createState() => _ToggleSwitchWidgetState();
  24. }
  25. class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
  26. late Future<bool> futureToggleValue;
  27. late bool toggleValue;
  28. ExecutionState executionState = ExecutionState.idle;
  29. final _debouncer = Debouncer(const Duration(milliseconds: 300));
  30. @override
  31. void initState() {
  32. futureToggleValue = widget.value.call();
  33. toggleValue = widget.initialValue;
  34. super.initState();
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. final enteColorScheme = Theme.of(context).colorScheme.enteTheme.colorScheme;
  39. final Widget stateIcon = _stateIcon(enteColorScheme);
  40. return FutureBuilder(
  41. future: futureToggleValue,
  42. builder: (context, snapshot) {
  43. return Row(
  44. children: [
  45. Padding(
  46. padding: const EdgeInsets.only(right: 2),
  47. child: AnimatedSwitcher(
  48. duration: const Duration(milliseconds: 175),
  49. switchInCurve: Curves.easeInExpo,
  50. switchOutCurve: Curves.easeOutExpo,
  51. child: stateIcon,
  52. ),
  53. ),
  54. SizedBox(
  55. height: 31,
  56. child: FittedBox(
  57. fit: BoxFit.contain,
  58. child: Switch.adaptive(
  59. activeColor: enteColorScheme.primary400,
  60. inactiveTrackColor: enteColorScheme.fillMuted,
  61. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  62. value: toggleValue,
  63. onChanged: (negationOfToggleValue) async {
  64. setState(() {
  65. toggleValue = negationOfToggleValue;
  66. //start showing inProgress statu icons if toggle takes more than debounce time
  67. _debouncer.run(
  68. () => Future(
  69. () {
  70. setState(() {
  71. executionState = ExecutionState.inProgress;
  72. });
  73. },
  74. ),
  75. );
  76. });
  77. final Stopwatch stopwatch = Stopwatch()..start();
  78. await widget.onChanged.call();
  79. //for toggle feedback on short unsuccessful onChanged
  80. await _feedbackOnUnsuccessfulToggle(stopwatch);
  81. //debouncer gets canceled if onChanged takes less than debounce time
  82. _debouncer.cancelDebounce();
  83. widget.value.call().then((newValue) {
  84. setState(() {
  85. //if onchanged on toggle is successful
  86. // print('here');
  87. // print(toggleValue);
  88. // print("newValue : $newValue");
  89. if (toggleValue == newValue) {
  90. if (executionState == ExecutionState.inProgress) {
  91. executionState = ExecutionState.successful;
  92. Future.delayed(const Duration(seconds: 2), () {
  93. setState(() {
  94. executionState = ExecutionState.idle;
  95. });
  96. });
  97. }
  98. } else {
  99. toggleValue = !toggleValue;
  100. executionState = ExecutionState.idle;
  101. }
  102. });
  103. });
  104. },
  105. ),
  106. ),
  107. ),
  108. ],
  109. );
  110. },
  111. );
  112. }
  113. Widget _stateIcon(enteColorScheme) {
  114. if (executionState == ExecutionState.idle) {
  115. return const SizedBox(width: 24);
  116. } else if (executionState == ExecutionState.inProgress) {
  117. return EnteLoadingWidget(
  118. color: enteColorScheme.strokeMuted,
  119. );
  120. } else if (executionState == ExecutionState.successful) {
  121. return Padding(
  122. padding: const EdgeInsets.symmetric(horizontal: 1),
  123. child: Icon(
  124. Icons.check_outlined,
  125. size: 22,
  126. color: enteColorScheme.primary500,
  127. ),
  128. );
  129. } else {
  130. return const SizedBox(width: 24);
  131. }
  132. }
  133. Future<void> _feedbackOnUnsuccessfulToggle(Stopwatch stopwatch) async {
  134. final timeElapsed = stopwatch.elapsedMilliseconds;
  135. if (timeElapsed < 200) {
  136. await Future.delayed(
  137. Duration(milliseconds: 200 - timeElapsed),
  138. );
  139. }
  140. }
  141. }