Skip to content

Commit bf2e8eb

Browse files
feat: add autofocus parameter (#134)
1 parent 8273b58 commit bf2e8eb

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/src/views/llm_chat_view/llm_chat_view.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class LlmChatView extends StatefulWidget {
8989
this.errorMessage = 'ERROR',
9090
this.enableAttachments = true,
9191
this.enableVoiceNotes = true,
92+
this.autofocus,
9293
super.key,
9394
}) : viewModel = ChatViewModel(
9495
provider: provider,
@@ -141,6 +142,13 @@ class LlmChatView extends StatefulWidget {
141142
/// Defaults to 'ERROR'.
142143
final String errorMessage;
143144

145+
/// Whether to autofocus the chat input field when the view is displayed.
146+
///
147+
/// Defaults to `null`, which means it will be determined based on the
148+
/// presence of suggestions. If there are no suggestions, the input field
149+
/// will be focused automatically.
150+
final bool? autofocus;
151+
144152
@override
145153
State<LlmChatView> createState() => _LlmChatViewState();
146154
}
@@ -204,7 +212,9 @@ class _LlmChatViewState extends State<LlmChatView>
204212
),
205213
ChatInput(
206214
initialMessage: _initialMessage,
207-
autofocus: widget.viewModel.suggestions.isEmpty,
215+
autofocus:
216+
widget.autofocus ??
217+
widget.viewModel.suggestions.isEmpty,
208218
onCancelEdit:
209219
_associatedResponse != null ? _onCancelEdit : null,
210220
onSendMessage: _onSendMessage,

test/suggestions_test.dart

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:flutter_test/flutter_test.dart';
66

77
void main() {
88
group('Suggestions - Welcome message overlap tests', () {
9-
Widget materialApp(int suggestionsCount) => MaterialApp(
9+
Widget materialApp(int suggestionsCount, {bool? autofocus}) => MaterialApp(
1010
home: Scaffold(
1111
appBar: AppBar(title: const Text('Title')),
1212
body: LlmChatView(
@@ -17,6 +17,7 @@ void main() {
1717
(index) => 'Suggestion sample ${index + 1}',
1818
),
1919
provider: EchoProvider(),
20+
autofocus: autofocus,
2021
),
2122
),
2223
);
@@ -69,6 +70,32 @@ void main() {
6970
// TextField must be focused now
7071
expect((tester.widget<TextField>(textField)).focusNode?.hasFocus, true);
7172
});
73+
testWidgets('force autofocus false even if no suggestions provided', (
74+
tester,
75+
) async {
76+
// No suggestions provided, but autofocus is set to false
77+
await tester.pumpWidget(materialApp(0, autofocus: false));
78+
79+
// ChatTextField must be autofocus false and TextField must not be focused
80+
// because parameter is set to false
81+
final chatTextField = find.byType(ChatTextField);
82+
final textField = find.byType(TextField);
83+
expect((tester.widget<ChatTextField>(chatTextField)).autofocus, false);
84+
expect((tester.widget<TextField>(textField)).focusNode?.hasFocus, false);
85+
});
86+
testWidgets('force autofocus true even if suggestions provided', (
87+
tester,
88+
) async {
89+
// Suggestions provided, but autofocus is set to true
90+
await tester.pumpWidget(materialApp(40, autofocus: true));
91+
92+
// ChatTextField must be autofocus true and TextField must be focused
93+
// because parameter is set to true
94+
final chatTextField = find.byType(ChatTextField);
95+
final textField = find.byType(TextField);
96+
expect((tester.widget<ChatTextField>(chatTextField)).autofocus, true);
97+
expect((tester.widget<TextField>(textField)).focusNode?.hasFocus, true);
98+
});
7299
testWidgets('Welcome message with a lot of suggestions allowing scroll', (
73100
tester,
74101
) async {

0 commit comments

Comments
 (0)