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

This PR changes the Hint uses topLeft alignment when maintainHintSize is
false
> code
```dart
buildTextField() {
  return const Center(
    child: TextField(
      decoration: InputDecoration(
        hintText: 'hint',
        maintainHintSize: false,
      ),
    ),
  );
}
```

Before:

<img width="416" alt="b"
src="https://github.com/user-attachments/assets/fe58c2d0-4d37-4bca-aabc-0f7d4785fb2a"
/>

After:
<img width="415" alt="a"
src="https://github.com/user-attachments/assets/096fd83a-9d8f-4cb7-be1d-c3075acbfdc0"
/>



## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

---------

Co-authored-by: 郑泽钦 <zhengzeqin@addcn.com>
This commit is contained in:
zhengzeqin 2025-06-24 16:04:12 +08:00 committed by GitHub
parent 0adb0337e0
commit dd89ae96c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 0 deletions

View File

@ -2267,6 +2267,10 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
return FadeTransition(opacity: _curvedAnimation!, child: child);
}
static Widget _topStartLayout(Widget? currentChild, List<Widget> previousChildren) {
return Stack(children: <Widget>[...previousChildren, if (currentChild != null) currentChild]);
}
@override
Widget build(BuildContext context) {
final ThemeData themeData = Theme.of(context);
@ -2308,6 +2312,7 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
: AnimatedSwitcher(
duration: decoration.hintFadeDuration ?? _kHintFadeTransitionDuration,
transitionBuilder: _buildTransition,
layoutBuilder: _topStartLayout,
child: showHint ? hintWidget : const SizedBox.shrink(),
);
}

View File

@ -5271,6 +5271,45 @@ void main() {
// The hintText replaced with SizeBox.
expect(find.text(hintText), findsNothing);
});
testWidgets('Hint does not change position when maintainHintSize is false - LTR', (
WidgetTester tester,
) async {
await tester.pumpWidget(
buildInputDecorator(isEmpty: true, decoration: const InputDecoration(hintText: hintText)),
);
final double expectedHintLeft = getHintRect(tester).left;
await tester.pumpWidget(
buildInputDecorator(
isEmpty: true,
decoration: const InputDecoration(hintText: hintText, maintainHintSize: false),
),
);
expect(getHintRect(tester).left, expectedHintLeft);
});
testWidgets('Hint does not change position when maintainHintSize is false - RTL', (
WidgetTester tester,
) async {
await tester.pumpWidget(
buildInputDecorator(
isEmpty: true,
decoration: const InputDecoration(hintText: hintText),
textDirection: TextDirection.rtl,
),
);
final double expectedHintRight = getHintRect(tester).right;
await tester.pumpWidget(
buildInputDecorator(
isEmpty: true,
decoration: const InputDecoration(hintText: hintText, maintainHintSize: false),
textDirection: TextDirection.rtl,
),
);
expect(getHintRect(tester).right, expectedHintRight);
});
});
group('Material3 - InputDecoration helper/counter/error', () {