flutter get global offset for widget when in customList how?
Assistant
You can use the key property of the widget to get its position using the findRenderObject() method and localToGlobal() method to convert its position to a global offset. Here's an example code snippet:
dart
User
answer above does not work when you scroll the list a bit. scrolled area is not considered. so how to get offset including scrolling position?
Assistant
class MyCustomList extends StatelessWidget{
final GlobalKey<_ListItemState> itemKey1 = GlobalKey();
final GlobalKey<_ListItemState> itemKey2 = GlobalKey();
@override
Widget build(BuildContext context) {
return Column(
children: [
ListItem(key: itemKey1),
ListItem(key: itemKey2),
ElevatedButton(
onPressed: () {
final RenderBox renderBox1 = itemKey1.currentContext.findRenderObject() as RenderBox;
final RenderBox renderBox2 = itemKey2.currentContext.findRenderObject() as RenderBox;
final Offset offset1 = renderBox1.localToGlobal(Offset.zero);
final Offset offset2 = renderBox2.localToGlobal(Offset.zero);
print('Item 1 has global position ${offset1.dx}, ${offset1.dy}');
print('Item 2 has global position ${offset2.dx}, ${offset2.dy}');
},
child: Text('Print item offsets'),
),
]
);
}
}
class ListItem extends StatefulWidget {
ListItem({Key? key}) : super(key: key);
@override
_ListItemState createState() => _ListItemState();
}
class _ListItemState extends State<ListItem> {
@override
Widget build(BuildContext context) {
return Container(
height: 50,
color: Colors.grey,
child: Text('List item'),
);
}
}
In this example, the MyCustomList widget has two ListItem widgets with different keys, and the ElevatedButton widget can be used to print the global offsets of each ListItem. The findRenderObject() method is used to find the render object of each ListItem, and localToGlobal() is used to convert the position of each ListItem from local to global coordinates.
To get the offset of a widget that takes into account the scrolling position of the list, you can use the Scrollable.of(context) method to get the nearest Scrollable ancestor of the widget. Then, you can use the position property of the Scrollable to get the current position of the scroll view, and adjust the global position of the widget using the scroll position. Here's an updated code snippet:
dart
class MyCustomList extends StatelessWidget {
final GlobalKey<_ListItemState> itemKey1 = GlobalKey();
final GlobalKey<_ListItemState> itemKey2 = GlobalKey();
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
ListItem(key: itemKey1),
ListItem(key: itemKey2),
ElevatedButton(
onPressed: () {
final RenderBox renderBox1 = itemKey1.currentContext!.findRenderObject() as RenderBox;
final RenderBox renderBox2 = itemKey2.currentContext!.findRenderObject() as RenderBox;
final ScrollableState scrollableState = Scrollable.of(context)!;
final ScrollPosition position = scrollableState.position;
final Offset offset1 = renderBox1.localToGlobal(Offset.zero, ancestor: scrollableState.context);
final Offset offset2 = renderBox2.localToGlobal(Offset.zero, ancestor: scrollableState.context);
print('Item 1 has global position ${offset1.dx}, ${offset1.dy - position.pixels}');
print('Item 2 has global position ${offset2.dx}, ${offset2.dy - position.pixels}');
},
child: Text('Print item offsets'),
),
]
),
);
}
}
class ListItem extends StatefulWidget {
ListItem({Key? key}) : super(key: key);
@override
_ListItemState createState() => _ListItemState();
}
class _ListItemState extends State<ListItem> {
@override
Widget build(BuildContext context) {
return Container(
height: 50,
color: Colors.grey,
child: Text('List item'),
);
}
}
In this updated code snippet, SingleChildScrollView is used as the parent widget of the Column to enable scrolling. The ancestor parameter of the localToGlobal() method is used to specify the scrollable context as the ancestor widget. Finally, the global position of each ListItem is adjusted by subtracting the current scroll position of the Scrollable, which is obtained from the ScrollPosition.pixels property. This should give you the correct global coordinates of the widgets even when the list has been scrolled.