Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions lib/screens/common_widgets/env_trigger_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,31 @@ class EnvironmentTriggerFieldState extends State<EnvironmentTriggerField> {
@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),
);
}
}
}
}

Expand Down Expand Up @@ -119,17 +137,17 @@ class EnvironmentTriggerFieldState extends State<EnvironmentTriggerField> {
],
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

);
Expand Down