fix(player): 评论输入改为独立弹窗

This commit is contained in:
Zhou Weidong committed 2026-10-21 13:31:41 +08:00
1 parent aa14d0a835
commit 282f51c24b
4 files changed
+418 -495

No files matched your search

+67 -162
View File
@@ -48,21 +48,18 @@ void main() {
final repository = FakeCommentRepository(pages: [const CommentPage()]);
await _pumpSheet(tester, repository: repository);
final input = find.byKey(const Key('comment-input'), skipOffstage: false);
final placeholder = find.byKey(const Key('comment-input-placeholder'));
expect(placeholder, findsOneWidget);
expect(find.byKey(const Key('comment-input')), findsNothing);
expect(find.text('Sign in to comment'), findsOneWidget);
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isFalse);
await tester.tap(placeholder);
await tester.pumpAndSettle();
expect(find.text('Sign in to continue'), findsOneWidget);
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isFalse);
await tester.tap(find.text('Not now'));
await tester.pumpAndSettle();
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isFalse);
expect(find.byKey(const Key('comment-composer-sheet')), findsNothing);
expect(placeholder, findsOneWidget);
});
testWidgets('should_focus_comment_input_after_successful_login', (
@@ -82,31 +79,35 @@ void main() {
(auth as _TestAuthController).setSignedIn(true);
await tester.pump();
Navigator.of(tester.element(find.byKey(const Key('auth-landing')))).pop();
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
await tester.pumpAndSettle();
expect(
tester
.widget<TextField>(find.byKey(const Key('comment-input')))
.widget<TextField>(find.byKey(const Key('comment-composer-input')))
.focusNode
?.hasFocus,
isTrue,
);
});
testWidgets('should_focus_real_input_when_fake_send_is_tapped', (
testWidgets('should_open_composer_sheet_when_fake_send_is_tapped', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final repository = FakeCommentRepository(pages: [const CommentPage()]);
await _pumpSheet(tester, repository: repository);
expect(find.byKey(const Key('comment-input')), findsNothing);
await tester.tap(find.byKey(const Key('comment-post')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('comment-input-placeholder')), findsNothing);
final input = find.byKey(const Key('comment-input'));
final composer = find.byKey(const Key('comment-composer-sheet'));
expect(composer, findsOneWidget);
expect(tester.getSize(composer).height, closeTo(844 / 2, 0.1));
final input = find.byKey(const Key('comment-composer-input'));
expect(input, findsOneWidget);
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isTrue);
expect(
@@ -234,16 +235,16 @@ void main() {
await tester.tap(find.byKey(const Key('comment-input-placeholder')));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('comment-input')),
find.byKey(const Key('comment-composer-input')),
'blocked text',
);
await tester.pump();
await tester.tap(find.byKey(const Key('comment-post')));
await tester.tap(find.byKey(const Key('comment-composer-post')));
await tester.pumpAndSettle();
expect(find.text('评论包含敏感词'), findsOneWidget);
final field = tester.widget<TextField>(
find.byKey(const Key('comment-input')),
find.byKey(const Key('comment-composer-input')),
);
expect(field.controller?.text, 'blocked text');
// 发送失败时保留输入与焦点,用户可以直接改完再发。
@@ -251,9 +252,7 @@ void main() {
},
);
testWidgets('should_clear_and_keep_input_focused_after_successful_post', (
tester,
) async {
testWidgets('should_close_composer_after_successful_post', (tester) async {
addTearDown(tester.view.resetViewInsets);
final repository = FakeCommentRepository(
pages: [const CommentPage()],
@@ -262,7 +261,7 @@ void main() {
);
await _pumpSheet(tester, repository: repository);
final input = find.byKey(const Key('comment-input'));
final input = find.byKey(const Key('comment-composer-input'));
tester.view.viewInsets = const FakeViewPadding(bottom: 180);
await tester.tap(find.byKey(const Key('comment-input-placeholder')));
await tester.pumpAndSettle();
@@ -271,28 +270,21 @@ void main() {
await tester.pump();
expect(
tester
.widget<TextButton>(find.byKey(const Key('comment-post')))
.widget<TextButton>(find.byKey(const Key('comment-composer-post')))
.onPressed,
isNotNull,
);
await tester.tap(find.byKey(const Key('comment-post')));
await tester.tap(find.byKey(const Key('comment-composer-post')));
await tester.pumpAndSettle();
expect(tester.widget<TextField>(input).controller?.text, isEmpty);
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isTrue);
expect(tester.widget<TextField>(input).maxLines, 4);
expect(find.byKey(const Key('comment-composer-sheet')), findsNothing);
expect(find.byKey(const Key('comment-list')), findsOneWidget);
expect(find.byKey(const Key('comment-count-label')), findsOneWidget);
expect(
tester
.widget<TextButton>(find.byKey(const Key('comment-post')))
.onPressed,
isNull,
);
expect(find.byKey(const Key('comment-input-placeholder')), findsOneWidget);
expect(repository.countCalls, ['5', '5']);
});
testWidgets('should_keep_composer_separate_when_keyboard_opens', (
testWidgets('should_keep_comment_panel_fixed_and_open_composer_sheet', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
@@ -309,128 +301,59 @@ void main() {
);
await _pumpSheet(tester, repository: repository);
expect(find.byKey(const Key('comment-list')), findsOneWidget);
expect(find.byKey(const Key('comment-input')), findsNothing);
expect(find.byKey(const Key('comment-input-placeholder')), findsOneWidget);
final hiddenInput = find.byKey(
const Key('comment-input'),
skipOffstage: false,
);
final editableBefore = tester.state<EditableTextState>(
find.descendant(
of: hiddenInput,
matching: find.byType(EditableText, skipOffstage: false),
skipOffstage: false,
),
);
final sheet = find.byKey(const Key('player-comments-sheet'));
final commentsSheet = find.byKey(const Key('player-comments-sheet'));
final list = find.byKey(const Key('comment-list'));
final heightBeforeKeyboard = tester.getSize(sheet).height;
tester.view.viewInsets = const FakeViewPadding(bottom: 180);
final placeholder = find.byKey(const Key('comment-input-placeholder'));
final sheetRect = tester.getRect(commentsSheet);
final sheetHeight = tester.getSize(commentsSheet).height;
await tester.tap(find.byKey(const Key('comment-input-placeholder')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('comment-list')), findsOneWidget);
expect(find.byKey(const Key('comment-count-label')), 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-placeholder')), findsNothing);
expect(
tester.widget<TextField>(find.byKey(const Key('comment-input'))).maxLines,
4,
final composer = find.byKey(const Key('comment-composer-sheet'));
final composerInput = find.byKey(const Key('comment-composer-input'));
expect(composer, findsOneWidget);
expect(tester.getSize(composer).height, closeTo(844 / 2, 0.1));
expect(tester.widget<TextField>(composerInput).focusNode?.hasFocus, isTrue);
final underlyingSheet = find.byKey(
const Key('player-comments-sheet'),
skipOffstage: false,
);
final editableAfter = tester.state<EditableTextState>(
find.descendant(
of: find.byKey(const Key('comment-input')),
matching: find.byType(EditableText),
),
);
expect(identical(editableBefore, editableAfter), isTrue);
expect(tester.getRect(underlyingSheet), sheetRect);
expect(tester.getSize(underlyingSheet).height, closeTo(sheetHeight, 0.1));
expect(sheet, findsOneWidget);
expect(list, findsOneWidget);
expect(tester.getSize(sheet).height, closeTo(heightBeforeKeyboard, 0.1));
expect(find.byKey(const Key('comment-count-label')), findsOneWidget);
final field = tester.widget<TextField>(
find.byKey(const Key('comment-input')),
);
final field = tester.widget<TextField>(composerInput);
expect(field.minLines, 1);
expect(field.maxLines, 4);
expect(field.style?.fontSize, 15);
expect(
tester
.widget<TextButton>(find.byKey(const Key('comment-post')))
.onPressed,
isNull,
);
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.emoji_emotions_outlined), findsNothing);
expect(find.byIcon(Icons.alternate_email_rounded), findsNothing);
await tester.enterText(find.byKey(const Key('comment-input')), 'Nice');
tester.view.viewInsets = const FakeViewPadding(bottom: 180);
await tester.pump();
await tester.pump(const Duration(milliseconds: 200));
expect(tester.getSize(composer).height, closeTo(844 / 2, 0.1));
expect(tester.getBottomLeft(composer).dy, closeTo(844 - 180, 0.1));
expect(
tester
.widget<TextButton>(find.byKey(const Key('comment-post')))
.onPressed,
isNotNull,
);
final editableDuringKeyboard = tester.state<EditableTextState>(
find.descendant(
of: find.byKey(const Key('comment-input')),
matching: find.byType(EditableText),
),
);
expect(identical(editableAfter, editableDuringKeyboard), isTrue);
expect(tester.getBottomLeft(sheet).dy, closeTo(844 - 180, 0.1));
expect(
tester.widget<ListView>(list).padding?.resolve(TextDirection.ltr).bottom,
greaterThanOrEqualTo(180 + 44),
);
expect(
tester.getBottomLeft(find.byKey(const Key('comment-input'))).dy,
tester.getBottomLeft(composerInput).dy,
lessThanOrEqualTo(844 - 180),
);
expect(
tester
.widget<TextField>(find.byKey(const Key('comment-input')))
.focusNode
?.hasFocus,
isTrue,
);
expect(tester.getRect(underlyingSheet), sheetRect);
await tester.tapAt(const Offset(20, 20));
await tester.pump();
expect(tester.widget<TextField>(hiddenInput).focusNode?.hasFocus, isFalse);
tester.view.viewInsets = const FakeViewPadding();
await tester.pumpAndSettle();
expect(composer, findsNothing);
expect(commentsSheet, findsOneWidget);
expect(list, findsOneWidget);
expect(find.byKey(const Key('comment-count-label')), findsOneWidget);
expect(find.byKey(const Key('comment-composer')), findsOneWidget);
expect(find.byKey(const Key('comment-input-placeholder')), findsOneWidget);
expect(find.byKey(const Key('comment-input')), findsNothing);
expect(tester.getSize(sheet).height, closeTo(heightBeforeKeyboard, 0.1));
expect(tester.getSize(sheet).height, lessThan(520));
final editableAfterKeyboard = tester.state<EditableTextState>(
find.descendant(
of: hiddenInput,
matching: find.byType(EditableText, skipOffstage: false),
skipOffstage: false,
),
);
expect(identical(editableAfter, editableAfterKeyboard), isTrue);
expect(tester.widget<TextField>(hiddenInput).controller?.text, 'Nice');
expect(tester.widget<TextField>(hiddenInput).focusNode?.hasFocus, isFalse);
expect(placeholder, findsOneWidget);
expect(tester.getRect(commentsSheet), sheetRect);
expect(tester.getSize(commentsSheet).height, closeTo(sheetHeight, 0.1));
});
testWidgets('should_dismiss_from_outside_in_two_steps', (tester) async {
testWidgets('should_close_composer_before_comment_panel_from_outside', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
@@ -446,37 +369,20 @@ void main() {
await _pumpSheet(tester, repository: repository);
final sheet = find.byKey(const Key('player-comments-sheet'));
final input = find.byKey(const Key('comment-input'));
final hiddenInput = find.byKey(
const Key('comment-input'),
skipOffstage: false,
);
expect(
find.byKey(const Key('player-comments-dismiss-layer')),
findsOneWidget,
);
tester.view.viewInsets = const FakeViewPadding(bottom: 180);
await tester.tap(find.byKey(const Key('comment-input-placeholder')));
await tester.pumpAndSettle();
expect(sheet, findsOneWidget);
expect(input, findsOneWidget);
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isTrue);
await tester.tap(find.byKey(const Key('comment-list')));
await tester.pump();
expect(sheet, findsOneWidget);
expect(find.byKey(const Key('comment-composer-sheet')), findsOneWidget);
await tester.tapAt(const Offset(20, 20));
await tester.pump();
expect(sheet, findsOneWidget);
expect(tester.widget<TextField>(hiddenInput).focusNode?.hasFocus, isFalse);
expect(find.byKey(const Key('comment-input-placeholder')), findsOneWidget);
expect(input, findsNothing);
tester.view.viewInsets = const FakeViewPadding();
await tester.pumpAndSettle();
expect(sheet, findsOneWidget);
expect(find.byKey(const Key('comment-composer-sheet')), findsNothing);
await tester.tapAt(const Offset(20, 20));
await tester.pumpAndSettle();
@@ -694,22 +600,18 @@ void main() {
await tester.tap(find.byKey(const Key('comment-input-placeholder')));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('comment-input')),
find.byKey(const Key('comment-composer-input')),
'hello badword',
);
await tester.pump();
await tester.tap(find.byKey(const Key('comment-post')));
await tester.tap(find.byKey(const Key('comment-composer-post')));
await tester.pumpAndSettle();
FocusManager.instance.primaryFocus?.unfocus();
await tester.pumpAndSettle();
expect(find.byKey(const Key('comment-composer-sheet')), findsNothing);
expect(find.text('hello ***'), findsOneWidget);
expect(find.text('Under review'), findsOneWidget);
expect(haptics.lightImpacts, 1);
final field = tester.widget<TextField>(
find.byKey(const Key('comment-input'), skipOffstage: false),
);
expect(field.controller?.text, isEmpty);
expect(find.byKey(const Key('comment-input-placeholder')), findsOneWidget);
});
testWidgets('should_send_parent_id_when_replying_to_top_level_comment', (
@@ -731,16 +633,19 @@ void main() {
await _pumpSheet(tester, repository: repository);
await tester.tap(find.byKey(const Key('comment-reply-10')));
await tester.pump();
await tester.pumpAndSettle();
expect(find.byKey(const Key('comment-composer-sheet')), findsOneWidget);
expect(find.textContaining('Replying to'), findsOneWidget);
await tester.enterText(find.byKey(const Key('comment-input')), 'My reply');
await tester.enterText(
find.byKey(const Key('comment-composer-input')),
'My reply',
);
await tester.pump();
await tester.tap(find.byKey(const Key('comment-post')));
await tester.tap(find.byKey(const Key('comment-composer-post')));
await tester.pumpAndSettle();
expect(repository.publishCalls.single.parentId, '10');
FocusManager.instance.primaryFocus?.unfocus();
await tester.pumpAndSettle();
expect(find.byKey(const Key('comment-composer-sheet')), findsNothing);
expect(find.text('My reply'), findsOneWidget);
});
+5 -4
View File
@@ -250,11 +250,12 @@ void main() {
expect(find.text('No comments yet.'), findsOneWidget);
await tester.tap(find.byKey(const Key('comment-input-placeholder')));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('comment-input')), 'Nice demo');
await tester.enterText(
find.byKey(const Key('comment-composer-input')),
'Nice demo',
);
await tester.pump();
await tester.tap(find.byKey(const Key('comment-post')));
await tester.pumpAndSettle();
FocusManager.instance.primaryFocus?.unfocus();
await tester.tap(find.byKey(const Key('comment-composer-post')));
await tester.pumpAndSettle();
expect(find.text('Nice demo'), findsOneWidget);
await tester.binding.handlePopRoute();