merge: 合并 K86.17 输入弹窗收起规则修复
This commit is contained in:
commit
bc971c17d5
2 files changed
+36
-40
No files matched your search
@@ -35,21 +35,16 @@ class CommentComposerSheet extends StatefulWidget {
|
||||
State<CommentComposerSheet> createState() => _CommentComposerSheetState();
|
||||
}
|
||||
|
||||
class _CommentComposerSheetState extends State<CommentComposerSheet>
|
||||
with WidgetsBindingObserver {
|
||||
class _CommentComposerSheetState extends State<CommentComposerSheet> {
|
||||
final _input = TextEditingController();
|
||||
final _inputFocus = FocusNode();
|
||||
final _keyboardInset = KeyboardInsetNotifier.instance;
|
||||
bool _keyboardWasVisible = false;
|
||||
bool _hadFocus = false;
|
||||
bool _closing = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_input.addListener(_onInputChanged);
|
||||
_inputFocus.addListener(_onFocusChanged);
|
||||
_keyboardInset.addListener(_onKeyboardInsetChanged);
|
||||
widget.controller.addListener(_onControllerChanged);
|
||||
unawaited(_keyboardInset.start());
|
||||
@@ -57,9 +52,7 @@ class _CommentComposerSheetState extends State<CommentComposerSheet>
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
_input.removeListener(_onInputChanged);
|
||||
_inputFocus.removeListener(_onFocusChanged);
|
||||
_keyboardInset.removeListener(_onKeyboardInsetChanged);
|
||||
widget.controller.removeListener(_onControllerChanged);
|
||||
_input.dispose();
|
||||
@@ -75,36 +68,9 @@ class _CommentComposerSheetState extends State<CommentComposerSheet>
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
|
||||
void _onFocusChanged() {
|
||||
if (!mounted) return;
|
||||
final hasFocus = _inputFocus.hasFocus;
|
||||
if (_hadFocus && !hasFocus) {
|
||||
_closeSheet();
|
||||
}
|
||||
_hadFocus = hasFocus;
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
void _onKeyboardInsetChanged() {
|
||||
if (!mounted) return;
|
||||
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(
|
||||
@@ -254,10 +220,6 @@ class _CommentComposerSheetState extends State<CommentComposerSheet>
|
||||
focusedBorder: InputBorder.none,
|
||||
),
|
||||
onTap: signedIn ? null : _authenticate,
|
||||
onTapOutside: (_) {
|
||||
_inputFocus.unfocus();
|
||||
_closeSheet();
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
@@ -387,7 +387,8 @@ void main() {
|
||||
|
||||
tester.view.viewInsets = const FakeViewPadding();
|
||||
await tester.pumpAndSettle();
|
||||
expect(composer, findsNothing);
|
||||
expect(composer, findsOneWidget);
|
||||
expect(tester.getBottomLeft(composer).dy, closeTo(844, 0.1));
|
||||
expect(commentsSheet, findsOneWidget);
|
||||
expect(list, findsOneWidget);
|
||||
expect(placeholder, findsOneWidget);
|
||||
@@ -395,6 +396,35 @@ void main() {
|
||||
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', (
|
||||
tester,
|
||||
) async {
|
||||
@@ -430,6 +460,10 @@ void main() {
|
||||
|
||||
final sheetRect = tester.getRect(sheet);
|
||||
final composerRect = tester.getRect(composer);
|
||||
await tester.tap(find.byKey(const Key('comment-composer-input')));
|
||||
await tester.pump();
|
||||
expect(composer, findsOneWidget);
|
||||
|
||||
await tester.tapAt(
|
||||
Offset(sheetRect.center.dx, (sheetRect.top + composerRect.top) / 2),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user