Want to get offset of a widget inside a GridView flutter? Use the following example code to get offset of a widget inside a GridView flutter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( body: GridExample(), ), ); } } class GridExample extends StatelessWidget { @override Widget build(BuildContext context) { return GridView.builder( itemCount: 10, itemBuilder: (ctx, ind) { return _Item('${ind + 1},000,000'); }, gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, childAspectRatio: 3), ); } } class _Item extends StatelessWidget { _Item(this.title); String title; final GlobalKey _globalKey = GlobalKey(); @override Widget build(BuildContext context) { return InkWell( key: _globalKey, onTap: () async { final RenderBox referenceBox = _globalKey.currentContext.findRenderObject(); final position = referenceBox.localToGlobal(Offset.zero); final x = position.dx; final y = position.dy; final w = referenceBox.size.width; Future.delayed(Duration(milliseconds: 100)) .then((_) => _particleFieldLineExplosion(x, y, w)); }, child: Center(child: Text(title)), ); } _particleFieldLineExplosion(x, y, w) { print(x); print(y); print(w); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.