From 9f744a9e4cbe25a6e135dd1fc856cef546dfcc3f Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Thu, 21 May 2020 14:37:04 -0700 Subject: [PATCH] Allow updating textAlignVertical (#57033) --- .../lib/src/material/input_decorator.dart | 15 ++++--- .../test/material/input_decorator_test.dart | 39 +++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index 989850583e1..3136bf50968 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -781,20 +781,22 @@ class _RenderDecoration extends RenderBox { markNeedsLayout(); } + TextAlignVertical get _defaultTextAlignVertical => _isOutlineAligned + ? TextAlignVertical.center + : TextAlignVertical.top; TextAlignVertical get textAlignVertical { if (_textAlignVertical == null) { - return _isOutlineAligned ? TextAlignVertical.center : TextAlignVertical.top; + return _defaultTextAlignVertical; } return _textAlignVertical; } TextAlignVertical _textAlignVertical; set textAlignVertical(TextAlignVertical value) { - assert(value != null); if (_textAlignVertical == value) { return; } // No need to relayout if the effective value is still the same. - if (textAlignVertical.y == value.y) { + if (textAlignVertical.y == (value?.y ?? _defaultTextAlignVertical.y)) { _textAlignVertical = value; return; } @@ -1707,10 +1709,11 @@ class _Decorator extends RenderObjectWidget { void updateRenderObject(BuildContext context, _RenderDecoration renderObject) { renderObject ..decoration = decoration - ..textDirection = textDirection - ..textBaseline = textBaseline ..expands = expands - ..isFocused = isFocused; + ..isFocused = isFocused + ..textAlignVertical = textAlignVertical + ..textBaseline = textBaseline + ..textDirection = textDirection; } } diff --git a/packages/flutter/test/material/input_decorator_test.dart b/packages/flutter/test/material/input_decorator_test.dart index 6187713215f..1f4bb823081 100644 --- a/packages/flutter/test/material/input_decorator_test.dart +++ b/packages/flutter/test/material/input_decorator_test.dart @@ -3997,4 +3997,43 @@ void main() { // because the label is not initially floating. expect(tester.getTopLeft(find.text('label')).dy, 20.0); }); + + testWidgets('textAlignVertical can be updated', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/56933 + const String hintText = 'hint'; + TextAlignVertical alignment = TextAlignVertical.top; + StateSetter setState; + await tester.pumpWidget( + MaterialApp( + home: StatefulBuilder( + builder: (BuildContext context, StateSetter setter) { + setState = setter; + return InputDecorator( + textAlignVertical: alignment, + decoration: const InputDecoration( + hintText: hintText, + ), + ); + }, + ), + ), + ); + + final double topPosition = tester.getTopLeft(find.text(hintText)).dy; + + setState(() { + alignment = TextAlignVertical.bottom; + }); + await tester.pump(); + + expect(tester.getTopLeft(find.text(hintText)).dy, greaterThan(topPosition)); + + // Setting textAlignVertical back to null works and reverts to the default. + setState(() { + alignment = null; + }); + await tester.pump(); + + expect(tester.getTopLeft(find.text(hintText)).dy, topPosition); + }); }