orlikraf / flutter-hexagon

MIT License
30 stars 17 forks source link

Adding listener event handling #26

Closed Engid closed 7 months ago

Engid commented 7 months ago

Hi there! Thanks for making this awesome library, I just completed the redblobgames intro and figured I'd see if anyone had done this in flutter yet and was happy to see your work.

I am a flutter noob so please forgive me for asking, but what would be the best way to treat the hexagon cells of a hex grid as "buttons"? I feel like wrapping the whole grid in a listener would be more efficient, but I'm not sure where to get the coordinates of which child was touched. If there's a more ergonomic way, I'm having trouble finding it in the examples.

Thank you for your time!

Engid commented 7 months ago

For now I'm adding a simple button to each hex in the buildChild, like so:


class MyHexGrid extends StatelessWidget {
  const MyHexGrid({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        HexagonOffsetGrid.oddPointy(
          columns: 8,
          rows: 12,
          buildTile: (col, row) => HexagonWidgetBuilder(
            color: row.isEven ? Colors.yellow : Colors.orangeAccent,
            elevation: 2,
          ),
          buildChild: (col, row) {
            var infoStr = '$col, $row';
            var text = Text(infoStr, style: const TextStyle(fontSize: 28));

            return TextButton(
                onPressed: () {
                  print(infoStr);
                },
                child: text);
          },
        ),
      ],
    );
  }
}

I'll keep playing with it, but if you have suggestions please let me know! Thanks.