Skip to content

Commit eddd1d1

Browse files
committed
正确获取 container 高度
1 parent a680455 commit eddd1d1

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

example/ios/Runner/NativeViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ - (void)viewDidLoad {
4343

4444
CGFloat originX = 20;
4545
CGFloat originY = 20 + 44 + 40;
46-
CGFloat flutterContainerHeight = 80;
46+
CGFloat flutterContainerHeight = 280;
4747
CGFloat spaceY = 20;
4848
// CGFloat height = self.view.bounds.size.height - originY - flutterContainerHeight;
4949
// _baichuanWebView = [[WKWebView alloc] initWithFrame:CGRectMake(originX, originY, width, height)];
@@ -89,7 +89,7 @@ - (void)viewDidLoad {
8989
make.bottom.equalTo(self.view.mas_bottom).with.offset(-spaceY);
9090
make.right.mas_equalTo(self.view.mas_right).with.offset(-originX);
9191
// make.height.mas_greaterThanOrEqualTo(flutterContainerHeight);
92-
make.height.mas_equalTo(80);
92+
make.height.mas_equalTo(flutterContainerHeight);
9393
}];
9494
[self.flutterContainer.view mas_updateConstraints:^(MASConstraintMaker *make) {
9595
// make.top.mas_equalTo(self.view.mas_bottom).with.offset(-(flutterContainerHeight+spaceY));

example/lib/case/show_dialog_demo.dart

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,34 @@ class ShowDialogDemo extends StatefulWidget {
99
class _ShowDialogDemoState extends State<ShowDialogDemo> {
1010
@override
1111
final GlobalKey _scaffoldKey = GlobalKey();
12+
final GlobalKey _containerKey = GlobalKey();
1213
double _containerHeight = 0;
1314

15+
@override
16+
void initState() {
17+
super.initState();
18+
19+
WidgetsBinding.instance.addPostFrameCallback((_) {
20+
final RenderBox renderBox = _scaffoldKey.currentContext?.findRenderObject() as RenderBox;
21+
double newHeight = renderBox.size.height;
22+
if (newHeight != _containerHeight) {
23+
_containerHeight = newHeight;
24+
print("_containerHeight Height: ${_containerHeight}");
25+
}
26+
});
27+
}
28+
1429
double _getContainerHeight() {
15-
final RenderBox renderBox = _scaffoldKey.currentContext?.findRenderObject() as RenderBox;
30+
RenderBox renderBox = _scaffoldKey.currentContext?.findRenderObject() as RenderBox;
1631
double newHeight = renderBox.size.height;
17-
if (newHeight != _containerHeight) {
18-
_containerHeight = newHeight;
19-
print("_containerHeight Height: ${_containerHeight}");
20-
}
32+
// if (newHeight != _containerHeight) {
33+
// _containerHeight = newHeight;
34+
print("_scaffoldHeight Height: ${_containerHeight}");
35+
// }
36+
37+
renderBox = _containerKey.currentContext?.findRenderObject() as RenderBox;
38+
newHeight = renderBox.size.height;
39+
print("_containerHeight Height: ${newHeight}");
2140

2241
return _containerHeight;
2342
}
@@ -26,15 +45,26 @@ class _ShowDialogDemoState extends State<ShowDialogDemo> {
2645
print('flutter build');
2746

2847
return Scaffold(
48+
key: _scaffoldKey,
2949
backgroundColor: Colors.red,
3050
// appBar: AppBar(
3151
// title: Text('TextField Example'),
3252
// ),
3353
body: Column(
54+
mainAxisAlignment: MainAxisAlignment.end,
3455
children: [
3556
Container(
36-
key: _scaffoldKey,
37-
child: TextField(
57+
color: Colors.blue,
58+
key: _containerKey,
59+
child: Column(
60+
mainAxisAlignment: MainAxisAlignment.end,
61+
children: [
62+
Text('Widget 1'),
63+
SizedBox(height: 10),
64+
Text('Widget 2'),
65+
SizedBox(height: 10),
66+
Text('Widget 3'),
67+
TextField(
3868
// key: _scaffoldKey,
3969
// expands: true,
4070
maxLines: 150,
@@ -59,6 +89,8 @@ class _ShowDialogDemoState extends State<ShowDialogDemo> {
5989
),
6090
),
6191
),
92+
],
93+
),
6294
),
6395
],
6496
),

0 commit comments

Comments
 (0)