import 'package:flutter/material.dart'; import 'state.dart'; /// Board of a single game of tic tac toe class TTCGame extends StatelessWidget { const TTCGame({ super.key, required this.turn, required this.data, this.cellOnTapCallback, this.cellTextStyle, }); final TTCState turn; final List data; /// styling for text in cell final TextStyle? cellTextStyle; /// hook into end of turn cycle; final void Function(int)? cellOnTapCallback; @override Widget build(BuildContext context) { List cells = data .map((state) => Text( state != TTCState.empty ? state.name.toUpperCase() : "", style: cellTextStyle, )) .toList(); return GameHash( cellOnTapCallback: cellOnTapCallback, children: cells, ); } } class GameHash extends StatelessWidget { const GameHash({super.key, required this.children, this.cellOnTapCallback}); final List children; final void Function(int)? cellOnTapCallback; Border _genCellBorder( int index, { BorderSide borderSide = const BorderSide(), }) { return Border( top: index < 3 ? BorderSide.none : borderSide, bottom: index > 5 ? BorderSide.none : borderSide, left: [0, 3, 6].contains(index) ? BorderSide.none : borderSide, right: [2, 5, 8].contains(index) ? BorderSide.none : borderSide, ); } MapEntry _genCell(int index, Widget content) { return MapEntry( index, Container( decoration: BoxDecoration(border: _genCellBorder(index)), child: HashCell( stateSetCallback: () { if (cellOnTapCallback != null) { cellOnTapCallback!(index); } }, child: content, ), ), ); } @override Widget build(BuildContext context) { return GridView.count( crossAxisCount: 3, shrinkWrap: true, children: children.asMap().map(_genCell).values.toList(), ); } } class HashCell extends StatelessWidget { const HashCell({ super.key, required this.child, this.stateSetCallback, }); final Widget child; final VoidCallback? stateSetCallback; @override Widget build(BuildContext context) => InkWell( onTap: stateSetCallback, child: IgnorePointer( child: Center(child: child), ), ); }