Skip to content

Commit 121db75

Browse files
committed
解决keyboardWillShow多次调用的 bug
1 parent c5fd8f7 commit 121db75

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

example/ios/Runner/NativeViewController.m

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ - (void)viewDidLoad {
120120
selector:@selector(keyboardWillHide:)
121121
name:UIKeyboardWillHideNotification
122122
object:nil];
123+
NSLog(@"注册键盘通知 updateFlutterContainerHeight");
123124
}
124125

125126
- (void)updateFlutterContainerHeight:(CGFloat)height {
@@ -129,6 +130,7 @@ - (void)updateFlutterContainerHeight:(CGFloat)height {
129130
NSLog(@"start updateFlutterContainerHeight frame.origin.y:%f, frame.size.height:%f", frame.origin.y, frame.size.height);
130131
CGFloat spaceY = height - frame.size.height;
131132
frame.origin.y -= spaceY;
133+
_flutterContainerViewOriginY = frame.origin.y;
132134
frame.size.height += spaceY;
133135
NSLog(@"end updateFlutterContainerHeight frame.origin.y:%f, frame.size.height:%f", frame.origin.y, frame.size.height);
134136
[self.flutterContainer.view setFrame:frame];
@@ -157,15 +159,15 @@ - (void)didMoveToParentViewController:(UIViewController *)parent {
157159
[super didMoveToParentViewController:parent];
158160
}
159161

160-
/*
161-
#pragma mark - Navigation
162-
163-
// In a storyboard-based application, you will often want to do a little preparation before navigation
164-
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
165-
// Get the new view controller using [segue destinationViewController].
166-
// Pass the selected object to the new view controller.
162+
- (void)showLog:(NSString *)log {
163+
// 格式化当前时间
164+
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
165+
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
166+
NSString *currentTimeString = [dateFormatter stringFromDate:[NSDate date]];
167+
168+
// 打印时间戳和执行时间
169+
NSLog(@"[%@] %@", currentTimeString, log);
167170
}
168-
*/
169171

170172
- (void)dealloc{
171173
NSLog(@"dealloc native controller%p", self.flutterContainer);
@@ -177,22 +179,21 @@ - (void)keyboardWillShow:(NSNotification *)notification {
177179
NSDictionary *info = [notification userInfo];
178180
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
179181
_keyboardHeight = keyboardSize.height;
180-
182+
181183
CGRect frame = self.flutterContainer.view.frame;
182-
frame.origin.y -= _keyboardHeight;
184+
[self showLog:[NSString stringWithFormat:@"keyboardWillShow start updateFlutterContainerHeight frame.origin.y:%f, frame.size.height:%f", frame.origin.y, frame.size.height]];
185+
frame.origin.y = _flutterContainerViewOriginY - _keyboardHeight;
183186
[self.flutterContainer.view setFrame:frame];
184-
185-
// NSLog(@"Keyboard will show. Height: %f", keyboardHeight);
186-
// 在这里可以对键盘弹起进行处理,比如调整界面布局
187+
[self showLog:[NSString stringWithFormat:@"keyboardWillShow end updateFlutterContainerHeight frame.origin.y:%f, frame.size.height:%f", frame.origin.y, frame.size.height]];
187188
}
188189

189190
- (void)keyboardWillHide:(NSNotification *)notification {
190191
CGRect frame = self.flutterContainer.view.frame;
192+
[self showLog:[NSString stringWithFormat:@"keyboardWillHide start updateFlutterContainerHeight frame.origin.y:%f, frame.size.height:%f", frame.origin.y, frame.size.height]];
191193
frame.origin.y += _keyboardHeight;
194+
_flutterContainerViewOriginY = frame.origin.y;
192195
[self.flutterContainer.view setFrame:frame];
193-
194-
// NSLog(@"Keyboard will hide.");
195-
// 在这里可以对键盘隐藏进行处理
196+
[self showLog:[NSString stringWithFormat:@"keyboardWillHide end updateFlutterContainerHeight frame.origin.y:%f, frame.size.height:%f", frame.origin.y, frame.size.height]];
196197
}
197198

198199
@end

example/lib/case/show_dialog_demo.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class _ShowDialogDemoState extends State<ShowDialogDemo> {
2222
}
2323

2424
void _getContainerHeight() {
25-
WidgetsBinding.instance.addPostFrameCallback((_) {
25+
final callback = (_) {
2626
// RenderBox renderBox = _scaffoldKey.currentContext?.findRenderObject() as RenderBox;
2727
// double newHeight = renderBox.size.height;
2828
// print("_scaffoldHeight Height: ${_containerHeight}");
@@ -34,15 +34,18 @@ class _ShowDialogDemoState extends State<ShowDialogDemo> {
3434
_channel.invokeMethod('updateHeight', {'height': newHeight});
3535
print("updateFlutterContainerHeight Height: ${newHeight}");
3636
}
37-
});
37+
38+
// WidgetsBinding.instance.addPostFrameCallback(callback);
39+
};
40+
WidgetsBinding.instance.addPostFrameCallback(callback);
3841
}
3942

4043
Widget build(BuildContext context) {
4144
print('flutter build');
4245

4346
return Scaffold(
4447
// key: _scaffoldKey,
45-
backgroundColor: Colors.red,
48+
backgroundColor: Colors.transparent,
4649
// appBar: AppBar(
4750
// title: Text('TextField Example'),
4851
// ),

0 commit comments

Comments
 (0)