fix(player): 修正评论输入浮层初始键盘高度与聚焦
This commit is contained in:
1 parent
6c72f8bf22
commit
a66cdb2e1b
2 files changed
+98
-10
No files matched your search
@@ -4,6 +4,7 @@ import 'dart:async';
|
|||||||
import 'dart:math' as math;
|
import 'dart:math' as math;
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import '../../../core/data/models/comment.dart';
|
import '../../../core/data/models/comment.dart';
|
||||||
@@ -44,6 +45,10 @@ class _CommentComposerSheetState extends State<CommentComposerSheet> {
|
|||||||
final _inputFocus = FocusNode();
|
final _inputFocus = FocusNode();
|
||||||
final _keyboardInset = KeyboardInsetNotifier.instance;
|
final _keyboardInset = KeyboardInsetNotifier.instance;
|
||||||
double _pinnedKeyboardInset = 0;
|
double _pinnedKeyboardInset = 0;
|
||||||
|
int _insetBaseline = 0;
|
||||||
|
bool _pinnedInitialized = false;
|
||||||
|
bool _insetReady = false;
|
||||||
|
bool _useChannelInset = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -51,9 +56,31 @@ class _CommentComposerSheetState extends State<CommentComposerSheet> {
|
|||||||
_input.addListener(_onInputChanged);
|
_input.addListener(_onInputChanged);
|
||||||
_keyboardInset.addListener(_onKeyboardInsetChanged);
|
_keyboardInset.addListener(_onKeyboardInsetChanged);
|
||||||
widget.controller.addListener(_onControllerChanged);
|
widget.controller.addListener(_onControllerChanged);
|
||||||
unawaited(_keyboardInset.start());
|
unawaited(_prepareInput());
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeDependencies() {
|
||||||
|
super.didChangeDependencies();
|
||||||
|
if (_pinnedInitialized) return;
|
||||||
|
_pinnedInitialized = true;
|
||||||
|
_pinnedKeyboardInset = MediaQuery.viewInsetsOf(context).bottom;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _prepareInput() async {
|
||||||
|
await _keyboardInset.start();
|
||||||
|
if (!mounted) return;
|
||||||
|
_insetBaseline = _keyboardInset.value;
|
||||||
|
_insetReady = true;
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
if (mounted) _inputFocus.requestFocus();
|
if (!mounted) return;
|
||||||
|
_inputFocus.requestFocus();
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (!mounted || _keyboardHeight(context) > 0) return;
|
||||||
|
unawaited(
|
||||||
|
SystemChannels.textInput.invokeMethod<void>('TextInput.show'),
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,14 +104,20 @@ class _CommentComposerSheetState extends State<CommentComposerSheet> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _onKeyboardInsetChanged() {
|
void _onKeyboardInsetChanged() {
|
||||||
if (!mounted) return;
|
if (!mounted || !_insetReady) return;
|
||||||
|
if (_keyboardInset.value != _insetBaseline) {
|
||||||
|
_useChannelInset = true;
|
||||||
|
}
|
||||||
setState(() {});
|
setState(() {});
|
||||||
}
|
}
|
||||||
|
|
||||||
double _keyboardHeight(BuildContext context) => math.max(
|
double _keyboardHeight(BuildContext context) {
|
||||||
MediaQuery.viewInsetsOf(context).bottom,
|
final viewInset = MediaQuery.viewInsetsOf(context).bottom;
|
||||||
_keyboardInset.value / MediaQuery.devicePixelRatioOf(context),
|
final channelInset = _useChannelInset
|
||||||
);
|
? _keyboardInset.value / MediaQuery.devicePixelRatioOf(context)
|
||||||
|
: 0.0;
|
||||||
|
return math.max(viewInset, channelInset);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _submit() async {
|
Future<void> _submit() async {
|
||||||
if (widget.controller.submitting) return;
|
if (widget.controller.submitting) return;
|
||||||
@@ -123,7 +156,10 @@ class _CommentComposerSheetState extends State<CommentComposerSheet> {
|
|||||||
final canPost = signedIn && hasContent && !widget.controller.submitting;
|
final canPost = signedIn && hasContent && !widget.controller.submitting;
|
||||||
final multiline = _input.text.contains('\n');
|
final multiline = _input.text.contains('\n');
|
||||||
|
|
||||||
return Stack(
|
return FocusScope(
|
||||||
|
key: const Key('comment-composer-focus-scope'),
|
||||||
|
autofocus: true,
|
||||||
|
child: Stack(
|
||||||
key: const Key('comment-composer-overlay'),
|
key: const Key('comment-composer-overlay'),
|
||||||
fit: StackFit.expand,
|
fit: StackFit.expand,
|
||||||
children: [
|
children: [
|
||||||
@@ -178,7 +214,9 @@ class _CommentComposerSheetState extends State<CommentComposerSheet> {
|
|||||||
Container(
|
Container(
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
color: AppColors.commentInput,
|
color: AppColors.commentInput,
|
||||||
borderRadius: BorderRadius.circular(_inputRadius),
|
borderRadius: BorderRadius.circular(
|
||||||
|
_inputRadius,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
child: TextField(
|
child: TextField(
|
||||||
key: const Key('comment-composer-input'),
|
key: const Key('comment-composer-input'),
|
||||||
@@ -227,7 +265,10 @@ class _CommentComposerSheetState extends State<CommentComposerSheet> {
|
|||||||
key: const Key('comment-composer-post'),
|
key: const Key('comment-composer-post'),
|
||||||
onPressed: canPost ? _submit : null,
|
onPressed: canPost ? _submit : null,
|
||||||
style: TextButton.styleFrom(
|
style: TextButton.styleFrom(
|
||||||
minimumSize: const Size(0, _postButtonHeight),
|
minimumSize: const Size(
|
||||||
|
0,
|
||||||
|
_postButtonHeight,
|
||||||
|
),
|
||||||
padding: const EdgeInsets.symmetric(
|
padding: const EdgeInsets.symmetric(
|
||||||
horizontal: 16,
|
horizontal: 16,
|
||||||
),
|
),
|
||||||
@@ -263,6 +304,7 @@ class _CommentComposerSheetState extends State<CommentComposerSheet> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:fruitshorts/core/data/models/comment.dart';
|
import 'package:fruitshorts/core/data/models/comment.dart';
|
||||||
import 'package:fruitshorts/core/data/models/drama.dart';
|
import 'package:fruitshorts/core/data/models/drama.dart';
|
||||||
@@ -33,6 +35,50 @@ const _drama = Drama(
|
|||||||
);
|
);
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
testWidgets('should_ignore_stale_keyboard_inset_when_composer_opens', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
const keyboardChannel = MethodChannel('com.stormg.lumadrama/keyboard');
|
||||||
|
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||||
|
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||||
|
keyboardChannel,
|
||||||
|
(call) async => call.method == 'get' ? 300 : null,
|
||||||
|
);
|
||||||
|
addTearDown(() {
|
||||||
|
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||||
|
keyboardChannel,
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
debugDefaultTargetPlatformOverride = null;
|
||||||
|
});
|
||||||
|
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);
|
||||||
|
await tester.tap(find.byKey(const Key('comment-input-placeholder')));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
final composer = find.byKey(const Key('comment-composer-sheet'));
|
||||||
|
final panel = find.byKey(const Key('comment-composer-panel'));
|
||||||
|
final input = find.byKey(const Key('comment-composer-input'));
|
||||||
|
expect(
|
||||||
|
find.byKey(const Key('comment-composer-focus-scope')),
|
||||||
|
findsOneWidget,
|
||||||
|
);
|
||||||
|
expect(tester.getSize(composer).height, inInclusiveRange(100, 150));
|
||||||
|
expect(
|
||||||
|
tester.getSize(panel).height,
|
||||||
|
closeTo(tester.getSize(composer).height, 1),
|
||||||
|
);
|
||||||
|
expect(tester.getBottomLeft(panel).dy, closeTo(844, 0.1));
|
||||||
|
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isTrue);
|
||||||
|
expect(tester.testTextInput.isVisible, isTrue);
|
||||||
|
debugDefaultTargetPlatformOverride = null;
|
||||||
|
});
|
||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
SharedPreferencesAsyncPlatform.instance =
|
SharedPreferencesAsyncPlatform.instance =
|
||||||
InMemorySharedPreferencesAsync.withData({
|
InMemorySharedPreferencesAsync.withData({
|
||||||
|
|||||||
Reference in new issue
Block a user