Skip to content
Open
Show file tree
Hide file tree
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
70 changes: 45 additions & 25 deletions lib/screens/common_widgets/env_trigger_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import 'env_regexp_span_builder.dart';
import 'env_trigger_options.dart';

class EnvironmentTriggerField extends StatefulWidget {
const EnvironmentTriggerField({
super.key,
required this.keyId,
this.initialValue,
this.controller,
this.focusNode,
this.onChanged,
this.onFieldSubmitted,
this.style,
this.decoration,
this.optionsWidthFactor,
this.autocompleteNoTrigger,
this.readOnly = false,
this.obscureText = false
}) : assert(
const EnvironmentTriggerField(
{super.key,
required this.keyId,
this.initialValue,
this.controller,
this.focusNode,
this.onChanged,
this.onFieldSubmitted,
this.style,
this.decoration,
this.optionsWidthFactor,
this.autocompleteNoTrigger,
this.readOnly = false,
this.obscureText = false,
}) : assert(
!(controller != null && initialValue != null),
'controller and initialValue cannot be simultaneously defined.',
);
Expand Down Expand Up @@ -49,11 +49,11 @@ class EnvironmentTriggerFieldState extends State<EnvironmentTriggerField> {
@override
void initState() {
super.initState();
final initialText = widget.initialValue ?? '';
controller = widget.controller ??
TextEditingController.fromValue(TextEditingValue(
text: widget.initialValue!,
selection:
TextSelection.collapsed(offset: widget.initialValue!.length)));
text: initialText,
selection: TextSelection.collapsed(offset: initialText.length)));
_focusNode = widget.focusNode ?? FocusNode();
}

Expand All @@ -67,13 +67,34 @@ 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) {
if (oldWidget.controller == null) {
controller.dispose();
}
final initialText = widget.initialValue ?? '';
controller = widget.controller ??
TextEditingController.fromValue(TextEditingValue(
text: widget.initialValue!,
selection: TextSelection.collapsed(
offset: widget.initialValue!.length)));
text: initialText,
selection: TextSelection.collapsed(offset: initialText.length)));
} else if (oldWidget.initialValue != widget.initialValue) {
if (widget.controller == null) {
final newText = widget.initialValue ?? '';
if (controller.text != newText) {
final currentSelection = controller.selection;
final newBaseOffset =
currentSelection.baseOffset.clamp(0, newText.length);
final newExtentOffset =
currentSelection.extentOffset.clamp(0, newText.length);
controller.value = TextEditingValue(
text: newText,
selection: TextSelection(
baseOffset: newBaseOffset,
extentOffset: newExtentOffset,
),
);
}
}
}
}

Expand Down Expand Up @@ -130,8 +151,7 @@ class EnvironmentTriggerFieldState extends State<EnvironmentTriggerField> {
_focusNode.unfocus();
},
readOnly: widget.readOnly,
obscureText: widget.obscureText

obscureText: widget.obscureText,
);
},
);
Expand Down
137 changes: 137 additions & 0 deletions test/screens/common_widgets/env_trigger_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,141 @@ void main() {

expect(fieldKey.currentState!.controller.text, emptyValue);
});

testWidgets(
'Testing EnvironmentTriggerField preserves cursor position on text update',
(WidgetTester tester) async {
final fieldKey = GlobalKey<EnvironmentTriggerFieldState>();
const initialValue = 'hello world';
const updatedValue = 'hello world!';

await tester.pumpWidget(
Portal(
child: MaterialApp(
home: Scaffold(
body: EnvironmentTriggerField(
key: fieldKey,
keyId: 'testKey',
initialValue: initialValue,
),
),
),
),
);

// Set cursor to position 5 (middle of text)
fieldKey.currentState!.controller.selection =
TextSelection.collapsed(offset: 5);

// Update the text value
await tester.pumpWidget(
Portal(
child: MaterialApp(
home: Scaffold(
body: EnvironmentTriggerField(
key: fieldKey,
keyId: 'testKey',
initialValue: updatedValue,
),
),
),
),
);

// Verify cursor is still at position 5
expect(fieldKey.currentState!.controller.selection.baseOffset, 5);
expect(fieldKey.currentState!.controller.text, updatedValue);
});

testWidgets(
'Testing EnvironmentTriggerField preserves text selection on text update',
(WidgetTester tester) async {
final fieldKey = GlobalKey<EnvironmentTriggerFieldState>();
const initialValue = 'hello world';
const updatedValue = 'hello world!';

await tester.pumpWidget(
Portal(
child: MaterialApp(
home: Scaffold(
body: EnvironmentTriggerField(
key: fieldKey,
keyId: 'testKey',
initialValue: initialValue,
),
),
),
),
);

// Set text selection from position 0 to 5 (highlighted text)
fieldKey.currentState!.controller.selection =
TextSelection(baseOffset: 0, extentOffset: 5);

// Update the text value
await tester.pumpWidget(
Portal(
child: MaterialApp(
home: Scaffold(
body: EnvironmentTriggerField(
key: fieldKey,
keyId: 'testKey',
initialValue: updatedValue,
),
),
),
),
);

// Verify selection is preserved (both base and extent offsets)
expect(fieldKey.currentState!.controller.selection.baseOffset, 0);
expect(fieldKey.currentState!.controller.selection.extentOffset, 5);
expect(fieldKey.currentState!.controller.text, updatedValue);
});

testWidgets(
'Testing EnvironmentTriggerField resets cursor when keyId changes',
(WidgetTester tester) async {
final fieldKey = GlobalKey<EnvironmentTriggerFieldState>();
const initialValue = 'hello world';
const newValue = 'new request';

await tester.pumpWidget(
Portal(
child: MaterialApp(
home: Scaffold(
body: EnvironmentTriggerField(
key: fieldKey,
keyId: 'testKey1',
initialValue: initialValue,
),
),
),
),
);

// Set cursor to position 5
fieldKey.currentState!.controller.selection =
TextSelection.collapsed(offset: 5);

// Change keyId (different request selected)
await tester.pumpWidget(
Portal(
child: MaterialApp(
home: Scaffold(
body: EnvironmentTriggerField(
key: fieldKey,
keyId: 'testKey2',
initialValue: newValue,
),
),
),
),
);

// Verify cursor is reset to end of new text (different request)
expect(fieldKey.currentState!.controller.selection.baseOffset,
newValue.length);
expect(fieldKey.currentState!.controller.text, newValue);
});
}