text_input_widget.dart 9.9 KB

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