class _SampleState extends State<Sample> {
GlobalKey key = GlobalKey();
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
RenderBox box = key.currentContext!.findRenderObject() as RenderBox;
Offset position = box.localToGlobal(Offset.zero);
final distanceFromBottom =
MediaQuery.of(context).size.height - (box.size.height + position.dy);
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
key: key, // element which uses the key
height: 100,
width: 100,
),
),
);
}
}
Use the key on the element you need to calculate the distance from.
0 Comments