toggle_switch_widget.dart 4.4 KB

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