text_input_widget.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:photos/theme/ente_theme.dart';
  4. import 'package:photos/ui/common/loading_widget.dart';
  5. import 'package:photos/ui/components/dialog_widget.dart';
  6. import 'package:photos/utils/debouncer.dart';
  7. import 'package:photos/utils/separators_util.dart';
  8. enum ExecutionState {
  9. idle,
  10. inProgress,
  11. error,
  12. successful;
  13. }
  14. class TextInputWidget extends StatefulWidget {
  15. final String? label;
  16. final String? message;
  17. final String? hintText;
  18. final IconData? prefixIcon;
  19. final String? initialValue;
  20. final Alignment? alignMessage;
  21. final bool? autoFocus;
  22. final int? maxLength;
  23. final ValueNotifier? submitNotifier;
  24. final bool alwaysShowSuccessState;
  25. final bool showOnlyLoadingState;
  26. final FutureVoidCallbackParamStr onSubmit;
  27. const TextInputWidget({
  28. required this.onSubmit,
  29. this.label,
  30. this.message,
  31. this.hintText,
  32. this.prefixIcon,
  33. this.initialValue,
  34. this.alignMessage,
  35. this.autoFocus,
  36. this.maxLength,
  37. this.submitNotifier,
  38. this.alwaysShowSuccessState = true,
  39. this.showOnlyLoadingState = false,
  40. super.key,
  41. });
  42. @override
  43. State<TextInputWidget> createState() => _TextInputWidgetState();
  44. }
  45. class _TextInputWidgetState extends State<TextInputWidget> {
  46. final _textController = TextEditingController();
  47. final _debouncer = Debouncer(const Duration(milliseconds: 300));
  48. final ValueNotifier<ExecutionState> _executionStateNotifier =
  49. ValueNotifier(ExecutionState.idle);
  50. @override
  51. void initState() {
  52. widget.submitNotifier?.addListener(() {
  53. _onSubmit();
  54. });
  55. _executionStateNotifier.addListener(() {
  56. setState(() {});
  57. });
  58. super.initState();
  59. }
  60. @override
  61. void dispose() {
  62. widget.submitNotifier?.dispose();
  63. _executionStateNotifier.dispose();
  64. super.dispose();
  65. }
  66. @override
  67. Widget build(BuildContext context) {
  68. if (widget.initialValue != null) {
  69. _textController.value = TextEditingValue(
  70. text: widget.initialValue!,
  71. selection: TextSelection.collapsed(offset: widget.initialValue!.length),
  72. );
  73. }
  74. final colorScheme = getEnteColorScheme(context);
  75. final textTheme = getEnteTextTheme(context);
  76. var textInputChildren = <Widget>[];
  77. if (widget.label != null) textInputChildren.add(Text(widget.label!));
  78. textInputChildren.add(
  79. ClipRRect(
  80. borderRadius: const BorderRadius.all(Radius.circular(8)),
  81. child: Material(
  82. child: TextFormField(
  83. autofocus: widget.autoFocus ?? false,
  84. controller: _textController,
  85. inputFormatters: widget.maxLength != null
  86. ? [LengthLimitingTextInputFormatter(50)]
  87. : null,
  88. decoration: InputDecoration(
  89. hintText: widget.hintText,
  90. hintStyle: textTheme.body.copyWith(color: colorScheme.textMuted),
  91. filled: true,
  92. contentPadding: const EdgeInsets.fromLTRB(
  93. 12,
  94. 12,
  95. 0,
  96. 12,
  97. ),
  98. border: const UnderlineInputBorder(
  99. borderSide: BorderSide.none,
  100. ),
  101. focusedBorder: OutlineInputBorder(
  102. borderSide: BorderSide(color: colorScheme.strokeMuted),
  103. borderRadius: BorderRadius.circular(8),
  104. ),
  105. suffixIcon: Padding(
  106. padding: const EdgeInsets.only(right: 12),
  107. child: AnimatedSwitcher(
  108. duration: const Duration(milliseconds: 175),
  109. switchInCurve: Curves.easeInExpo,
  110. switchOutCurve: Curves.easeOutExpo,
  111. child: SuffixIconWidget(
  112. _executionStateNotifier.value,
  113. key: ValueKey(_executionStateNotifier.value),
  114. ),
  115. ),
  116. ),
  117. prefixIconConstraints: const BoxConstraints(
  118. maxHeight: 44,
  119. maxWidth: 44,
  120. minHeight: 44,
  121. minWidth: 44,
  122. ),
  123. suffixIconConstraints: const BoxConstraints(
  124. maxHeight: 24,
  125. maxWidth: 36,
  126. minHeight: 24,
  127. minWidth: 36,
  128. ),
  129. prefixIcon: widget.prefixIcon != null
  130. ? Icon(
  131. widget.prefixIcon,
  132. color: colorScheme.strokeMuted,
  133. )
  134. : null,
  135. ),
  136. onEditingComplete: () {},
  137. ),
  138. ),
  139. ),
  140. );
  141. if (widget.message != null) {
  142. textInputChildren.add(
  143. Padding(
  144. padding: const EdgeInsets.symmetric(horizontal: 8),
  145. child: Align(
  146. alignment: widget.alignMessage ?? Alignment.centerLeft,
  147. child: Text(
  148. widget.message!,
  149. style: textTheme.small.copyWith(color: colorScheme.textMuted),
  150. ),
  151. ),
  152. ),
  153. );
  154. }
  155. textInputChildren =
  156. addSeparators(textInputChildren, const SizedBox(height: 4));
  157. return Column(
  158. mainAxisSize: MainAxisSize.min,
  159. crossAxisAlignment: CrossAxisAlignment.start,
  160. children: textInputChildren,
  161. );
  162. }
  163. Future<void> _onSubmit() async {
  164. _debouncer.run(
  165. () => Future(
  166. () {
  167. _executionStateNotifier.value = ExecutionState.inProgress;
  168. },
  169. ),
  170. );
  171. await widget.onSubmit.call(_textController.text).then(
  172. (value) {
  173. widget.alwaysShowSuccessState
  174. ? _executionStateNotifier.value = ExecutionState.successful
  175. : null;
  176. },
  177. onError: (error, stackTrace) => _debouncer.cancelDebounce(),
  178. );
  179. _debouncer.cancelDebounce();
  180. if (widget.alwaysShowSuccessState) {
  181. Future.delayed(const Duration(seconds: 2), () {
  182. _executionStateNotifier.value = ExecutionState.idle;
  183. });
  184. return;
  185. }
  186. if (_executionStateNotifier.value == ExecutionState.inProgress) {
  187. if (widget.showOnlyLoadingState) {
  188. _executionStateNotifier.value = ExecutionState.idle;
  189. } else {
  190. _executionStateNotifier.value = ExecutionState.successful;
  191. Future.delayed(const Duration(seconds: 2), () {
  192. _executionStateNotifier.value = ExecutionState.idle;
  193. });
  194. }
  195. }
  196. }
  197. }
  198. class SuffixIconWidget extends StatelessWidget {
  199. final ExecutionState executionState;
  200. const SuffixIconWidget(this.executionState, {super.key});
  201. @override
  202. Widget build(BuildContext context) {
  203. final Widget trailingWidget;
  204. final colorScheme = getEnteColorScheme(context);
  205. if (executionState == ExecutionState.idle) {
  206. trailingWidget = const SizedBox.shrink();
  207. } else if (executionState == ExecutionState.inProgress) {
  208. trailingWidget = EnteLoadingWidget(
  209. color: colorScheme.strokeMuted,
  210. );
  211. } else if (executionState == ExecutionState.successful) {
  212. trailingWidget = Icon(
  213. Icons.check_outlined,
  214. size: 22,
  215. color: colorScheme.primary500,
  216. );
  217. } else {
  218. trailingWidget = const SizedBox.shrink();
  219. }
  220. return trailingWidget;
  221. }
  222. }