findRenderObject()
얻어온 후 RenderBox
로 Type Casting을 해주면 얻을 수 있다.Code:
final _widgetKey = GlobalKey();
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
final widgetHeight = (_widgetKey.currentContext!
.findRenderObject() as RenderBox)
.size
.height;
print('widgetHeight: $widgetHeight');
});
}
Widget build(BuildContext context) {
...
CustomWidget(
key: _widgetKey,
child: ...,
),
...
}
Result:
widgetHeight: 76.0
Code:
final _widgetKey = GlobalKey();
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
final appBarHeight = (_widgetKey.currentContext!
.findRenderObject() as RenderBox)
.size
.height;
print('appBarHeight: $appBarHeight');
});
}
Widget build(BuildContext context) {
...
CustomScrollView(
controller: _controller,
slivers: [
SliverCustomWidget(
key: _widgetKey,
child: ...,
),
...
],
);
}
Result:
_TypeError (type '_RenderSliverPinnedPersistentHeaderForWidgets' is not a subtype of type 'RenderBox' in type cast)
Casting 에러가 난다..
Cause:
참고: [stack overflow] type 'RenderSliverList' is not a subtype of type 'RenderBox' in type cast - Flutter
Sliver 위젯은 RenderBox
가 아니라 RenderSliver
였다.
getAbsoluteSize()
와 getAbsoluteSizeRelativeToOrigin
가 있었다.Widget과 똑같은 내용
Sliver 특정 내용
getAbsoluteSize()
를 이용한다.code:
WidgetsBinding.instance.addPostFrameCallback((_) {
final appBarHeight =
(_appBarKey.currentContext!.findRenderObject() as RenderSliver)
.getAbsoluteSize()
.height;
print('appBarHeight: $appBarHeight');
});
Result:
appBarHeight: 76.0
full code:
final _widgetKey = GlobalKey();
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
final appBarHeight =
(_appBarKey.currentContext!.findRenderObject() as RenderSliver)
.getAbsoluteSize()
.height;
print('appBarHeight: $appBarHeight');
});
}
Widget build(BuildContext context) {
...
CustomScrollView(
controller: _controller,
slivers: [
SliverAppBar(
key: _widgetKey,
child: ...,
),
...
],
);
}