dart

Get element position from the bottom of the screen

Calculate the distance of the widget from the bottom of the screen

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.

Was this helpful?