I am trying to add an exploding effect to Inkwell Widget inside a GridView but I need to find its offset, I have tried this so far:
GridView.builder(
itemCount: 10,
itemBuilder: (ctx, ind) {
return InkWell(
onTap: () async {
try {
final RenderBox box = ctx.findRenderObject();
Offset position = box.localToGlobal(Offset.zero,
ancestor: context.findRenderObject()
);
double x = position.dx;
double y = position.dy;
double w = box.size.width;
Future.delayed(Duration(milliseconds: 100)).then(
(_) => _particleField.lineExplosion(x, y, w)
);
} catch (e) {
print(e);
}
},
child: Center(
child: Text("${ind + 1},000,000")
),
);
},
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 3
),
)
but I get this error:
type 'RenderSliverGrid' is not a subtype of type 'RenderBox'
Flutter GridView is a widget that is similar to a 2-D Array in any programming language. As the name suggests, a GridView Widget is used when we have to display something on a Grid. We can display images, text, icons, etc on GridView. We can implement GridView in various ways in Flutter :
The returned offset is the top left position of the widget. If you want to get the center of the widget, just add the size of the widget in the respective axis and divide the result with 2. You must be careful that you can only get the RenderBox after the widget has been rendered.
In Flutter, there are two ways of constructing an Offset. The first way is using the Offset constructor and passing in the x and y value like this: To demonstrate how to position an entity with an Offset, we’ll draw a red circle and position it 20px to the right from the point of origin.
In Flutter, the two GridView is mostly used. GridView.count () is used with some named parameters. The properties that we can use with GridView.count () are: crossAxisCount: It defines the number of columns in GridView. crossAxisSpacing: It defines the number of pixels between each child listed along the cross axis.
is this what you are looking for?
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);
}
}
Add a GlobalKey
as the key of the InkWell
itemBuilder: (ctx, ind) {
GlobalKey _key=GlobalKey();
return InkWell(
key:_key;
);
}
Then you can simply replace
final RenderBox box = _key.currentContext.findRenderObject();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With