From 22ed22a2417c7a8d5b9528b05a7e44dc85875a64 Mon Sep 17 00:00:00 2001 From: onlytushar Date: Fri, 21 Nov 2025 22:10:57 +0530 Subject: [PATCH 1/2] FIx EnvironmentTriggerFieldState: Preserve cursor position when updating initial value and fix controller handling on key change --- .../common_widgets/env_trigger_field.dart | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/screens/common_widgets/env_trigger_field.dart b/lib/screens/common_widgets/env_trigger_field.dart index 3432135f0..9818aa994 100644 --- a/lib/screens/common_widgets/env_trigger_field.dart +++ b/lib/screens/common_widgets/env_trigger_field.dart @@ -67,13 +67,31 @@ class EnvironmentTriggerFieldState extends State { @override void didUpdateWidget(EnvironmentTriggerField oldWidget) { super.didUpdateWidget(oldWidget); - if ((oldWidget.keyId != widget.keyId) || - (oldWidget.initialValue != widget.initialValue)) { + if (oldWidget.keyId != widget.keyId) { + // Key changed - create new controller with cursor at end controller = widget.controller ?? TextEditingController.fromValue(TextEditingValue( text: widget.initialValue!, selection: TextSelection.collapsed( offset: widget.initialValue!.length))); + } else if (oldWidget.initialValue != widget.initialValue) { + // Initial value changed but key is same + // Preserve cursor position if text is being updated + if (widget.controller == null) { + final currentSelection = controller.selection; + final newText = widget.initialValue ?? ''; + + // Only update if the text actually differs from controller's current text + if (controller.text != newText) { + // Preserve cursor position, ensuring it's within bounds + final newOffset = + currentSelection.baseOffset.clamp(0, newText.length); + controller.value = TextEditingValue( + text: newText, + selection: TextSelection.collapsed(offset: newOffset), + ); + } + } } } @@ -119,17 +137,17 @@ class EnvironmentTriggerFieldState extends State { ], fieldViewBuilder: (context, textEditingController, focusnode) { return ExtendedTextField( - controller: textEditingController, - focusNode: focusnode, - decoration: widget.decoration, - style: widget.style, - onChanged: widget.onChanged, - onSubmitted: widget.onFieldSubmitted, - specialTextSpanBuilder: EnvRegExpSpanBuilder(), - onTapOutside: (event) { - _focusNode.unfocus(); - }, - readOnly: widget.readOnly, + controller: textEditingController, + focusNode: focusnode, + decoration: widget.decoration, + style: widget.style, + onChanged: widget.onChanged, + onSubmitted: widget.onFieldSubmitted, + specialTextSpanBuilder: EnvRegExpSpanBuilder(), + onTapOutside: (event) { + _focusNode.unfocus(); + }, + readOnly: widget.readOnly, obscureText: widget.obscureText ); From 933b401a2897483e8a4182c751217abfe01cbad6 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Thu, 4 Dec 2025 23:20:49 +0530 Subject: [PATCH 2/2] Update env_trigger_field.dart --- .../common_widgets/env_trigger_field.dart | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/screens/common_widgets/env_trigger_field.dart b/lib/screens/common_widgets/env_trigger_field.dart index 9818aa994..4d4a337d1 100644 --- a/lib/screens/common_widgets/env_trigger_field.dart +++ b/lib/screens/common_widgets/env_trigger_field.dart @@ -18,7 +18,7 @@ class EnvironmentTriggerField extends StatefulWidget { this.optionsWidthFactor, this.autocompleteNoTrigger, this.readOnly = false, - this.obscureText = false + this.obscureText = false, }) : assert( !(controller != null && initialValue != null), 'controller and initialValue cannot be simultaneously defined.', @@ -137,19 +137,18 @@ class EnvironmentTriggerFieldState extends State { ], fieldViewBuilder: (context, textEditingController, focusnode) { return ExtendedTextField( - controller: textEditingController, - focusNode: focusnode, - decoration: widget.decoration, - style: widget.style, - onChanged: widget.onChanged, - onSubmitted: widget.onFieldSubmitted, - specialTextSpanBuilder: EnvRegExpSpanBuilder(), - onTapOutside: (event) { - _focusNode.unfocus(); - }, - readOnly: widget.readOnly, - obscureText: widget.obscureText - + controller: textEditingController, + focusNode: focusnode, + decoration: widget.decoration, + style: widget.style, + onChanged: widget.onChanged, + onSubmitted: widget.onFieldSubmitted, + specialTextSpanBuilder: EnvRegExpSpanBuilder(), + onTapOutside: (event) { + _focusNode.unfocus(); + }, + readOnly: widget.readOnly, + obscureText: widget.obscureText, ); }, );