Include cursor in textfield intrinsic width measurement (#25055)
* Include cursor in textfield intrinsic width measurement * Add cursorWidth assertion, add comment about assumed cursor width, and test null stepWidth
This commit is contained in:
parent
4d2d800d9e
commit
9161ec42cb
@ -160,6 +160,7 @@ class RenderEditable extends RenderBox {
|
||||
assert(ignorePointer != null),
|
||||
assert(obscureText != null),
|
||||
assert(textSelectionDelegate != null),
|
||||
assert(cursorWidth != null && cursorWidth >= 0.0),
|
||||
_textPainter = TextPainter(
|
||||
text: text,
|
||||
textAlign: textAlign,
|
||||
@ -1060,7 +1061,7 @@ class RenderEditable extends RenderBox {
|
||||
@override
|
||||
double computeMaxIntrinsicWidth(double height) {
|
||||
_layoutText(double.infinity);
|
||||
return _textPainter.maxIntrinsicWidth;
|
||||
return _textPainter.maxIntrinsicWidth + cursorWidth;
|
||||
}
|
||||
|
||||
/// An estimate of the height of a line in the text. See [TextPainter.preferredLineHeight].
|
||||
|
@ -3485,6 +3485,80 @@ void main() {
|
||||
expect(tapCount, 0);
|
||||
});
|
||||
|
||||
testWidgets('Includes cursor for TextField', (WidgetTester tester) async {
|
||||
// This is a regression test for https://github.com/flutter/flutter/issues/24612
|
||||
|
||||
Widget buildFrame({
|
||||
double stepWidth,
|
||||
double cursorWidth,
|
||||
TextAlign textAlign,
|
||||
}) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
IntrinsicWidth(
|
||||
stepWidth: stepWidth,
|
||||
child: TextField(
|
||||
textAlign: textAlign,
|
||||
cursorWidth: cursorWidth,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// A cursor of default size doesn't cause the TextField to increase its
|
||||
// width.
|
||||
const String text = '1234';
|
||||
double stepWidth = 80.0;
|
||||
await tester.pumpWidget(buildFrame(
|
||||
stepWidth: 80.0,
|
||||
cursorWidth: 2.0,
|
||||
textAlign: TextAlign.left,
|
||||
));
|
||||
await tester.enterText(find.byType(TextField), text);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getSize(find.byType(TextField)).width, stepWidth);
|
||||
|
||||
// A wide cursor is counted in the width of the text and causes the
|
||||
// TextField to increase to twice the stepWidth.
|
||||
await tester.pumpWidget(buildFrame(
|
||||
stepWidth: stepWidth,
|
||||
cursorWidth: 18.0,
|
||||
textAlign: TextAlign.left,
|
||||
));
|
||||
await tester.enterText(find.byType(TextField), text);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getSize(find.byType(TextField)).width, 2 * stepWidth);
|
||||
|
||||
// A null stepWidth causes the TextField to perfectly wrap the text plus
|
||||
// the cursor regardless of alignment.
|
||||
stepWidth = null;
|
||||
const double WIDTH_OF_CHAR = 16.0;
|
||||
await tester.pumpWidget(buildFrame(
|
||||
stepWidth: stepWidth,
|
||||
cursorWidth: 18.0,
|
||||
textAlign: TextAlign.left,
|
||||
));
|
||||
await tester.enterText(find.byType(TextField), text);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getSize(find.byType(TextField)).width, WIDTH_OF_CHAR * text.length + 18.0);
|
||||
await tester.pumpWidget(buildFrame(
|
||||
stepWidth: stepWidth,
|
||||
cursorWidth: 18.0,
|
||||
textAlign: TextAlign.right,
|
||||
));
|
||||
await tester.enterText(find.byType(TextField), text);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getSize(find.byType(TextField)).width, WIDTH_OF_CHAR * text.length + 18.0);
|
||||
});
|
||||
|
||||
testWidgets('TextField style is merged with theme', (WidgetTester tester) async {
|
||||
// Regression test for https://github.com/flutter/flutter/issues/23994
|
||||
|
||||
|
@ -44,7 +44,8 @@ void main() {
|
||||
textSelectionDelegate: delegate,
|
||||
);
|
||||
expect(editable.getMinIntrinsicWidth(double.infinity), 50.0);
|
||||
expect(editable.getMaxIntrinsicWidth(double.infinity), 50.0);
|
||||
// The width includes the width of the cursor (1.0).
|
||||
expect(editable.getMaxIntrinsicWidth(double.infinity), 51.0);
|
||||
expect(editable.getMinIntrinsicHeight(double.infinity), 10.0);
|
||||
expect(editable.getMaxIntrinsicHeight(double.infinity), 10.0);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user