I\'m trying to get the height of widget but it prints the same values
I/flutter (19253): full height: 976.0 I/flutter (19253): safe height: 976.0 >
to get the size of the SafeArea
, you need to encapsulate inside a LayoutBuilder
, and use constraints.maxHeight
look at your own example modified:
I/flutter (20405): full Screen height: 683.4285714285714
I/flutter (20405): Screen height: 683.4285714285714
I/flutter (20405): Real safe height: 659.4285714285714
class _MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(body: _Body()),
);
}
}
class _Body extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('full Screen height: ${MediaQuery.of(context).size.height}');
return Container(
constraints: BoxConstraints.expand(),
decoration: BoxDecoration(color: Colors.red),
child: SafeArea(
child: _SafeHeightWidget(),
),
);
}
}
class _SafeHeightWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
print('Screen height: ${MediaQuery.of(context).size.height}');
print('Real safe height: ${constraints.maxHeight}');
return Container(
color: Colors.lightBlue,
);
},
);
}
}