toggle_switch_widget.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. super.initState();
  25. toggleValue = widget.value.call();
  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. inactiveTrackColor: Colors.transparent,
  48. activeTrackColor: enteColorScheme.primary500,
  49. activeColor: Colors.white,
  50. inactiveThumbColor: enteColorScheme.primary500,
  51. trackOutlineColor: MaterialStateColor.resolveWith(
  52. (states) => enteColorScheme.primary500,
  53. ),
  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. if (mounted) {
  85. setState(() {
  86. executionState = ExecutionState.idle;
  87. });
  88. }
  89. });
  90. }
  91. } else {
  92. toggleValue = !toggleValue!;
  93. executionState = ExecutionState.idle;
  94. }
  95. });
  96. },
  97. ),
  98. ),
  99. ),
  100. ],
  101. );
  102. }
  103. Widget _stateIcon(enteColorScheme) {
  104. if (executionState == ExecutionState.idle) {
  105. return const SizedBox(width: 24);
  106. } else if (executionState == ExecutionState.inProgress) {
  107. return EnteLoadingWidget(
  108. color: enteColorScheme.strokeMuted,
  109. );
  110. } else if (executionState == ExecutionState.successful) {
  111. return Padding(
  112. padding: const EdgeInsets.symmetric(horizontal: 1),
  113. child: Icon(
  114. Icons.check_outlined,
  115. size: 22,
  116. color: enteColorScheme.primary500,
  117. ),
  118. );
  119. } else {
  120. return const SizedBox(width: 24);
  121. }
  122. }
  123. Future<void> _feedbackOnUnsuccessfulToggle(Stopwatch stopwatch) async {
  124. final timeElapsed = stopwatch.elapsedMilliseconds;
  125. if (timeElapsed < 200) {
  126. await Future.delayed(
  127. Duration(milliseconds: 200 - timeElapsed),
  128. );
  129. }
  130. }
  131. }