Chat composer: a newline at the line limit scrolls to the new line
At the field's line limit, a newline at the end left the caret on an empty line the field only scrolled to after the next character. The field now scrolls to the end right after such an edit.
This commit is contained in:
@@ -138,6 +138,10 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
text: widget.composer.draft,
|
||||
);
|
||||
final FocusNode _focus = FocusNode(debugLabel: 'chat-composer');
|
||||
|
||||
/// The field's own scroll position, so a new last line is brought into
|
||||
/// view as soon as it exists (see [_revealCaretLine]).
|
||||
final ScrollController _fieldScroll = ScrollController();
|
||||
late final ComposerFocusRestorer _focusRestorer;
|
||||
final ChatAttachmentDraft _attachments = ChatAttachmentDraft();
|
||||
|
||||
@@ -284,6 +288,22 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
if (_selectedCommand >= suggestions.length) _selectedCommand = 0;
|
||||
});
|
||||
unawaited(widget.composer.setDraft(value));
|
||||
_revealCaretLine(value);
|
||||
}
|
||||
|
||||
/// At the field's line limit, a newline at the end leaves the caret on a
|
||||
/// new empty line the field only scrolls to once it has a character.
|
||||
/// Scroll there right away whenever the caret is at the end.
|
||||
void _revealCaretLine(String value) {
|
||||
final caret = _text.selection;
|
||||
if (!caret.isCollapsed || caret.baseOffset != value.length) return;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted || !_fieldScroll.hasClients) return;
|
||||
final position = _fieldScroll.position;
|
||||
if (position.pixels < position.maxScrollExtent) {
|
||||
_fieldScroll.jumpTo(position.maxScrollExtent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
KeyEventResult _onKey(FocusNode node, KeyEvent event) {
|
||||
@@ -620,6 +640,7 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
_focusRestorer.dispose();
|
||||
_text.dispose();
|
||||
_focus.dispose();
|
||||
_fieldScroll.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -1039,6 +1060,7 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
key: const Key('chat-composer-field'),
|
||||
controller: _text,
|
||||
focusNode: _focus,
|
||||
scrollController: _fieldScroll,
|
||||
placeholder: widget.placeholder,
|
||||
// The pill draws the field's surface; the field adds no border.
|
||||
decoration: null,
|
||||
@@ -1065,6 +1087,7 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
key: const Key('chat-composer-field'),
|
||||
controller: _text,
|
||||
focusNode: _focus,
|
||||
scrollController: _fieldScroll,
|
||||
minLines: 1,
|
||||
maxLines: maxLines,
|
||||
keyboardType: TextInputType.multiline,
|
||||
|
||||
@@ -40,4 +40,31 @@ void main() {
|
||||
expect(phone, greaterThan(6));
|
||||
});
|
||||
}
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
final host = cupertino ? 'Cupertino' : 'Material';
|
||||
testWidgets('$host: a newline at the line limit scrolls to the new '
|
||||
'empty line at once', (tester) async {
|
||||
final gateway = ComposerBarGateway()..page = fixtureTurn();
|
||||
addTearDown(gateway.close);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
tester.view.physicalSize = const Size(900, 1000);
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(composerThread(gateway, cupertino: cupertino));
|
||||
await tester.pump();
|
||||
final field = find.byKey(const Key('chat-composer-field'));
|
||||
// Past the 12-line limit, then a newline at the end: no character
|
||||
// typed on the new line yet.
|
||||
final text = List.generate(14, (i) => 'line $i').join('\n');
|
||||
await tester.enterText(field, '$text\n');
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
final scrollable = tester.state<ScrollableState>(
|
||||
find.descendant(of: field, matching: find.byType(Scrollable)).first,
|
||||
);
|
||||
final position = scrollable.position;
|
||||
expect(position.maxScrollExtent, greaterThan(0));
|
||||
expect(position.pixels, moreOrLessEquals(position.maxScrollExtent));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user