Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get offset of a widget inside a GridView flutter

Tags:

flutter

dart

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'

like image 747
geekymano Avatar asked Jan 10 '20 11:01

geekymano


People also ask

What is flutter GridView widget?

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 :

How do I get the offset of a widget?

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.

How to create an offset in flutter?

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.

How to count the number of columns in GridView in flutter?

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.


2 Answers

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);
  }
}
like image 62
Kherel Avatar answered Oct 19 '22 10:10

Kherel


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();
like image 2
Guru Prasad mohapatra Avatar answered Oct 19 '22 09:10

Guru Prasad mohapatra