Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
42 kB
2
Indexable
Never
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sudoku_master/core/color_constants.dart';
import 'package:sudoku_master/providers/sudoku_provider.dart';
import 'package:sudoku_master/providers/theme_providers.dart';
import 'package:sudoku_master/screens/home_screen.dart';
import 'package:sudoku_master/screens/widget/custom_container.dart';

class SudokuBoardWidget extends StatelessWidget {
  const SudokuBoardWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final themeProvider = Provider.of<ThemeProvider>(context, listen: false);

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

    return Center(
      child: PopScope(
        canPop: false,
        onPopInvoked: (didPop) {
          _onPopInvoked(
            didPop,
            context,
            themeProvider,
          );
        },
        child: Scaffold(
          backgroundColor: themeProvider.themeMode == ThemeMode.dark
              ? ColorConstants.scaffoldColorDark
              : ColorConstants.scaffoldColorLight,
          appBar: AppBar(
            elevation: 1,
            leading: InkWell(
              onTap: () {
                _onPopInvoked(
                  false,
                  context,
                  themeProvider,
                );
              },
              child: Icon(
                Icons.arrow_back_sharp,
                color: themeProvider.themeMode == ThemeMode.dark
                    ? Colors.white
                    : Colors.black,
              ),
            ),
            title: Center(
              child: Text(
                'Sudoku Game',
                style: Theme.of(context).textTheme.headlineLarge?.copyWith(
                      fontSize: 22,
                      fontWeight: FontWeight.w500,
                      color: themeProvider.themeMode == ThemeMode.dark
                          ? Colors.white
                          : Colors.black,
                    ),
              ),
            ),
            backgroundColor: themeProvider.themeMode == ThemeMode.dark
                ? ColorConstants.primaryColorDark
                : ColorConstants.primaryColorLight.withOpacity(0.8),
            iconTheme: IconThemeData(
              color: themeProvider.themeMode == ThemeMode.dark
                  ? Colors.white
                  : Colors.black,
            ),
            actions: [
              Switch(
                activeColor: Colors.green,
                inactiveThumbColor: Colors.green,
                trackOutlineColor:
                    WidgetStateProperty.all(Colors.grey.withOpacity(0.7)),
                value: Provider.of<ThemeProvider>(context).themeMode ==
                    ThemeMode.dark,
                onChanged: (value) {
                  if (value) {
                    Provider.of<ThemeProvider>(context, listen: false)
                        .setThemeMode(ThemeMode.dark);
                  } else {
                    Provider.of<ThemeProvider>(context, listen: false)
                        .setThemeMode(ThemeMode.light);
                  }
                },
              ),
            ],
          ),
          body: Consumer<SudokuProvider>(
            builder: (context, sudokuBoardProvider, child) {
              // List<List<int?>> initialPuzzle = sudokuBoardProvider.initialPuzzle;

              return Column(
                children: [
                  SizedBox(height: sh * 0.025),
                  _timerRow(sudokuBoardProvider, context, themeProvider, sh),
                  const SizedBox(height: 8),
                  _bannerSection(context),
                  const SizedBox(height: 8),
                  _mainBoardWidget(sudokuBoardProvider, themeProvider),
                  SizedBox(height: sh * 0.03),
                  _numberInputUpperRow(sudokuBoardProvider),
                  _numberInputLowerRow(
                      sudokuBoardProvider, context, themeProvider),
                  _optionsRow(sudokuBoardProvider, context, themeProvider),
                  SizedBox(height: sh * 0.02),
                ],
              );
            },
          ),
        ),
      ),
    );
  }

  Widget _bannerSection(BuildContext context){
    return Container(
      height: MediaQuery.sizeOf(context).height * 0.065,
      width: MediaQuery.sizeOf(context).width,
      color: Colors.red,
      margin: const EdgeInsets.only(bottom: 8),
    );
  }

  void _onPopInvoked(
    didPop,
    BuildContext context,
    ThemeProvider themeProvider,
  ) {
    if (didPop) {
      return;
    }

    showModalBottomSheet(
      isDismissible: true,
      isScrollControlled: false,
      context: context,
      builder: (context) {
        return Container(
            width: MediaQuery.sizeOf(context).width,
            decoration: BoxDecoration(
                color: themeProvider.themeMode == ThemeMode.dark
                    ? ColorConstants.primaryColorDark
                    : ColorConstants.primaryColorLight,
                borderRadius: const BorderRadius.only(
                    topRight: Radius.circular(12),
                    topLeft: Radius.circular(12))),
            padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  'Want to exit?',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.w600,
                    color: themeProvider.themeMode == ThemeMode.dark
                        ? Colors.white
                        : Colors.black,
                  ),
                ),
                const SizedBox(height: 20),
                Text('You will lose all your progress.',
                    style: Theme.of(context).textTheme.headlineMedium?.copyWith(
                          fontWeight: FontWeight.w400,
                          fontSize: 16,
                          color: themeProvider.themeMode == ThemeMode.dark
                              ? Colors.white
                              : Colors.black,
                        )),
                const SizedBox(height: 12),
                Row(
                  children: [
                    Expanded(
                      child: InkWell(
                        splashColor: Colors.transparent,
                        highlightColor: Colors.transparent,
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                        child: Container(
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(8),
                            color: themeProvider.themeMode == ThemeMode.dark
                                ? Colors.black
                                : Colors.white,
                          ),
                          padding: const EdgeInsets.symmetric(
                              horizontal: 6, vertical: 12),
                          margin: const EdgeInsets.symmetric(
                              horizontal: 18, vertical: 8),
                          child: Center(
                            child: Text(
                              'No',
                              style: TextStyle(
                                color: themeProvider.themeMode == ThemeMode.dark
                                    ? Colors.white
                                    : Colors.black,
                                fontSize: 16,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: InkWell(
                        splashColor: Colors.transparent,
                        highlightColor: Colors.transparent,
                        onTap: () {
                          Navigator.pushReplacement(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => const HomeScreen()));
                        },
                        child: Container(
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(8),
                            color: Colors.red,
                          ),
                          padding: const EdgeInsets.symmetric(
                              horizontal: 6, vertical: 12),
                          margin: const EdgeInsets.symmetric(
                              horizontal: 18, vertical: 8),
                          child: const Center(
                            child: Text(
                              'Yes',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 16,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              ],
            ));
      },
    );
  }

  Padding _optionsRow(
    SudokuProvider sudokuBoardProvider,
    BuildContext context,
    ThemeProvider themeProvider,
  ) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          CustomContainer(
              icon: Icon(
                Icons.undo,
                color: themeProvider.themeMode == ThemeMode.dark
                    ? Colors.white
                    : Colors.black,
              ),
              isCircle: true,
              hideExpanded: true,
              onTap: () {
                sudokuBoardProvider.undo();
              }),
          CustomContainer(
            title: 'Restart',
            onTap: () {
              _showRestartGameDialog(
                context,
                sudokuBoardProvider,
                themeProvider,
              );
            },
          ),
          CustomContainer(
            title: 'Solve',
            onTap: () {
              _showSolveDialog(
                context,
                sudokuBoardProvider,
                themeProvider,
              );
            },
          ),
          CustomContainer(
            icon: Icon(
              Icons.delete,
              color: themeProvider.themeMode == ThemeMode.dark
                  ? Colors.white
                  : Colors.black,
            ),
            isCircle: true,
            hideExpanded: true,
            onTap: () {
              sudokuBoardProvider.deleteSelectedCell();
            },
          ),
          CustomContainer(
              icon: Icon(
                sudokuBoardProvider.isTimerVisible
                    ? Icons.timer
                    : Icons.timer_off,
                color: themeProvider.themeMode == ThemeMode.dark
                    ? Colors.white
                    : Colors.black,
              ),
              isCircle: true,
              hideExpanded: true,
              onTap: () {
                sudokuBoardProvider.toggleTimerVisibility();
              }),
        ],
      ),
    );
  }

  Future<dynamic> _showSolveDialog(
    BuildContext context,
    SudokuProvider sudokuBoardProvider,
    ThemeProvider themeProvider,
  ) {
    return showModalBottomSheet(
        isDismissible: true,
        isScrollControlled: false,
        context: context,
        builder: (context) {
          return Container(
              width: MediaQuery.sizeOf(context).width,
              decoration: BoxDecoration(
                  color: themeProvider.themeMode == ThemeMode.dark
                      ? ColorConstants.primaryColorDark
                      : ColorConstants.primaryColorLight,
                  borderRadius: const BorderRadius.only(
                      topRight: Radius.circular(12),
                      topLeft: Radius.circular(12))),
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    'Want to solve?',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.w600,
                      color: themeProvider.themeMode == ThemeMode.dark
                          ? Colors.white
                          : Colors.black,
                    ),
                  ),
                  const SizedBox(height: 20),
                  Row(
                    children: [
                      Expanded(
                        child: InkWell(
                          splashColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          onTap: () {
                            Navigator.of(context).pop(false);
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(8),
                              color: themeProvider.themeMode == ThemeMode.dark
                                  ? Colors.black
                                  : Colors.white,
                            ),
                            padding: const EdgeInsets.symmetric(
                                horizontal: 6, vertical: 12),
                            margin: const EdgeInsets.symmetric(
                                horizontal: 12, vertical: 8),
                            child: Center(
                              child: Text(
                                'No',
                                style: TextStyle(
                                  color:
                                      themeProvider.themeMode == ThemeMode.dark
                                          ? Colors.white
                                          : Colors.black,
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: InkWell(
                          splashColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          onTap: () {
                            sudokuBoardProvider.solvePuzzle(context);
                            Navigator.of(context).pop();
                            ScaffoldMessenger.of(context).clearSnackBars();
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(
                                margin: const EdgeInsets.symmetric(horizontal: 12,vertical: 12),
                                behavior: SnackBarBehavior.floating,
                                elevation: 10,
                                shape: RoundedRectangleBorder(
                                  side:  BorderSide( color: themeProvider.themeMode == ThemeMode.dark
                                      ? Colors.white
                                      : Colors.black, width: 0.5),
                                  borderRadius: BorderRadius.circular(12),
                                ),
                                content: Center(
                                    child: Text(
                                  'Solved the puzzle.If not solved remove incorrect input.',
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                    fontWeight: FontWeight.w500,
                                    color: themeProvider.themeMode ==
                                            ThemeMode.dark
                                        ? Colors.white
                                        : Colors.black,
                                  ),
                                )),
                                backgroundColor:
                                    themeProvider.themeMode == ThemeMode.dark
                                        ? ColorConstants.primaryColorDark
                                        : ColorConstants.primaryColorLight,
                              ),
                            );
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(8),
                              color: Colors.green,
                            ),
                            padding: const EdgeInsets.symmetric(
                                horizontal: 6, vertical: 12),
                            margin: const EdgeInsets.symmetric(
                                horizontal: 12, vertical: 8),
                            child: const Center(
                              child: Text(
                                'Yes',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ],
              ));
        });

    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          backgroundColor: Theme.of(context).colorScheme.primary,
          title: Text(' Want to solve?',
              style: Theme.of(context).textTheme.headlineMedium),
          content: Text('You will lose all your progress.',
              style: Theme.of(context).textTheme.headlineSmall),
          actions: <Widget>[
            TextButton(
              child:
                  Text('No', style: Theme.of(context).textTheme.headlineSmall),
              onPressed: () {
                Navigator.of(context).pop(false);
              },
            ),
            TextButton(
              child:
                  Text('Yes', style: Theme.of(context).textTheme.headlineSmall),
              onPressed: () {
                sudokuBoardProvider.solvePuzzle(context);
                Navigator.of(context).pop();
                ScaffoldMessenger.of(context).clearSnackBars();
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(
                      content: Text(
                          'Solved the puzzle.If not solved remove incorrect input.')),
                );
              },
            ),
          ],
        );
      },
    );
  }

  Future<dynamic> _showRestartGameDialog(
    BuildContext context,
    SudokuProvider sudokuBoardProvider,
    ThemeProvider themeProvider,
  ) {
    return showModalBottomSheet(
        isDismissible: true,
        isScrollControlled: false,
        context: context,
        builder: (context) {
          return Container(
              width: MediaQuery.sizeOf(context).width,
              decoration: BoxDecoration(
                  color: themeProvider.themeMode == ThemeMode.dark
                      ? ColorConstants.primaryColorDark
                      : ColorConstants.primaryColorLight,
                  borderRadius: const BorderRadius.only(
                      topRight: Radius.circular(12),
                      topLeft: Radius.circular(12))),
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    'Want to restart?',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.w600,
                      color: themeProvider.themeMode == ThemeMode.dark
                          ? Colors.white
                          : Colors.black,
                    ),
                  ),
                  const SizedBox(height: 20),
                  Text('You will lose all your progress.',
                      style:
                          Theme.of(context).textTheme.headlineMedium?.copyWith(
                                fontWeight: FontWeight.w400,
                                fontSize: 16,
                                color: themeProvider.themeMode == ThemeMode.dark
                                    ? Colors.white
                                    : Colors.black,
                              )),
                  const SizedBox(height: 12),
                  Row(
                    children: [
                      Expanded(
                        child: InkWell(
                          splashColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          onTap: () {
                            Navigator.of(context).pop(false);
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(8),
                              color: themeProvider.themeMode == ThemeMode.dark
                                  ? Colors.black
                                  : Colors.white,
                            ),
                            padding: const EdgeInsets.symmetric(
                                horizontal: 6, vertical: 12),
                            margin: const EdgeInsets.symmetric(
                                horizontal: 12, vertical: 8),
                            child: Center(
                              child: Text(
                                'No',
                                style: TextStyle(
                                  color:
                                      themeProvider.themeMode == ThemeMode.dark
                                          ? Colors.white
                                          : Colors.black,
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        child: InkWell(
                          splashColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          onTap: () {
                            sudokuBoardProvider.resetPuzzle();
                            Navigator.of(context).pop();
                            ScaffoldMessenger.of(context).clearSnackBars();
                            ScaffoldMessenger.of(context).showSnackBar(

                              SnackBar(
                                margin: const EdgeInsets.symmetric(horizontal: 12,vertical: 12),
                                padding: const EdgeInsets.symmetric(vertical: 20),
                                behavior: SnackBarBehavior.floating,
                                elevation: 10,
                                shape: RoundedRectangleBorder(
                                  side:  BorderSide( color: themeProvider.themeMode == ThemeMode.dark
                                      ? Colors.white
                                      : Colors.black, width: 0.5),
                                  borderRadius: BorderRadius.circular(12),
                                ),
                                content: Center(
                                    child: Text(
                                      'Puzzle Restarted. ',
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        fontWeight: FontWeight.w500,
                                        color: themeProvider.themeMode ==
                                            ThemeMode.dark
                                            ? Colors.white
                                            : Colors.black,
                                      ),
                                    )),
                                backgroundColor:
                                themeProvider.themeMode == ThemeMode.dark
                                    ? ColorConstants.primaryColorDark
                                    : ColorConstants.primaryColorLight,
                              )
                            );
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(8),
                              color: Colors.red,
                            ),
                            padding: const EdgeInsets.symmetric(
                                horizontal: 6, vertical: 12),
                            margin: const EdgeInsets.symmetric(
                                horizontal: 12, vertical: 8),
                            child: const Center(
                              child: Text(
                                'Yes',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ],
              ));
        });

    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          backgroundColor: Theme.of(context).colorScheme.primary,
          title: Text(' Want to restart?',
              style: Theme.of(context).textTheme.headlineMedium),
          content: Text('You will lose all your progress.',
              style: Theme.of(context).textTheme.headlineSmall),
          actions: <Widget>[
            TextButton(
              child:
                  Text('No', style: Theme.of(context).textTheme.headlineSmall),
              onPressed: () {
                Navigator.of(context).pop(false);
              },
            ),
            TextButton(
              child:
                  Text('Yes', style: Theme.of(context).textTheme.headlineSmall),
              onPressed: () {
                sudokuBoardProvider.resetPuzzle();
                Navigator.of(context).pop();
                ScaffoldMessenger.of(context).clearSnackBars();
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('Puzzle Restarted.')),
                );
              },
            ),
          ],
        );
      },
    );
  }

  Padding _numberInputUpperRow(SudokuProvider sudokuBoardProvider) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          for (int i = 1; i <= 5; i++)
            CustomContainer(
              title: i.toString(),
              onTap: () {
                if (sudokuBoardProvider.selectedRow != -1 &&
                    sudokuBoardProvider.selectedCol != -1) {
                  sudokuBoardProvider.updatePuzzle(
                      sudokuBoardProvider.selectedRow,
                      sudokuBoardProvider.selectedCol,
                      i);
                }
              },
            )
        ],
      ),
    );
  }

  Padding _numberInputLowerRow(
    SudokuProvider sudokuBoardProvider,
    BuildContext context,
    ThemeProvider themeProvider,
  ) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8),
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
        for (int i = 6; i <= 9; i++)
          CustomContainer(
            title: i.toString(),
            onTap: () {
              if (sudokuBoardProvider.selectedRow != -1 &&
                  sudokuBoardProvider.selectedCol != -1) {
                sudokuBoardProvider.updatePuzzle(
                    sudokuBoardProvider.selectedRow,
                    sudokuBoardProvider.selectedCol,
                    i);
              }
            },
          ),
        CustomContainer(
          icon: Icon(
            Icons.check,
            color: themeProvider.themeMode == ThemeMode.dark
                ? Colors.white
                : Colors.black,
          ),
          onTap: sudokuBoardProvider.checkIfPuzzleIsSolved()
              ? () {
                  _showPuzzleSolvedSheet(
                    context,
                    sudokuBoardProvider,
                    themeProvider,
                  );
                }
              : () {},
        )
      ]),
    );
  }

  Future<dynamic> _showPuzzleSolvedSheet(
    BuildContext context,
    SudokuProvider sudokuBoardProvider,
    ThemeProvider themeProvider,
  ) {
    return showModalBottomSheet(
        isDismissible: true,
        isScrollControlled: false,
        context: context,
        builder: (context) {
          return Container(
              width: MediaQuery.sizeOf(context).width,
              decoration: BoxDecoration(
                  color: themeProvider.themeMode == ThemeMode.dark
                      ? ColorConstants.primaryColorDark
                      : ColorConstants.primaryColorLight,
                  borderRadius: const BorderRadius.only(
                      topRight: Radius.circular(12),
                      topLeft: Radius.circular(12))),
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    'Congratulations!',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.w600,
                      color: themeProvider.themeMode == ThemeMode.dark
                          ? Colors.white
                          : Colors.black,
                    ),
                  ),
                  const SizedBox(height: 20),
                  Text('You solved the puzzle!',
                      style:
                          Theme.of(context).textTheme.headlineMedium?.copyWith(
                                fontWeight: FontWeight.w400,
                                fontSize: 16,
                                color: themeProvider.themeMode == ThemeMode.dark
                                    ? Colors.white
                                    : Colors.black,
                              )),
                  const SizedBox(height: 4),
                  Text(
                      'Time taken: ${Duration(seconds: sudokuBoardProvider.elapsedSeconds).toString().split('.').first.padLeft(8, "0")}',
                      style:
                          Theme.of(context).textTheme.headlineMedium?.copyWith(
                                fontWeight: FontWeight.w400,
                                fontSize: 16,
                                color: themeProvider.themeMode == ThemeMode.dark
                                    ? Colors.white
                                    : Colors.black,
                              )),
                  const SizedBox(height: 12),
                  InkWell(
                    splashColor: Colors.transparent,
                    highlightColor: Colors.transparent,
                    onTap: () {
                      sudokuBoardProvider.nextPuzzle();
                      sudokuBoardProvider.startTimer();
                      Navigator.of(context).pop();
                    },
                    child: Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8),
                        color: Colors.green,
                      ),
                      padding: const EdgeInsets.symmetric(
                          horizontal: 6, vertical: 12),
                      margin: const EdgeInsets.symmetric(
                          horizontal: 18, vertical: 8),
                      child: Center(
                        child: Text(
                          'Play Again',
                          style: TextStyle(
                            color: themeProvider.themeMode == ThemeMode.dark
                                ? Colors.white
                                : Colors.white,
                            fontSize: 16,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              ));
        });
  }

  Padding _timerRow(SudokuProvider sudokuBoardProvider, BuildContext context,
      ThemeProvider themeProvider, double sh) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 12),
      child: Row(
        mainAxisSize: MainAxisSize.max,
        children: [
          Text(
            'Difficulty: ${sudokuBoardProvider.getDifficultyText()}',
            style: Theme.of(context).textTheme.headlineMedium?.copyWith(
                  fontSize: 16,
                  fontWeight: FontWeight.w400,
                  color: themeProvider.themeMode == ThemeMode.dark
                      ? Colors.white
                      : Colors.black,
                ),
          ),
          const SizedBox(width: 12),
          Expanded(
            child: sudokuBoardProvider.isTimerVisible
                ? Text(
                    ' Time: ${Duration(seconds: sudokuBoardProvider.elapsedSeconds).toString().split('.').first.padLeft(8, "0")} ',
                    style: Theme.of(context).textTheme.headlineMedium?.copyWith(
                          fontSize: 16,
                          fontWeight: FontWeight.w400,
                          color: themeProvider.themeMode == ThemeMode.dark
                              ? Colors.white
                              : Colors.black,
                        ),
                    textAlign: TextAlign.end,
                  )
                : SizedBox(height: sh * 0.02),
          ),
        ],
      ),
    );
  }

  Expanded _mainBoardWidget(
      SudokuProvider sudokuBoardProvider, ThemeProvider themeProvider) {
    return Expanded(
      child: Center(
        child: GridView.builder(
          padding: EdgeInsets.zero,
          scrollDirection: Axis.vertical,
          physics: const NeverScrollableScrollPhysics(),
          itemCount: 81,
          shrinkWrap: true,
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 9,
          ),
          itemBuilder: (BuildContext context, int index) {
            int row = (index / 9).floor();
            int col = index % 9;
            int gridRow = (row / 3).floor();
            int gridCol = (col / 3).floor();
            bool isDarkGrid = (gridRow + gridCol) % 2 == 1;
            bool isSelected = sudokuBoardProvider.selectedRow >= 0 &&
                sudokuBoardProvider.selectedRow < 9 &&
                sudokuBoardProvider.selectedCol >= 0 &&
                sudokuBoardProvider.selectedCol < 9 &&
                sudokuBoardProvider.selectedRow == row &&
                sudokuBoardProvider.selectedCol == col;

            bool isMatchingSelectedValue = sudokuBoardProvider.selectedRow >=
                    0 &&
                sudokuBoardProvider.selectedRow < 9 &&
                sudokuBoardProvider.selectedCol >= 0 &&
                sudokuBoardProvider.selectedCol < 9 &&
                sudokuBoardProvider.puzzle[row][col] ==
                    sudokuBoardProvider.puzzle[sudokuBoardProvider.selectedRow]
                        [sudokuBoardProvider.selectedCol];
            return GestureDetector(
              onTap: () {
                sudokuBoardProvider.selectedRow = row;
                sudokuBoardProvider.selectedCol = col;
              },
              child: Container(
                decoration: BoxDecoration(
                  border: Border.all(
                    color: Colors.black,
                    width: 0.5,
                  ),
                  color: isSelected
                      ? themeProvider.themeMode == ThemeMode.dark
                          ? ColorConstants.selectedTileColorDark
                          : ColorConstants.selectedTileColorLight
                      : isDarkGrid
                          ? themeProvider.themeMode == ThemeMode.dark
                              ? ColorConstants.innerTileColorDark
                              : ColorConstants.innerTileColorLight
                          : themeProvider.themeMode == ThemeMode.dark
                              ? ColorConstants.tileColorDark
                              : ColorConstants.tileColorLight,
                ),
                child: Center(
                  child: Text(
                    sudokuBoardProvider.puzzle[row][col] != null &&
                            sudokuBoardProvider.puzzle[row][col] != 0
                        ? sudokuBoardProvider.puzzle[row][col].toString()
                        : '',
                    style: TextStyle(
                      color: isSelected
                          ? themeProvider.themeMode == ThemeMode.dark
                              ? Colors.white
                              : Colors.black // Color for user tapped cell
                          : isMatchingSelectedValue
                              ? Colors.orange
                              : // Color for matching selected value:

                              (sudokuBoardProvider.initialPuzzle[row][col] !=
                                          null &&
                                      sudokuBoardProvider.initialPuzzle[row]
                                              [col] !=
                                          0)
                                  ? themeProvider.themeMode == ThemeMode.dark
                                      ? Colors.white
                                      : Colors.black // Color for initial values
                                  : sudokuBoardProvider.checkIfIncorrect(
                                          row, col)
                                      ? Colors.red // Color for incorrect input
                                      : Colors.green,
                      //Color for text that user has entered manually and aren't wrong
                      // Color for updated numbers
                      fontSize: 18,
                      fontWeight: isMatchingSelectedValue
                          ? FontWeight.w600
                          : FontWeight.w400,
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Color _getTextColor(bool isSelected, bool isMatchingSelectedValue,
      SudokuProvider sudokuBoardProvider, int row, int col) {
    return isSelected
        ? Colors.black // Color for selected cell
        : isMatchingSelectedValue
            ? Colors.green // Color for matching selected value
            : (sudokuBoardProvider.initialPuzzle[row][col] != null &&
                    sudokuBoardProvider.initialPuzzle[row][col] != 0)
                ? Colors.black // Color for initial values
                : sudokuBoardProvider.checkIfIncorrect(row, col)
                    ? Colors.black // Color for incorrect input
                    : Colors.black;
  }
}
Leave a Comment