Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get widget's absolute coordinates on a screen in Flutter?

Tags:

flutter

dart

How to get widget's absolute coordinates on a screen in Flutter?

Or its offset in a parent

Example:

import 'package:flutter/material.dart';  void main() => runApp(MyApp());  class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return MaterialApp(       title: 'Simple app',       theme: ThemeData(         primarySwatch: Colors.blue,       ),       home: SimpleScreen(         title: 'Simple screen',       ),     );   } }  class SimpleScreen extends StatefulWidget {   final String title;    SimpleScreen({Key key, this.title}) : super(key: key);    @override   _SimpleScreenState createState() => new _SimpleScreenState(); }  class _SimpleScreenState extends State<SimpleScreen> {   @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: Text(widget.title),       ),       body: Center(         child: Container(           width: 48.0,           height: 48.0,           color: Colors.blue,         ),       ),     );   } } 

Goal is to get Container's offset in parent. If Center's size is 48.0, then Container's offset in Center's parent/root

Condition: you don't know the wrapper layout (it's a library widget), should be flexible, without hardcoded values

Thanks

like image 665
Ihor Klimov Avatar asked May 13 '18 12:05

Ihor Klimov


People also ask

How do you get the offset of a widget Flutter?

For the widget's offset position, call RenderBox 's localToGlobal method whose return type is Offset . The Offset class has some properties. For getting the offset position in x and y axis, you can read the dx and dy properties respectively. The returned offset is the top left position of the widget.

How do you get the touch position in Flutter?

You can add a GestureDetector as parent of stack, and register onTapDownDetails listener. This should call your listener on every tapdown event, with global offset of the tap in TapDownDetails parameter of the your listener. Here is sample code demonstrating the same.

How do you use draggable in Flutter?

Draggable is a Flutter widget that you can drag or move around. As soon as the user click and starts dragging the Draggable widget, a new feedback widget appears and follows the user's finger or mouse pointer. When the user lifts the finger or mouse pointer, the feedback widget disappears.

How do I position widgets in Flutter?

Here's how you do it:Step 1: Wrap the Stack's child widget inside the Position widget. Step 2: Inside the Position widget, add the top , right , bottom , left property and give it a value. For example, setting top:15 and right:0 will position a widget on the top right of your screen with 15 px space from the top.


2 Answers

You can use this extension I wrote (requires Dart 2.6):

extension GlobalKeyExtension on GlobalKey {   Rect? get globalPaintBounds {     final renderObject = currentContext?.findRenderObject();     final translation = renderObject?.getTransformTo(null).getTranslation();     if (translation != null && renderObject?.paintBounds != null) {       final offset = Offset(translation.x, translation.y);       return renderObject!.paintBounds.shift(offset);     } else {       return null;     }   } }  

Example how to use it:

final containerKey = GlobalKey();  Container(   key: containerKey,   width: 100,   height: 50, )  void printWidgetPosition() {   print('absolute coordinates on screen: ${containerKey.globalPaintBounds}'); } 
like image 182
vovahost Avatar answered Oct 06 '22 00:10

vovahost


Maybe this simple widget is what you want : rect_getter 0.0.1

This is a widget provide a simple way to get child's rectangle information after rendered.

demo

like image 40
debuggerx Avatar answered Oct 06 '22 00:10

debuggerx