Skip to content
Snippets Groups Projects
game_board.dart 4.06 KiB
Newer Older
Benoît Harrault's avatar
Benoît Harrault committed
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';

Benoît Harrault's avatar
Benoît Harrault committed
import 'package:suguru/config/application_config.dart';
Benoît Harrault's avatar
Benoît Harrault committed
import 'package:suguru/cubit/activity/activity_cubit.dart';
import 'package:suguru/models/activity/cell_location.dart';
import 'package:suguru/models/activity/activity.dart';
import 'package:suguru/ui/widgets/game/cell.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ActivityCubit, ActivityState>(
      builder: (BuildContext context, ActivityState activityState) {
        final Activity currentActivity = activityState.currentActivity;

        final Color borderColor = Theme.of(context).colorScheme.onSurface;

        final Size size = MediaQuery.of(context).size;
        final double width = size.width;
        final double height = size.height;

Benoît Harrault's avatar
Benoît Harrault committed
        final String theme =
            currentActivity.activitySettings.get(ApplicationConfig.parameterCodeColorTheme);

        final Container board = Container(
Benoît Harrault's avatar
Benoît Harrault committed
          margin: const EdgeInsets.all(2),
          padding: const EdgeInsets.all(2),
          decoration: BoxDecoration(
            color: borderColor,
            borderRadius: BorderRadius.circular(2),
            border: Border.all(
              color: borderColor,
              width: 2,
            ),
          ),
          child: Column(
            children: [
              Table(
                defaultColumnWidth: const IntrinsicColumnWidth(),
                children: [
                  for (int row = 0; row < currentActivity.board.boardSizeVertical; row++)
Benoît Harrault's avatar
Benoît Harrault committed
                    TableRow(
                      children: [
                        for (int col = 0;
                            col < currentActivity.board.boardSizeHorizontal;
                            col++)
Benoît Harrault's avatar
Benoît Harrault committed
                          Column(
                            children: [
                              CellWidget(
                                cell: currentActivity.board.get(CellLocation.go(row, col)),
Benoît Harrault's avatar
Benoît Harrault committed
                                theme: theme,
Benoît Harrault's avatar
Benoît Harrault committed
                                hasBlockBorderBottom: currentActivity.board
                                        .get(CellLocation.go(row, col))
                                        .blockId !=
Benoît Harrault's avatar
Benoît Harrault committed
                                    currentActivity.board
Benoît Harrault's avatar
Benoît Harrault committed
                                        .get(CellLocation.go(row + 1, col))
                                        .blockId,
                                hasBlockBorderLeft: currentActivity.board
                                        .get(CellLocation.go(row, col))
                                        .blockId !=
Benoît Harrault's avatar
Benoît Harrault committed
                                    currentActivity.board
Benoît Harrault's avatar
Benoît Harrault committed
                                        .get(CellLocation.go(row, col - 1))
                                        .blockId,
                                hasBlockBorderRight: currentActivity.board
                                        .get(CellLocation.go(row, col))
                                        .blockId !=
                                    currentActivity.board
                                        .get(CellLocation.go(row, col + 1))
                                        .blockId,
                                hasBlockBorderTop: currentActivity.board
                                        .get(CellLocation.go(row, col))
                                        .blockId !=
                                    currentActivity.board
                                        .get(CellLocation.go(row - 1, col))
                                        .blockId,

        return ConstrainedBox(
          constraints: BoxConstraints.tightFor(
            width: width,
            height: height * .6,
          ),
          child: FittedBox(
            child: board,
          ), //Text
        );