fix(app): an empty Cupertino composer is as tall as its wrapped placeholder
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -687,18 +688,53 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
|
||||
);
|
||||
}
|
||||
|
||||
/// The lines the composer placeholder needs at [width] (the field's
|
||||
/// default 7px side padding excluded), capped at 3.
|
||||
int _placeholderLines(BuildContext context, double width) {
|
||||
if (!width.isFinite) return 1;
|
||||
final painter = TextPainter(
|
||||
text: TextSpan(
|
||||
text: chatComposerPlaceholder,
|
||||
style: CupertinoTheme.of(context).textTheme.textStyle,
|
||||
),
|
||||
textDirection: Directionality.of(context),
|
||||
textScaler: MediaQuery.textScalerOf(context),
|
||||
maxLines: 3,
|
||||
)..layout(maxWidth: math.max(0, width - 14));
|
||||
final lines = painter.computeLineMetrics().length;
|
||||
painter.dispose();
|
||||
return lines.clamp(1, 3);
|
||||
}
|
||||
|
||||
Widget _field(bool cupertino) {
|
||||
if (cupertino) {
|
||||
return CupertinoTextField(
|
||||
key: const Key('chat-composer-field'),
|
||||
controller: _text,
|
||||
focusNode: _focus,
|
||||
placeholder: chatComposerPlaceholder,
|
||||
minLines: 1,
|
||||
maxLines: 6,
|
||||
keyboardType: TextInputType.multiline,
|
||||
textInputAction: TextInputAction.newline,
|
||||
onChanged: _onDraftEdited,
|
||||
// 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.
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final empty = _text.text.isEmpty;
|
||||
final lines = empty
|
||||
? _placeholderLines(context, constraints.maxWidth)
|
||||
: 1;
|
||||
return CupertinoTextField(
|
||||
key: const Key('chat-composer-field'),
|
||||
controller: _text,
|
||||
focusNode: _focus,
|
||||
placeholder: chatComposerPlaceholder,
|
||||
placeholderStyle: const TextStyle(
|
||||
color: CupertinoColors.placeholderText,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
minLines: lines,
|
||||
maxLines: empty ? lines : 6,
|
||||
keyboardType: TextInputType.multiline,
|
||||
textInputAction: TextInputAction.newline,
|
||||
onChanged: _onDraftEdited,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
return TextField(
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart';
|
||||
|
||||
import 'chat_composer_bar_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('an empty Cupertino composer holds its whole placeholder at '
|
||||
'320px with 2x text', (tester) async {
|
||||
tester.view.physicalSize = const Size(640, 1400);
|
||||
tester.view.devicePixelRatio = 2;
|
||||
tester.platformDispatcher.textScaleFactorTestValue = 2;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
|
||||
|
||||
final gateway = ComposerBarGateway();
|
||||
addTearDown(gateway.close);
|
||||
await tester.pumpWidget(composerThread(gateway, cupertino: true));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final field = tester.getRect(find.byType(CupertinoTextField));
|
||||
final placeholder = find.text(chatComposerPlaceholder);
|
||||
final rect = tester.getRect(placeholder);
|
||||
expect(rect.bottom, lessThanOrEqualTo(field.bottom));
|
||||
// Longer than three lines it ends in an ellipsis, never a clipped line.
|
||||
final paragraph = tester.renderObject<RenderParagraph>(placeholder);
|
||||
if (paragraph.didExceedMaxLines) {
|
||||
expect(paragraph.overflow, TextOverflow.ellipsis);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user