Moved execution of onSubmit from ButtonWidget associated with TextInputWidget to within TextInputWidget

This commit is contained in:
ashilkn 2023-02-08 11:13:46 +05:30
parent b5c0ee7ce6
commit 5208ff3e13
5 changed files with 66 additions and 39 deletions

View file

@ -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<TextInputDialog> {
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<TextInputDialog> {
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<TextInputDialog> {
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<TextInputDialog> {
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<TextInputDialog> {
),
);
}
Future<void> _onTap() async {
await widget.onConfirm.call(_textController.text);
}
}

View file

@ -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<TextInputWidget> createState() => _TextInputWidgetState();
}
class _TextInputWidgetState extends State<TextInputWidget> {
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 = <Widget>[];
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),
),
),

View file

@ -114,9 +114,9 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
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;

View file

@ -256,12 +256,12 @@ Future<ButtonAction?> 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<ButtonAction?> showTextInputDialog(
label: label,
body: body,
icon: icon,
confirmationButtonLabel: confirmationButtonLabel,
onConfirm: onConfirm,
submitButtonLabel: submitButtonLabel,
onSubmit: onSubmit,
hintText: hintText,
prefixIcon: prefixIcon,
initialValue: initialValue,

View file

@ -103,13 +103,13 @@ Future<void> 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;