toggle_switch_widget.dart 4.5 KB

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