Skip to content

When maintainHintSize is false, hint is centered and aligned, it is different from the original one #168654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/flutter/lib/src/material/input_decorator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2289,6 +2289,13 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
return FadeTransition(opacity: _curvedAnimation!, child: child);
}

static Widget _topLeftLayout(Widget? currentChild, List<Widget> previousChildren) {
return Stack(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using an Align widget?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justinmc

What about using an Align widget?

@justinmc Thank you for your suggestion, but layoutBuilder needs to return multiple children. It seems that this cannot be done if Align is used? The defaultLayoutBuilder implementation uses Stack to center the layout.

alignment: Alignment.topLeft,
children: <Widget>[...previousChildren, if (currentChild != null) currentChild],
);
}

@override
Widget build(BuildContext context) {
final ThemeData themeData = Theme.of(context);
Expand Down Expand Up @@ -2331,6 +2338,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
: AnimatedSwitcher(
duration: decoration.hintFadeDuration ?? _kHintFadeTransitionDuration,
transitionBuilder: _buildTransition,
layoutBuilder: _topLeftLayout,
child: showHint ? hintWidget : const SizedBox.shrink(),
);
}
Expand Down
33 changes: 33 additions & 0 deletions packages/flutter/test/material/input_decorator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5243,6 +5243,39 @@ void main() {
// The hintText replaced with SizeBox.
expect(find.text(hintText), findsNothing);
});

testWidgets('Hint uses topLeft alignment when maintainHintSize is false', (
WidgetTester tester,
) async {
const InputDecoration decoration = InputDecoration(
hintText: hintText,
maintainHintSize: false,
);

await tester.pumpWidget(buildInputDecorator(isEmpty: true, decoration: decoration));

final Finder animatedSwitcherFinder = find.byType(AnimatedSwitcher);
expect(animatedSwitcherFinder, findsOneWidget);

final Finder stackFinder = find.descendant(
of: animatedSwitcherFinder,
matching: find.byType(Stack),
);
expect(stackFinder, findsOneWidget);

final Stack stack = tester.widget<Stack>(stackFinder);
expect(stack.alignment, Alignment.topLeft);

final Finder hintTextFinder = find.text(hintText);
expect(hintTextFinder, findsOneWidget);

// Get the hint text box and stack box to calculate the position of the hint text.
final RenderBox hintTextBox = tester.renderObject<RenderBox>(hintTextFinder);
final RenderBox stackBox = tester.renderObject<RenderBox>(stackFinder);
final Offset hintTextPosition = hintTextBox.localToGlobal(Offset.zero, ancestor: stackBox);
expect(hintTextPosition.dx, 0.0);
expect(hintTextPosition.dy, 0.0);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check the actual left position of the hintText too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justinmc Thanks you, I updated

});

group('Material3 - InputDecoration helper/counter/error', () {
Expand Down