From 50bca8a76ab4f136693b51cddae9018886270f68 Mon Sep 17 00:00:00 2001 From: cp-ishita-g Date: Tue, 17 Dec 2024 15:42:47 +0530 Subject: [PATCH] fix on press backspace cursor not moving to previous field --- .../ui/flow/space/join/join_space_screen.dart | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/app/lib/ui/flow/space/join/join_space_screen.dart b/app/lib/ui/flow/space/join/join_space_screen.dart index b14cb6b..b24d0b8 100644 --- a/app/lib/ui/flow/space/join/join_space_screen.dart +++ b/app/lib/ui/flow/space/join/join_space_screen.dart @@ -33,8 +33,37 @@ class _JoinSpaceState extends ConsumerState { @override void initState() { super.initState(); - _controllers = List.generate(6, (index) => TextEditingController()); + _controllers = List.generate(6, (index) => TextEditingController(text: '\u200b')); _focusNodes = List.generate(6, (index) => FocusNode()); + + for (int i = 0; i < _controllers.length; i++) { + _controllers[i].addListener(() { + _handleTextChange(i); + }); + } + } + + void _handleTextChange(int index) { + final text = _controllers[index].text; + + if (text.isEmpty) { + _controllers[index].text = '\u200b'; + _controllers[index].selection = const TextSelection.collapsed(offset: 1); + + if (index > 0) { + _focusNodes[index - 1].requestFocus(); + } + } else if (text.length > 1) { + String newText = text.replaceAll('\u200b', '').toUpperCase(); + _controllers[index].text = newText; + _controllers[index].selection = TextSelection.collapsed(offset: newText.length); + + // Move focus to the next field + if (index < 5) { + _focusNodes[index + 1].requestFocus(); + } + _updateJoinSpaceButtonState(); + } } @override @@ -150,7 +179,7 @@ class _JoinSpaceState extends ConsumerState { controller: _controllers[index], focusNode: _focusNodes[index], textAlign: TextAlign.center, - maxLength: 1, + maxLength: 2, decoration: const InputDecoration( border: InputBorder.none, counterText: '', @@ -170,17 +199,6 @@ class _JoinSpaceState extends ConsumerState { selection: TextSelection.collapsed(offset: upperCaseText.length), ); } - - if (text.isEmpty) { - if (index > 0) _focusNodes[index - 1].requestFocus(); - } else { - if (index < 5) { - _focusNodes[index + 1].requestFocus(); - } else { - FocusManager.instance.primaryFocus?.unfocus(); - } - } - _updateJoinSpaceButtonState(); }, ), ), @@ -238,8 +256,10 @@ class _JoinSpaceState extends ConsumerState { void _updateJoinSpaceButtonState() { setState(() { - enabled = - _controllers.every((controller) => controller.text.trim().isNotEmpty); + enabled = _controllers.every((controller) { + final text = controller.text; + return text.length == 1 && text != '\u200b'; + }); }); }