Question answers: no shaking while typing; Enter sends, Shift+Enter breaks

Typing in a question card's answer box asked the transcript to reveal
the caret on every keystroke; the split, bottom-following transcript
jumped and followed back, so the conversation shook. The request now
stops at the card, which is on screen while the operator types.

A free-text answer now behaves like the chat composer: it grows to six
lines, Enter sends it (desktop and web keyboards) and Shift+Enter
breaks the line; an open IME candidate keeps its Enter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 10:30:30 +00:00
parent a59b80fb48
commit 33874005c4
5 changed files with 156 additions and 2 deletions
@@ -0,0 +1,27 @@
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
/// Keeps a text field inside the transcript from scrolling the transcript to
/// "reveal" its caret on every keystroke.
///
/// An editable field asks every scrollable above it to show its caret after
/// each edit. The transcript's split, bottom-following viewport answers that
/// with a jump and then follows its newest row back, so a question card's
/// answer box made the whole conversation shake while typing. The card is on
/// screen while the operator types in it, so the request stops here.
class ChatNoReveal extends SingleChildRenderObjectWidget {
const ChatNoReveal({super.key, super.child});
@override
RenderObject createRenderObject(BuildContext context) => _RenderNoReveal();
}
class _RenderNoReveal extends RenderProxyBox {
@override
void showOnScreen({
RenderObject? descendant,
Rect? rect,
Duration duration = Duration.zero,
Curve curve = Curves.ease,
}) {}
}
@@ -173,6 +173,7 @@ class _ChatQuestionCardState extends State<ChatQuestionCard> {
answers: _answers,
controllers: _controllers,
onChanged: _changed,
onSubmit: _submit,
)
else
...chatQuestionRecordedAnswers(context: context, form: widget.form),
@@ -1,8 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../../core/api_gateway.dart';
import 'chat_card_styles.dart';
import 'chat_question_card_model.dart';
import 'chat_composer_bar.dart' show chatEnterSends;
import 'chat_no_reveal.dart';
import 'chat_question_option_controls.dart';
/// A free-text input is part of a field when the form allows an answer outside
@@ -18,6 +21,7 @@ List<Widget> chatQuestionFieldWidgets({
required Map<String, dynamic> answers,
required Map<String, TextEditingController> controllers,
required void Function(String key, Object? value) onChanged,
VoidCallback? onSubmit,
}) {
final widgets = <Widget>[];
for (final field in fields) {
@@ -32,6 +36,7 @@ List<Widget> chatQuestionFieldWidgets({
answers: answers,
controllers: controllers,
onChanged: onChanged,
onSubmit: onSubmit,
),
);
}
@@ -45,6 +50,7 @@ Widget _field({
required Map<String, dynamic> answers,
required Map<String, TextEditingController> controllers,
required void Function(String key, Object? value) onChanged,
VoidCallback? onSubmit,
}) {
final title = '${field['title'] ?? ''}'.trim();
final description = '${field['description'] ?? ''}'.trim();
@@ -86,6 +92,7 @@ Widget _field({
keyName: keyName,
controller: controllers[keyName]!,
hasOptions: chatQuestionOptions(field).isNotEmpty,
onSubmit: onSubmit,
onChanged: (value) => onChanged(
keyName,
chatQuestionTextValue(field, value, answers),
@@ -105,6 +112,7 @@ Widget _input({
required TextEditingController controller,
required bool hasOptions,
required ValueChanged<String> onChanged,
VoidCallback? onSubmit,
}) {
final placeholder = '${field['placeholder'] ?? ''}'.trim().isNotEmpty
? '${field['placeholder']}'.trim()
@@ -123,15 +131,36 @@ Widget _input({
: TextInputType.text;
// The visual label sits above the input; the same words must also reach the
// input's own semantics node, as the web's `aria-label` does.
// A free-text answer behaves like the chat composer: it grows to a few
// lines, Enter sends (on a desktop or web keyboard) and Shift+Enter breaks
// the line. A secret or typed (number, email, ...) answer stays one line.
final multiline = keyboard == TextInputType.text && !secret;
final input = TextField(
key: ValueKey<String>('chat-question-input-$keyName'),
controller: controller,
obscureText: secret,
keyboardType: keyboard,
keyboardType: multiline ? TextInputType.multiline : keyboard,
minLines: 1,
maxLines: multiline ? 6 : 1,
textInputAction: multiline && !chatEnterSends()
? TextInputAction.newline
: TextInputAction.done,
decoration: InputDecoration(hintText: placeholder),
onChanged: onChanged,
onSubmitted: onSubmit == null ? null : (_) => onSubmit(),
);
return Semantics(
label: label,
child: ChatNoReveal(
child: Focus(
canRequestFocus: false,
skipTraversal: true,
onKeyEvent: (_, event) =>
_enterKey(event, controller, multiline, onChanged, onSubmit),
child: input,
),
),
);
return Semantics(label: label, child: input);
}
Widget _external(BuildContext context, JsonMap field) {
@@ -146,3 +175,49 @@ Widget _external(BuildContext context, JsonMap field) {
}
return SelectableText(raw);
}
/// Enter sends the answer and Shift+Enter breaks the line, as in the chat
/// composer. An open IME candidate keeps its Enter, and a phone keyboard's
/// Return stays the field's own newline.
KeyEventResult _enterKey(
KeyEvent event,
TextEditingController controller,
bool multiline,
ValueChanged<String> onChanged,
VoidCallback? onSubmit,
) {
final key = event.logicalKey;
if (key != LogicalKeyboardKey.enter &&
key != LogicalKeyboardKey.numpadEnter) {
return KeyEventResult.ignored;
}
final value = controller.value;
if (value.composing.isValid && !value.composing.isCollapsed) {
return KeyEventResult.ignored;
}
if (!chatEnterSends()) return KeyEventResult.ignored;
final keyboard = HardwareKeyboard.instance;
if (keyboard.isShiftPressed) {
if (!multiline) return KeyEventResult.handled;
if (event is KeyDownEvent || event is KeyRepeatEvent) {
final selection = value.selection;
final start = selection.isValid ? selection.start : value.text.length;
final end = selection.isValid ? selection.end : start;
final text = value.text.replaceRange(start, end, '\n');
controller.value = TextEditingValue(
text: text,
selection: TextSelection.collapsed(offset: start + 1),
);
onChanged(text);
}
return KeyEventResult.handled;
}
if (keyboard.isAltPressed ||
keyboard.isControlPressed ||
keyboard.isMetaPressed) {
return KeyEventResult.ignored;
}
// A held key must not send twice.
if (event is KeyDownEvent) onSubmit?.call();
return KeyEventResult.handled;
}
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_question_card.dart';
@@ -297,6 +298,53 @@ void main() {
},
);
testWidgets(
'Enter sends a typed answer; Shift+Enter breaks the line',
(tester) async {
final sent = <Map<String, dynamic>>[];
await tester.pumpWidget(
scrollHost(
ChatQuestionCard(
form: {
'id': 'q',
'kind': 'question',
'actions': ['accept', 'decline'],
'fields': [
{
'key': 'q0',
'title': 'Which one?',
'type': 'string',
'required': true,
'custom': true,
'options': [
{'value': 'A', 'label': 'A'},
],
},
],
},
onSubmit: (answers, action) async => sent.add(answers),
),
),
);
final box = find.byKey(const ValueKey<String>('chat-question-input-q0'));
await tester.tap(box);
await tester.enterText(box, 'first line');
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.pump();
expect(tester.widget<TextField>(box).controller!.text, 'first line\n');
expect(sent, isEmpty);
await tester.enterText(box, 'first line\nsecond');
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(sent, [
{'q0': 'first line\nsecond'},
]);
},
variant: TargetPlatformVariant.only(TargetPlatform.linux),
);
testWidgets('dismissing a question sends the reply to the composer', (
tester,
) async {
+3
View File
@@ -37,12 +37,15 @@ void chatQuestionStream(String sessionId, void Function(JsonMap frame) emit) {
'session_id': sessionId,
'kind': 'question',
'title': 'Database',
// Claude questions take a custom answer and can be dismissed.
if (_mode == 'claude') 'actions': ['accept', 'decline'],
'fields': [
{
'key': 'q0',
'title': 'Which database should the release use?',
'type': 'string',
'required': true,
if (_mode == 'claude') 'custom': true,
'options': [
{'value': 'Postgres', 'label': 'Postgres'},
{'value': 'SQLite', 'label': 'SQLite'},