-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add TaskChangeNameDialog.dart to allow task name change
- Loading branch information
Showing
3 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
lib/screens/locations_overview_screen_widgets/TaskChangeNameDialog.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import 'package:basic_utils/basic_utils.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'; | ||
import 'package:locus/utils/theme.dart'; | ||
|
||
class TaskChangeNameDialog extends StatefulWidget { | ||
final String initialName; | ||
final Function(String) onNameChanged; | ||
|
||
const TaskChangeNameDialog({ | ||
required this.initialName, | ||
required this.onNameChanged, | ||
super.key, | ||
}); | ||
|
||
@override | ||
State<TaskChangeNameDialog> createState() => _TaskChangeNameDialogState(); | ||
} | ||
|
||
class _TaskChangeNameDialogState extends State<TaskChangeNameDialog> { | ||
final formKey = GlobalKey<FormState>(); | ||
final nameController = TextEditingController(); | ||
final nameFocusNode = FocusNode(); | ||
|
||
late bool showClearButton; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
|
||
nameController.text = widget.initialName; | ||
nameController.addListener(() { | ||
setState(() { | ||
showClearButton = nameController.text.isNotEmpty; | ||
}); | ||
}); | ||
showClearButton = nameController.text.isNotEmpty; | ||
} | ||
|
||
@override | ||
void dispose() { | ||
nameController.dispose(); | ||
|
||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final l10n = AppLocalizations.of(context); | ||
|
||
return PlatformAlertDialog( | ||
material: (_, __) => MaterialAlertDialogData( | ||
icon: const Icon(Icons.edit_rounded), | ||
), | ||
title: Text(l10n.taskAction_changeName), | ||
actions: createCancellableDialogActions( | ||
context, | ||
[ | ||
PlatformDialogAction( | ||
child: Text(l10n.closeSaveAction), | ||
cupertino: (_, __) => CupertinoDialogActionData( | ||
isDefaultAction: true, | ||
), | ||
material: (_, __) => MaterialDialogActionData( | ||
icon: const Icon(Icons.check_rounded), | ||
), | ||
onPressed: () { | ||
if (formKey.currentState!.validate()) { | ||
widget.onNameChanged(nameController.text); | ||
} | ||
}, | ||
) | ||
], | ||
), | ||
content: Form( | ||
key: formKey, | ||
child: PlatformTextFormField( | ||
autofillHints: const [AutofillHints.name], | ||
controller: nameController, | ||
autofocus: true, | ||
focusNode: nameFocusNode, | ||
validator: (value) { | ||
if (value == null || value.isEmpty) { | ||
return l10n.fields_errors_isEmpty; | ||
} | ||
|
||
if (!StringUtils.isAscii(value)) { | ||
return l10n.fields_errors_invalidCharacters; | ||
} | ||
|
||
return null; | ||
}, | ||
textInputAction: TextInputAction.done, | ||
keyboardType: TextInputType.name, | ||
maxLines: 1, | ||
material: (_, __) => MaterialTextFormFieldData( | ||
decoration: InputDecoration( | ||
filled: false, | ||
labelText: l10n.taskAction_changeName_field_label, | ||
suffixIcon: showClearButton | ||
? IconButton( | ||
visualDensity: VisualDensity.compact, | ||
icon: const Icon(Icons.clear), | ||
onPressed: () { | ||
nameController.clear(); | ||
nameFocusNode.requestFocus(); | ||
}, | ||
) | ||
: null, | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters