fix(player): 修正评论面板键盘态布局重叠

This commit is contained in:
Zhou Weidong committed 2026-10-21 13:00:05 +08:00
1 parent a7c0576a03
commit bc4c84a4ae
2 files changed
+95 -73

No files matched your search

@@ -184,54 +184,67 @@ class _DramaCommentsSheetState extends State<DramaCommentsSheet> {
final maxHeight = screenLimit < _maxSheetHeight final maxHeight = screenLimit < _maxSheetHeight
? screenLimit ? screenLimit
: _maxSheetHeight; : _maxSheetHeight;
final focused = _inputFocus.hasFocus; final inputFocused = _inputFocus.hasFocus;
final keyboardHeight = _keyboardHeight(context); final keyboardHeight = _keyboardHeight(context);
final keyboardOpen = keyboardHeight > 0;
final expanded = inputFocused && keyboardOpen;
final availableHeight = math.max(0.0, screenLimit - keyboardHeight);
final keyboardPanelHeight = math.min(
_sheetHeightWithoutKeyboard ?? maxHeight,
availableHeight,
);
_measureSheetWithoutKeyboard(keyboardHeight); _measureSheetWithoutKeyboard(keyboardHeight);
return GestureDetector( return AnimatedPadding(
behavior: HitTestBehavior.translucent, duration: const Duration(milliseconds: 200),
onTap: focused ? _inputFocus.unfocus : null, curve: Curves.easeOut,
child: SizedBox( padding: EdgeInsets.only(bottom: keyboardHeight),
key: _sheetMeasureKey, child: GestureDetector(
child: Container( behavior: HitTestBehavior.translucent,
key: const Key('player-comments-sheet'), onTap: inputFocused ? _inputFocus.unfocus : null,
decoration: const BoxDecoration( child: SizedBox(
color: AppColors.card, key: _sheetMeasureKey,
borderRadius: BorderRadius.vertical( child: Container(
top: Radius.circular(_sheetRadius), key: const Key('player-comments-sheet'),
decoration: const BoxDecoration(
color: AppColors.card,
borderRadius: BorderRadius.vertical(
top: Radius.circular(_sheetRadius),
),
), ),
), child: SafeArea(
child: SafeArea( top: false,
top: false, child: ListenableBuilder(
child: ListenableBuilder( listenable: _controller,
listenable: _controller, builder: (context, _) {
builder: (context, _) { final minHeight = _controller.comments.isEmpty
final minHeight = _controller.comments.isEmpty ? (_minSheetHeight < maxHeight
? (_minSheetHeight < maxHeight ? _minSheetHeight
? _minSheetHeight : maxHeight)
: maxHeight) : 0.0;
: 0.0; return AnimatedSize(
final pinnedHeight = duration: const Duration(milliseconds: 200),
keyboardHeight > 0 && _sheetHeightWithoutKeyboard != null curve: Curves.easeOut,
? math.min(_sheetHeightWithoutKeyboard!, maxHeight) alignment: Alignment.bottomCenter,
: null; child: ConstrainedBox(
return ConstrainedBox( key: const Key('comment-sheet-constraints'),
key: const Key('comment-sheet-constraints'), constraints: keyboardOpen
constraints: pinnedHeight == null ? BoxConstraints.tightFor(height: keyboardPanelHeight)
? BoxConstraints( : BoxConstraints(
minHeight: minHeight, minHeight: minHeight,
maxHeight: maxHeight, maxHeight: maxHeight,
) ),
: BoxConstraints.tightFor(height: pinnedHeight), child: Padding(
child: Padding( padding: const EdgeInsets.symmetric(horizontal: 16),
padding: const EdgeInsets.symmetric(horizontal: 16), child: _content(
child: _content( context,
context, expanded: expanded,
focused: focused, keyboardHeight: keyboardHeight,
keyboardHeight: keyboardHeight, ),
),
), ),
), );
); },
}, ),
), ),
), ),
), ),
@@ -241,12 +254,12 @@ class _DramaCommentsSheetState extends State<DramaCommentsSheet> {
Widget _content( Widget _content(
BuildContext context, { BuildContext context, {
required bool focused, required bool expanded,
required double keyboardHeight, required double keyboardHeight,
}) { }) {
final l10n = AppLocalizations.of(context)!; final l10n = AppLocalizations.of(context)!;
final count = _serverCount ?? _controller.exactCount; final count = _serverCount ?? _controller.exactCount;
final composerReserve = focused final composerReserve = expanded
? _expandedComposerReserve ? _expandedComposerReserve
: _composerHeight + _composerTopGap; : _composerHeight + _composerTopGap;
return Column( return Column(
@@ -299,18 +312,13 @@ class _DramaCommentsSheetState extends State<DramaCommentsSheet> {
), ),
), ),
if (_replyTo != null) _replyBanner(l10n, _replyTo!), if (_replyTo != null) _replyBanner(l10n, _replyTo!),
AnimatedContainer( Padding(
key: const Key('comment-input-lift'), key: const Key('comment-composer-area'),
duration: const Duration(milliseconds: 200), padding: EdgeInsets.only(
curve: Curves.easeOut, top: expanded ? 12 : _composerTopGap,
transform: Matrix4.translationValues(0, -keyboardHeight, 0), bottom: expanded ? 12 : 0,
child: Padding(
padding: EdgeInsets.only(
top: focused ? 12 : _composerTopGap,
bottom: focused ? 12 : 0,
),
child: _composer(l10n, focused: focused),
), ),
child: _composer(l10n, focused: expanded),
), ),
], ],
); );
+30 -16
View File
@@ -222,6 +222,7 @@ void main() {
testWidgets('should_clear_and_keep_input_focused_after_successful_post', ( testWidgets('should_clear_and_keep_input_focused_after_successful_post', (
tester, tester,
) async { ) async {
addTearDown(tester.view.resetViewInsets);
final repository = FakeCommentRepository( final repository = FakeCommentRepository(
pages: [const CommentPage()], pages: [const CommentPage()],
publishResult: commentFixture(id: '11', content: 'Nice demo', mine: true), publishResult: commentFixture(id: '11', content: 'Nice demo', mine: true),
@@ -230,8 +231,9 @@ void main() {
await _pumpSheet(tester, repository: repository); await _pumpSheet(tester, repository: repository);
final input = find.byKey(const Key('comment-input')); final input = find.byKey(const Key('comment-input'));
tester.view.viewInsets = const FakeViewPadding(bottom: 180);
await tester.tap(input); await tester.tap(input);
await tester.pump(); await tester.pumpAndSettle();
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isTrue); expect(tester.widget<TextField>(input).focusNode?.hasFocus, isTrue);
await tester.enterText(input, 'Nice demo'); await tester.enterText(input, 'Nice demo');
await tester.pump(); await tester.pump();
@@ -258,7 +260,7 @@ void main() {
expect(repository.countCalls, ['5', '5']); expect(repository.countCalls, ['5', '5']);
}); });
testWidgets('should_expand_composer_only_while_input_is_focused', ( testWidgets('should_keep_composer_separate_when_keyboard_opens', (
tester, tester,
) async { ) async {
tester.view.physicalSize = const Size(390, 844); tester.view.physicalSize = const Size(390, 844);
@@ -290,6 +292,11 @@ void main() {
expect(find.byKey(const Key('comment-count-label')), findsOneWidget); expect(find.byKey(const Key('comment-count-label')), findsOneWidget);
expect(find.byKey(const Key('comment-composer')), findsOneWidget); expect(find.byKey(const Key('comment-composer')), findsOneWidget);
expect(find.byKey(const Key('comment-input-surface')), findsOneWidget); expect(find.byKey(const Key('comment-input-surface')), findsOneWidget);
expect(
tester.widget<TextField>(find.byKey(const Key('comment-input'))).maxLines,
1,
);
expect(tester.getSize(find.byKey(const Key('comment-input'))).height, 44);
final editableAfter = tester.state<EditableTextState>( final editableAfter = tester.state<EditableTextState>(
find.descendant( find.descendant(
of: find.byKey(const Key('comment-input')), of: find.byKey(const Key('comment-input')),
@@ -297,6 +304,16 @@ void main() {
), ),
); );
expect(identical(editableBefore, editableAfter), isTrue); expect(identical(editableBefore, editableAfter), isTrue);
final sheet = find.byKey(const Key('player-comments-sheet'));
final list = find.byKey(const Key('comment-list'));
tester.view.viewInsets = const FakeViewPadding(bottom: 180);
await tester.pump();
await tester.pump(const Duration(milliseconds: 200));
expect(sheet, findsOneWidget);
expect(list, findsOneWidget);
expect(find.byKey(const Key('comment-count-label')), findsOneWidget);
final field = tester.widget<TextField>( final field = tester.widget<TextField>(
find.byKey(const Key('comment-input')), find.byKey(const Key('comment-input')),
); );
@@ -310,6 +327,10 @@ void main() {
isNull, isNull,
); );
expect(tester.getSize(find.byKey(const Key('comment-post'))).height, 32); expect(tester.getSize(find.byKey(const Key('comment-post'))).height, 32);
final listRect = tester.getRect(list);
final inputRect = tester.getRect(find.byKey(const Key('comment-input')));
expect(listRect.overlaps(inputRect), isFalse);
expect(listRect.bottom, lessThanOrEqualTo(inputRect.top));
expect(find.byIcon(Icons.image_outlined), findsNothing); expect(find.byIcon(Icons.image_outlined), findsNothing);
expect(find.byIcon(Icons.emoji_emotions_outlined), findsNothing); expect(find.byIcon(Icons.emoji_emotions_outlined), findsNothing);
expect(find.byIcon(Icons.alternate_email_rounded), findsNothing); expect(find.byIcon(Icons.alternate_email_rounded), findsNothing);
@@ -323,16 +344,6 @@ void main() {
isNotNull, isNotNull,
); );
final sheet = find.byKey(const Key('player-comments-sheet'));
final list = find.byKey(const Key('comment-list'));
final sheetTop = tester.getTopLeft(sheet).dy;
final listTop = tester.getTopLeft(list).dy;
tester.view.viewInsets = const FakeViewPadding(bottom: 180);
await tester.pump();
await tester.pump(const Duration(milliseconds: 200));
expect(sheet, findsOneWidget);
expect(list, findsOneWidget);
expect(find.byKey(const Key('comment-count-label')), findsOneWidget);
final editableDuringKeyboard = tester.state<EditableTextState>( final editableDuringKeyboard = tester.state<EditableTextState>(
find.descendant( find.descendant(
of: find.byKey(const Key('comment-input')), of: find.byKey(const Key('comment-input')),
@@ -340,8 +351,7 @@ void main() {
), ),
); );
expect(identical(editableAfter, editableDuringKeyboard), isTrue); expect(identical(editableAfter, editableDuringKeyboard), isTrue);
expect(tester.getTopLeft(sheet).dy, closeTo(sheetTop, 0.1)); expect(tester.getBottomLeft(sheet).dy, closeTo(844 - 180, 0.1));
expect(tester.getTopLeft(list).dy, closeTo(listTop, 0.1));
expect( expect(
tester.widget<ListView>(list).padding?.resolve(TextDirection.ltr).bottom, tester.widget<ListView>(list).padding?.resolve(TextDirection.ltr).bottom,
greaterThanOrEqualTo(180 + 44), greaterThanOrEqualTo(180 + 44),
@@ -363,6 +373,12 @@ void main() {
expect(list, findsOneWidget); expect(list, findsOneWidget);
expect(find.byKey(const Key('comment-count-label')), findsOneWidget); expect(find.byKey(const Key('comment-count-label')), findsOneWidget);
expect(find.byKey(const Key('comment-composer')), findsOneWidget); expect(find.byKey(const Key('comment-composer')), findsOneWidget);
final collapsedField = tester.widget<TextField>(
find.byKey(const Key('comment-input')),
);
expect(collapsedField.maxLines, 1);
expect(tester.getSize(find.byKey(const Key('comment-input'))).height, 44);
expect(tester.getSize(sheet).height, lessThan(520));
final editableAfterKeyboard = tester.state<EditableTextState>( final editableAfterKeyboard = tester.state<EditableTextState>(
find.descendant( find.descendant(
of: find.byKey(const Key('comment-input')), of: find.byKey(const Key('comment-input')),
@@ -370,8 +386,6 @@ void main() {
), ),
); );
expect(identical(editableAfter, editableAfterKeyboard), isTrue); expect(identical(editableAfter, editableAfterKeyboard), isTrue);
expect(tester.getTopLeft(sheet).dy, closeTo(sheetTop, 0.1));
expect(tester.getTopLeft(list).dy, closeTo(listTop, 0.1));
expect( expect(
tester tester
.widget<TextField>(find.byKey(const Key('comment-input'))) .widget<TextField>(find.byKey(const Key('comment-input')))