fix(player): 收起键盘不关闭评论输入弹窗
This commit is contained in:
1 parent
f9f77090d9
commit
cb58f649c1
2 files changed
+36
-40
No files matched your search
@@ -35,21 +35,16 @@ class CommentComposerSheet extends StatefulWidget {
|
|||||||
State<CommentComposerSheet> createState() => _CommentComposerSheetState();
|
State<CommentComposerSheet> createState() => _CommentComposerSheetState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _CommentComposerSheetState extends State<CommentComposerSheet>
|
class _CommentComposerSheetState extends State<CommentComposerSheet> {
|
||||||
with WidgetsBindingObserver {
|
|
||||||
final _input = TextEditingController();
|
final _input = TextEditingController();
|
||||||
final _inputFocus = FocusNode();
|
final _inputFocus = FocusNode();
|
||||||
final _keyboardInset = KeyboardInsetNotifier.instance;
|
final _keyboardInset = KeyboardInsetNotifier.instance;
|
||||||
bool _keyboardWasVisible = false;
|
|
||||||
bool _hadFocus = false;
|
|
||||||
bool _closing = false;
|
bool _closing = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance.addObserver(this);
|
|
||||||
_input.addListener(_onInputChanged);
|
_input.addListener(_onInputChanged);
|
||||||
_inputFocus.addListener(_onFocusChanged);
|
|
||||||
_keyboardInset.addListener(_onKeyboardInsetChanged);
|
_keyboardInset.addListener(_onKeyboardInsetChanged);
|
||||||
widget.controller.addListener(_onControllerChanged);
|
widget.controller.addListener(_onControllerChanged);
|
||||||
unawaited(_keyboardInset.start());
|
unawaited(_keyboardInset.start());
|
||||||
@@ -57,9 +52,7 @@ class _CommentComposerSheetState extends State<CommentComposerSheet>
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
WidgetsBinding.instance.removeObserver(this);
|
|
||||||
_input.removeListener(_onInputChanged);
|
_input.removeListener(_onInputChanged);
|
||||||
_inputFocus.removeListener(_onFocusChanged);
|
|
||||||
_keyboardInset.removeListener(_onKeyboardInsetChanged);
|
_keyboardInset.removeListener(_onKeyboardInsetChanged);
|
||||||
widget.controller.removeListener(_onControllerChanged);
|
widget.controller.removeListener(_onControllerChanged);
|
||||||
_input.dispose();
|
_input.dispose();
|
||||||
@@ -75,36 +68,9 @@ class _CommentComposerSheetState extends State<CommentComposerSheet>
|
|||||||
if (mounted) setState(() {});
|
if (mounted) setState(() {});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onFocusChanged() {
|
|
||||||
if (!mounted) return;
|
|
||||||
final hasFocus = _inputFocus.hasFocus;
|
|
||||||
if (_hadFocus && !hasFocus) {
|
|
||||||
_closeSheet();
|
|
||||||
}
|
|
||||||
_hadFocus = hasFocus;
|
|
||||||
setState(() {});
|
|
||||||
}
|
|
||||||
|
|
||||||
void _onKeyboardInsetChanged() {
|
void _onKeyboardInsetChanged() {
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() {});
|
setState(() {});
|
||||||
_syncKeyboardVisibility(_keyboardHeight(context));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void didChangeMetrics() {
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
||||||
if (!mounted) return;
|
|
||||||
_syncKeyboardVisibility(_keyboardHeight(context));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void _syncKeyboardVisibility(double keyboardHeight) {
|
|
||||||
if (keyboardHeight > 0) {
|
|
||||||
_keyboardWasVisible = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (_keyboardWasVisible) _closeSheet();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double _keyboardHeight(BuildContext context) => math.max(
|
double _keyboardHeight(BuildContext context) => math.max(
|
||||||
@@ -254,10 +220,6 @@ class _CommentComposerSheetState extends State<CommentComposerSheet>
|
|||||||
focusedBorder: InputBorder.none,
|
focusedBorder: InputBorder.none,
|
||||||
),
|
),
|
||||||
onTap: signedIn ? null : _authenticate,
|
onTap: signedIn ? null : _authenticate,
|
||||||
onTapOutside: (_) {
|
|
||||||
_inputFocus.unfocus();
|
|
||||||
_closeSheet();
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
|
|||||||
@@ -387,7 +387,8 @@ void main() {
|
|||||||
|
|
||||||
tester.view.viewInsets = const FakeViewPadding();
|
tester.view.viewInsets = const FakeViewPadding();
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
expect(composer, findsNothing);
|
expect(composer, findsOneWidget);
|
||||||
|
expect(tester.getBottomLeft(composer).dy, closeTo(844, 0.1));
|
||||||
expect(commentsSheet, findsOneWidget);
|
expect(commentsSheet, findsOneWidget);
|
||||||
expect(list, findsOneWidget);
|
expect(list, findsOneWidget);
|
||||||
expect(placeholder, findsOneWidget);
|
expect(placeholder, findsOneWidget);
|
||||||
@@ -395,6 +396,35 @@ void main() {
|
|||||||
expect(tester.getSize(commentsSheet).height, closeTo(sheetHeight, 0.1));
|
expect(tester.getSize(commentsSheet).height, closeTo(sheetHeight, 0.1));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('should_keep_composer_open_after_input_loses_focus', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final repository = FakeCommentRepository(
|
||||||
|
pages: [
|
||||||
|
CommentPage(
|
||||||
|
comments: [commentFixture(id: '10', content: 'A comment')],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
await _pumpSheet(tester, repository: repository);
|
||||||
|
await tester.tap(find.byKey(const Key('comment-input-placeholder')));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final composer = find.byKey(const Key('comment-composer-sheet'));
|
||||||
|
final input = find.byKey(const Key('comment-composer-input'));
|
||||||
|
expect(composer, findsOneWidget);
|
||||||
|
|
||||||
|
tester.widget<TextField>(input).focusNode?.unfocus();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(composer, findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(input);
|
||||||
|
await tester.pump();
|
||||||
|
expect(composer, findsOneWidget);
|
||||||
|
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('should_close_composer_before_comment_panel_from_outside', (
|
testWidgets('should_close_composer_before_comment_panel_from_outside', (
|
||||||
tester,
|
tester,
|
||||||
) async {
|
) async {
|
||||||
@@ -430,6 +460,10 @@ void main() {
|
|||||||
|
|
||||||
final sheetRect = tester.getRect(sheet);
|
final sheetRect = tester.getRect(sheet);
|
||||||
final composerRect = tester.getRect(composer);
|
final composerRect = tester.getRect(composer);
|
||||||
|
await tester.tap(find.byKey(const Key('comment-composer-input')));
|
||||||
|
await tester.pump();
|
||||||
|
expect(composer, findsOneWidget);
|
||||||
|
|
||||||
await tester.tapAt(
|
await tester.tapAt(
|
||||||
Offset(sheetRect.center.dx, (sheetRect.top + composerRect.top) / 2),
|
Offset(sheetRect.center.dx, (sheetRect.top + composerRect.top) / 2),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user