Chat composer: grow to 12 lines, at most ~40% of the visible height

The message field grows up to 12 lines (was 6) before it scrolls, capped at
about 40% of the visible height with the keyboard excluded, so the
conversation stays in view on a phone.
This commit is contained in:
2026-11-03 10:37:39 +00:00
parent 6413fc5a90
commit 73646a038d
2 changed files with 64 additions and 3 deletions
@@ -1004,13 +1004,31 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
return lines.clamp(1, 3);
}
/// The most lines the field grows to before it scrolls: up to
/// [_composerMaxLines], but never more than about 40% of the visible
/// height (the keyboard excluded), so the conversation stays in view.
static const int _composerMaxLines = 12;
int _maxLines(BuildContext context) {
final media = MediaQuery.of(context);
// The scaffold hides the keyboard from its body; the window still has it.
final keyboard = MediaQueryData.fromView(
View.of(context),
).viewInsets.bottom;
final visible = media.size.height - keyboard;
final lineHeight = MediaQuery.textScalerOf(context).scale(22);
final fit = (visible * 0.4 / lineHeight).floor();
return fit.clamp(3, _composerMaxLines);
}
Widget _field(bool cupertino) {
final maxLines = _maxLines(context);
if (cupertino) {
// CupertinoTextField lays its placeholder out with up to maxLines lines
// but sizes an empty field by minLines, so a wrapped placeholder was
// clipped on a narrow pane. While empty the field is exactly as tall as
// its placeholder (at most 3 lines, then ellipsised); typing restores
// the normal 1-6 lines.
// the normal range (see _maxLines).
return LayoutBuilder(
builder: (context, constraints) {
final empty = _text.text.isEmpty;
@@ -1031,7 +1049,7 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
minLines: lines,
// Keep the empty field multiline too: a single-line web editor
// strips Shift+Enter's newline on the next text edit.
maxLines: empty && lines > 1 ? lines : 6,
maxLines: empty && lines > 1 ? lines : maxLines,
keyboardType: TextInputType.multiline,
textInputAction: _enterSends
? TextInputAction.send
@@ -1048,7 +1066,7 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
controller: _text,
focusNode: _focus,
minLines: 1,
maxLines: 6,
maxLines: maxLines,
keyboardType: TextInputType.multiline,
textInputAction: _enterSends
? TextInputAction.send
@@ -0,0 +1,43 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'chat_composer_bar_fixtures.dart';
/// The composer grows to 12 lines before it scrolls, but never past about
/// 40% of the visible height (the keyboard excluded).
void main() {
int? maxLinesOf(WidgetTester tester, bool cupertino) {
final field = find.byKey(const Key('chat-composer-field'));
return cupertino
? tester.widget<CupertinoTextField>(field).maxLines
: tester.widget<TextField>(field).maxLines;
}
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('$host: a tall window allows 12 lines; a phone with its '
'keyboard open fewer', (tester) async {
final gateway = ComposerBarGateway()..page = fixtureTurn();
addTearDown(gateway.close);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
tester.view.physicalSize = const Size(900, 1000);
await tester.pumpWidget(composerThread(gateway, cupertino: cupertino));
await tester.pump();
// A non-empty field (the Cupertino placeholder sizing only applies
// while empty).
await tester.enterText(find.byKey(const Key('chat-composer-field')), 'x');
await tester.pump();
expect(maxLinesOf(tester, cupertino), 12);
tester.view.physicalSize = const Size(390, 844);
tester.view.viewInsets = const FakeViewPadding(bottom: 340);
await tester.pump();
final phone = maxLinesOf(tester, cupertino)!;
expect(phone, lessThan(12));
expect(phone, greaterThan(6));
});
}
}