From 5208ff3e13b0ac07a57e47442e34b63584330fee Mon Sep 17 00:00:00 2001 From: ashilkn Date: Wed, 8 Feb 2023 11:13:46 +0530 Subject: [PATCH] Moved execution of onSubmit from ButtonWidget associated with TextInputWidget to within TextInputWidget --- lib/ui/components/dialog_widget.dart | 26 ++++---- lib/ui/components/text_input_widget.dart | 63 ++++++++++++++----- .../gallery/gallery_app_bar_widget.dart | 4 +- lib/utils/dialog_util.dart | 8 +-- lib/utils/magic_util.dart | 4 +- 5 files changed, 66 insertions(+), 39 deletions(-) diff --git a/lib/ui/components/dialog_widget.dart b/lib/ui/components/dialog_widget.dart index 4b5601bb4..af7c4370e 100644 --- a/lib/ui/components/dialog_widget.dart +++ b/lib/ui/components/dialog_widget.dart @@ -159,11 +159,11 @@ class Actions extends StatelessWidget { class TextInputDialog extends StatefulWidget { final String title; final String? body; - final String confirmationButtonLabel; + final String submitButtonLabel; final IconData? icon; final String? label; final String? message; - final FutureVoidCallbackParamStr onConfirm; + final FutureVoidCallbackParamStr onSubmit; final String? hintText; final IconData? prefixIcon; final String? initialValue; @@ -172,8 +172,8 @@ class TextInputDialog extends StatefulWidget { const TextInputDialog({ required this.title, this.body, - required this.confirmationButtonLabel, - required this.onConfirm, + required this.submitButtonLabel, + required this.onSubmit, this.icon, this.label, this.message, @@ -190,7 +190,8 @@ class TextInputDialog extends StatefulWidget { } class _TextInputDialogState extends State { - final TextEditingController _textController = TextEditingController(); + //the value of this ValueNotifier has no significance + final _submitNotifier = ValueNotifier(false); @override Widget build(BuildContext context) { final widthOfScreen = MediaQuery.of(context).size.width; @@ -219,7 +220,6 @@ class _TextInputDialogState extends State { Padding( padding: const EdgeInsets.only(top: 19), child: TextInputWidget( - textController: _textController, label: widget.label, message: widget.message, hintText: widget.hintText, @@ -228,6 +228,8 @@ class _TextInputDialogState extends State { alignMessage: widget.alignMessage, autoFocus: true, maxLength: widget.maxLength, + submitNotifier: _submitNotifier, + onSubmit: widget.onSubmit, ), ), const SizedBox(height: 36), @@ -247,10 +249,10 @@ class _TextInputDialogState extends State { child: ButtonWidget( buttonSize: ButtonSize.small, buttonType: ButtonType.neutral, - labelText: widget.confirmationButtonLabel, - onTap: _onTap, - isInAlert: true, - shouldShowSuccessConfirmation: true, + labelText: widget.submitButtonLabel, + onTap: () async { + _submitNotifier.value = !_submitNotifier.value; + }, ), ), ], @@ -260,8 +262,4 @@ class _TextInputDialogState extends State { ), ); } - - Future _onTap() async { - await widget.onConfirm.call(_textController.text); - } } diff --git a/lib/ui/components/text_input_widget.dart b/lib/ui/components/text_input_widget.dart index d8b7c19f1..d334e5010 100644 --- a/lib/ui/components/text_input_widget.dart +++ b/lib/ui/components/text_input_widget.dart @@ -1,10 +1,10 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:photos/theme/ente_theme.dart'; +import 'package:photos/ui/components/dialog_widget.dart'; import 'package:photos/utils/separators_util.dart'; -class TextInputWidget extends StatelessWidget { - final TextEditingController textController; +class TextInputWidget extends StatefulWidget { final String? label; final String? message; final String? hintText; @@ -13,8 +13,12 @@ class TextInputWidget extends StatelessWidget { final Alignment? alignMessage; final bool? autoFocus; final int? maxLength; + final ValueNotifier? submitNotifier; + final bool alwaysShowSuccessState; + final bool showOnlyLoadingState; + final FutureVoidCallbackParamStr onSubmit; const TextInputWidget({ - required this.textController, + required this.onSubmit, this.label, this.message, this.hintText, @@ -23,33 +27,57 @@ class TextInputWidget extends StatelessWidget { this.alignMessage, this.autoFocus, this.maxLength, + this.submitNotifier, + this.alwaysShowSuccessState = false, + this.showOnlyLoadingState = false, super.key, }); + @override + State createState() => _TextInputWidgetState(); +} + +class _TextInputWidgetState extends State { + final _textController = TextEditingController(); + + @override + void initState() { + widget.submitNotifier?.addListener(() { + widget.onSubmit.call(_textController.text); + }); + super.initState(); + } + + @override + void dispose() { + widget.submitNotifier?.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { - if (initialValue != null) { - textController.value = TextEditingValue( - text: initialValue!, - selection: TextSelection.collapsed(offset: initialValue!.length), + if (widget.initialValue != null) { + _textController.value = TextEditingValue( + text: widget.initialValue!, + selection: TextSelection.collapsed(offset: widget.initialValue!.length), ); } final colorScheme = getEnteColorScheme(context); final textTheme = getEnteTextTheme(context); var textInputChildren = []; - if (label != null) textInputChildren.add(Text(label!)); + if (widget.label != null) textInputChildren.add(Text(widget.label!)); textInputChildren.add( ClipRRect( borderRadius: const BorderRadius.all(Radius.circular(8)), child: Material( child: TextFormField( - autofocus: autoFocus ?? false, - controller: textController, - inputFormatters: maxLength != null + autofocus: widget.autoFocus ?? false, + controller: _textController, + inputFormatters: widget.maxLength != null ? [LengthLimitingTextInputFormatter(50)] : null, decoration: InputDecoration( - hintText: hintText, + hintText: widget.hintText, hintStyle: textTheme.body.copyWith(color: colorScheme.textMuted), filled: true, contentPadding: const EdgeInsets.symmetric( @@ -75,25 +103,26 @@ class TextInputWidget extends StatelessWidget { minHeight: 44, minWidth: 44, ), - prefixIcon: prefixIcon != null + prefixIcon: widget.prefixIcon != null ? Icon( - prefixIcon, + widget.prefixIcon, color: colorScheme.strokeMuted, ) : null, ), + onEditingComplete: () {}, ), ), ), ); - if (message != null) { + if (widget.message != null) { textInputChildren.add( Padding( padding: const EdgeInsets.symmetric(horizontal: 8), child: Align( - alignment: alignMessage ?? Alignment.centerLeft, + alignment: widget.alignMessage ?? Alignment.centerLeft, child: Text( - message!, + widget.message!, style: textTheme.small.copyWith(color: colorScheme.textMuted), ), ), diff --git a/lib/ui/viewer/gallery/gallery_app_bar_widget.dart b/lib/ui/viewer/gallery/gallery_app_bar_widget.dart index 1f214c0e4..f55336303 100644 --- a/lib/ui/viewer/gallery/gallery_app_bar_widget.dart +++ b/lib/ui/viewer/gallery/gallery_app_bar_widget.dart @@ -114,9 +114,9 @@ class _GalleryAppBarWidgetState extends State { final result = await showTextInputDialog( context, title: "Rename album", - confirmationButtonLabel: "Rename", + submitButtonLabel: "Rename", hintText: "Enter album name", - onConfirm: (String text) async { + onSubmit: (String text) async { // indicates user cancelled the rename request if (text == "" || text.trim() == _appBarTitle!.trim()) { return; diff --git a/lib/utils/dialog_util.dart b/lib/utils/dialog_util.dart index d52dcdb55..da8ec5069 100644 --- a/lib/utils/dialog_util.dart +++ b/lib/utils/dialog_util.dart @@ -256,12 +256,12 @@ Future showTextInputDialog( BuildContext context, { required String title, String? body, - required String confirmationButtonLabel, + required String submitButtonLabel, IconData? icon, String? label, String? message, String? hintText, - required FutureVoidCallbackParamStr onConfirm, + required FutureVoidCallbackParamStr onSubmit, IconData? prefixIcon, String? initialValue, Alignment? alignMessage, @@ -282,8 +282,8 @@ Future showTextInputDialog( label: label, body: body, icon: icon, - confirmationButtonLabel: confirmationButtonLabel, - onConfirm: onConfirm, + submitButtonLabel: submitButtonLabel, + onSubmit: onSubmit, hintText: hintText, prefixIcon: prefixIcon, initialValue: initialValue, diff --git a/lib/utils/magic_util.dart b/lib/utils/magic_util.dart index 94be4fa2c..558182df6 100644 --- a/lib/utils/magic_util.dart +++ b/lib/utils/magic_util.dart @@ -103,13 +103,13 @@ Future editFilename( await showTextInputDialog( context, title: "Rename file", - confirmationButtonLabel: "Rename", + submitButtonLabel: "Rename", initialValue: nameWithoutExt, message: extName, alignMessage: Alignment.centerRight, hintText: "Enter file name", maxLength: 50, - onConfirm: (String text) async { + onSubmit: (String text) async { try { if (text.isEmpty || text.trim() == nameWithoutExt.trim()) { return;