text_input_widget.dart 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:photos/models/execution_states.dart';
  4. import 'package:photos/models/typedefs.dart';
  5. import 'package:photos/theme/ente_theme.dart';
  6. import 'package:photos/ui/common/loading_widget.dart';
  7. import 'package:photos/utils/debouncer.dart';
  8. import 'package:photos/utils/separators_util.dart';
  9. class TextInputWidget extends StatefulWidget {
  10. final String? label;
  11. final String? message;
  12. final String? hintText;
  13. final IconData? prefixIcon;
  14. final String? initialValue;
  15. final Alignment? alignMessage;
  16. final bool? autoFocus;
  17. final int? maxLength;
  18. ///TextInputWidget will listen to this notifier and executes onSubmit when
  19. ///notified.
  20. final ValueNotifier? submitNotifier;
  21. final bool alwaysShowSuccessState;
  22. final bool showOnlyLoadingState;
  23. final FutureVoidCallbackParamStr onSubmit;
  24. final bool popNavAfterSubmission;
  25. final bool shouldSurfaceExecutionStates;
  26. final TextCapitalization? textCapitalization;
  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 = false,
  39. this.showOnlyLoadingState = false,
  40. this.popNavAfterSubmission = false,
  41. this.shouldSurfaceExecutionStates = true,
  42. this.textCapitalization = TextCapitalization.none,
  43. super.key,
  44. });
  45. @override
  46. State<TextInputWidget> createState() => _TextInputWidgetState();
  47. }
  48. class _TextInputWidgetState extends State<TextInputWidget> {
  49. ExecutionState executionState = ExecutionState.idle;
  50. final _textController = TextEditingController();
  51. final _debouncer = Debouncer(const Duration(milliseconds: 300));
  52. ///This is to pass if the TextInputWidget is in a dialog and an error is
  53. ///thrown in executing onSubmit by passing it as arg in Navigator.pop()
  54. Exception? _exception;
  55. @override
  56. void initState() {
  57. widget.submitNotifier?.addListener(_onSubmit);
  58. if (widget.initialValue != null) {
  59. _textController.value = TextEditingValue(
  60. text: widget.initialValue!,
  61. selection: TextSelection.collapsed(offset: widget.initialValue!.length),
  62. );
  63. }
  64. super.initState();
  65. }
  66. @override
  67. void dispose() {
  68. widget.submitNotifier?.removeListener(_onSubmit);
  69. _textController.dispose();
  70. super.dispose();
  71. }
  72. @override
  73. Widget build(BuildContext context) {
  74. if (executionState == ExecutionState.successful) {
  75. Future.delayed(Duration(seconds: widget.popNavAfterSubmission ? 1 : 2),
  76. () {
  77. setState(() {
  78. executionState = ExecutionState.idle;
  79. });
  80. });
  81. }
  82. final colorScheme = getEnteColorScheme(context);
  83. final textTheme = getEnteTextTheme(context);
  84. var textInputChildren = <Widget>[];
  85. if (widget.label != null) {
  86. textInputChildren.add(Text(widget.label!));
  87. }
  88. textInputChildren.add(
  89. ClipRRect(
  90. borderRadius: const BorderRadius.all(Radius.circular(8)),
  91. child: Material(
  92. child: TextFormField(
  93. textCapitalization: widget.textCapitalization!,
  94. autofocus: widget.autoFocus ?? false,
  95. controller: _textController,
  96. inputFormatters: widget.maxLength != null
  97. ? [LengthLimitingTextInputFormatter(50)]
  98. : null,
  99. decoration: InputDecoration(
  100. hintText: widget.hintText,
  101. hintStyle: textTheme.body.copyWith(color: colorScheme.textMuted),
  102. filled: true,
  103. fillColor: colorScheme.fillFaint,
  104. contentPadding: const EdgeInsets.fromLTRB(
  105. 12,
  106. 12,
  107. 0,
  108. 12,
  109. ),
  110. border: const UnderlineInputBorder(
  111. borderSide: BorderSide.none,
  112. ),
  113. focusedBorder: OutlineInputBorder(
  114. borderSide: BorderSide(color: colorScheme.strokeMuted),
  115. borderRadius: BorderRadius.circular(8),
  116. ),
  117. suffixIcon: Padding(
  118. padding: const EdgeInsets.symmetric(horizontal: 12),
  119. child: AnimatedSwitcher(
  120. duration: const Duration(milliseconds: 175),
  121. switchInCurve: Curves.easeInExpo,
  122. switchOutCurve: Curves.easeOutExpo,
  123. child: SuffixIconWidget(
  124. key: ValueKey(executionState),
  125. executionState: executionState,
  126. shouldSurfaceExecutionStates:
  127. widget.shouldSurfaceExecutionStates,
  128. ),
  129. ),
  130. ),
  131. prefixIconConstraints: const BoxConstraints(
  132. maxHeight: 44,
  133. maxWidth: 44,
  134. minHeight: 44,
  135. minWidth: 44,
  136. ),
  137. suffixIconConstraints: const BoxConstraints(
  138. maxHeight: 24,
  139. maxWidth: 48,
  140. minHeight: 24,
  141. minWidth: 48,
  142. ),
  143. prefixIcon: widget.prefixIcon != null
  144. ? Icon(
  145. widget.prefixIcon,
  146. color: colorScheme.strokeMuted,
  147. )
  148. : null,
  149. ),
  150. onEditingComplete: () {
  151. _onSubmit();
  152. },
  153. ),
  154. ),
  155. ),
  156. );
  157. if (widget.message != null) {
  158. textInputChildren.add(
  159. Padding(
  160. padding: const EdgeInsets.symmetric(horizontal: 8),
  161. child: Align(
  162. alignment: widget.alignMessage ?? Alignment.centerLeft,
  163. child: Text(
  164. widget.message!,
  165. style: textTheme.small.copyWith(color: colorScheme.textMuted),
  166. ),
  167. ),
  168. ),
  169. );
  170. }
  171. textInputChildren =
  172. addSeparators(textInputChildren, const SizedBox(height: 4));
  173. return Column(
  174. mainAxisSize: MainAxisSize.min,
  175. crossAxisAlignment: CrossAxisAlignment.start,
  176. children: textInputChildren,
  177. );
  178. }
  179. void _onSubmit() async {
  180. _debouncer.run(
  181. () => Future(() {
  182. setState(() {
  183. executionState = ExecutionState.inProgress;
  184. });
  185. }),
  186. );
  187. try {
  188. await widget.onSubmit.call(_textController.text);
  189. } catch (e) {
  190. executionState = ExecutionState.error;
  191. _debouncer.cancelDebounce();
  192. _exception = e as Exception;
  193. if (!widget.popNavAfterSubmission) {
  194. rethrow;
  195. }
  196. }
  197. widget.alwaysShowSuccessState && _debouncer.isActive()
  198. ? executionState = ExecutionState.successful
  199. : null;
  200. _debouncer.cancelDebounce();
  201. if (executionState == ExecutionState.successful) {
  202. setState(() {});
  203. }
  204. // when the time taken by widget.onSubmit is approximately equal to the debounce
  205. // time, the callback is getting executed when/after the if condition
  206. // below is executing/executed which results in execution state stuck at
  207. // idle state. This Future is for delaying the execution of the if
  208. // condition so that the calback in the debouncer finishes execution before.
  209. await Future.delayed(const Duration(milliseconds: 5));
  210. if (executionState == ExecutionState.inProgress ||
  211. executionState == ExecutionState.error) {
  212. if (executionState == ExecutionState.inProgress) {
  213. if (mounted) {
  214. if (widget.showOnlyLoadingState) {
  215. setState(() {
  216. executionState = ExecutionState.idle;
  217. });
  218. _popNavigatorStack(context);
  219. } else {
  220. setState(() {
  221. executionState = ExecutionState.successful;
  222. Future.delayed(
  223. Duration(
  224. seconds: widget.shouldSurfaceExecutionStates
  225. ? (widget.popNavAfterSubmission ? 1 : 2)
  226. : 0,
  227. ), () {
  228. widget.popNavAfterSubmission
  229. ? _popNavigatorStack(context)
  230. : null;
  231. if (mounted) {
  232. setState(() {
  233. executionState = ExecutionState.idle;
  234. });
  235. }
  236. });
  237. });
  238. }
  239. }
  240. }
  241. if (executionState == ExecutionState.error) {
  242. setState(() {
  243. executionState = ExecutionState.idle;
  244. widget.popNavAfterSubmission
  245. ? Future.delayed(
  246. const Duration(seconds: 0),
  247. () => _popNavigatorStack(context, e: _exception),
  248. )
  249. : null;
  250. });
  251. }
  252. } else {
  253. if (widget.popNavAfterSubmission) {
  254. Future.delayed(
  255. Duration(seconds: widget.alwaysShowSuccessState ? 1 : 0),
  256. () => _popNavigatorStack(context),
  257. );
  258. }
  259. }
  260. }
  261. void _popNavigatorStack(BuildContext context, {Exception? e}) {
  262. Navigator.of(context).canPop() ? Navigator.of(context).pop(e) : null;
  263. }
  264. }
  265. //todo: Add clear and custom icon for suffic icon
  266. class SuffixIconWidget extends StatelessWidget {
  267. final ExecutionState executionState;
  268. final bool shouldSurfaceExecutionStates;
  269. const SuffixIconWidget({
  270. required this.executionState,
  271. required this.shouldSurfaceExecutionStates,
  272. super.key,
  273. });
  274. @override
  275. Widget build(BuildContext context) {
  276. final Widget trailingWidget;
  277. final colorScheme = getEnteColorScheme(context);
  278. if (executionState == ExecutionState.idle ||
  279. !shouldSurfaceExecutionStates) {
  280. trailingWidget = const SizedBox.shrink();
  281. } else if (executionState == ExecutionState.inProgress) {
  282. trailingWidget = EnteLoadingWidget(
  283. color: colorScheme.strokeMuted,
  284. );
  285. } else if (executionState == ExecutionState.successful) {
  286. trailingWidget = Icon(
  287. Icons.check_outlined,
  288. size: 22,
  289. color: colorScheme.primary500,
  290. );
  291. } else {
  292. trailingWidget = const SizedBox.shrink();
  293. }
  294. return trailingWidget;
  295. }
  296. }