Skip to content

Commit

Permalink
selected cells row,column and box are colored
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufgngor committed Aug 14, 2024
1 parent 3f22cff commit e6f0874
Show file tree
Hide file tree
Showing 10 changed files with 804 additions and 95 deletions.
9 changes: 7 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:my_sudoku_table/sudoku_view_model.dart';
import 'package:my_sudoku_table/theme/theme.dart';
import 'package:my_sudoku_table/theme/util.dart';
import 'package:my_sudoku_table/view/table.dart';
import 'package:provider/provider.dart';

Expand All @@ -12,12 +14,15 @@ class MainApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
TextTheme textTheme = createTextTheme(context, "Roboto", "Abhaya Libre");
MaterialTheme theme = MaterialTheme(textTheme);
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => SudokuNotifier()),
],
child: const MaterialApp(
home: Scaffold(
child: MaterialApp(
theme: theme.light(),
home: const Scaffold(
body: Center(
child: SudokuTable(),
),
Expand Down
105 changes: 92 additions & 13 deletions lib/sudoku_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

enum InputMode {
value,
note,
}

class Cell {
final int index;
int? value;
List<int> notes;
List<int> notes = [];

get rowNumber => index ~/ 9;
get columnNumber => index % 9;
get boxNumber => (rowNumber ~/ 3) * 3 + (columnNumber ~/ 3);

Cell({required this.index, this.value});

Cell({required this.index, this.value, this.notes = const [1]});
addNoteToCell(int note) {
notes.add(note);
}

removeNoteFromCell(int note) {
notes.remove(note);
}

clear() {
value = null;
notes = [];
}


}

class SudokuNotifier extends ChangeNotifier {
final List<Cell> _cells = List.generate(81, (index) => Cell(index: index));

final List<List<Cell>> rows = List.generate(9, (_) => [], growable: false);
final List<List<Cell>> columns = List.generate(9, (_) => [], growable: false);
final List<List<Cell>> boxes = List.generate(9, (_) => [], growable: false);

List<Cell> get cells => _cells;

Cell? selectedCell;
InputMode inputMode = InputMode.note;

SudokuNotifier() {
for (var cell in _cells) {
rows[cell.rowNumber].add(cell);
columns[cell.columnNumber].add(cell);
boxes[cell.boxNumber].add(cell);
}
HardwareKeyboard.instance.addHandler(
(event) {
if (selectedCell == null) {
if (event is! KeyDownEvent || selectedCell == null) {
return true;
}

switch (event.logicalKey) {
// 1-9 keys
case LogicalKeyboardKey.digit1:
updateCell(selectedCell!.index, 1);
break;
Expand Down Expand Up @@ -51,9 +86,44 @@ class SudokuNotifier extends ChangeNotifier {
case LogicalKeyboardKey.digit9:
updateCell(selectedCell!.index, 9);
break;

// Manage notes
case LogicalKeyboardKey.backspace:
clearCell(selectedCell!);
break;
case LogicalKeyboardKey.keyQ:
toogleInputMode(InputMode.note);
break;
case LogicalKeyboardKey.keyW:
toogleInputMode(InputMode.value);
break;

// Arrow keys
case LogicalKeyboardKey.arrowUp:
if (selectedCell!.index >= 9) {
selectCell(_cells[selectedCell!.index - 9]);
}
break;
case LogicalKeyboardKey.arrowDown:
if (selectedCell!.index < 72) {
selectCell(_cells[selectedCell!.index + 9]);
}
break;
case LogicalKeyboardKey.arrowLeft:
if (selectedCell!.index % 9 != 0) {
selectCell(_cells[selectedCell!.index - 1]);
}
break;
case LogicalKeyboardKey.arrowRight:
if (selectedCell!.index % 9 != 8) {
selectCell(_cells[selectedCell!.index + 1]);
}
break;
case LogicalKeyboardKey.tab:
if (selectedCell!.index < 80) {
selectCell(_cells[selectedCell!.index + 1]);
}

default:
return false;
}
Expand All @@ -63,28 +133,37 @@ class SudokuNotifier extends ChangeNotifier {
}

void updateCell(int index, int value) {
_cells[index].value = value;
if (inputMode == InputMode.note) {
if (_cells[index].notes.contains(value)) {
_cells[index].removeNoteFromCell(value);
} else {
_cells[index].addNoteToCell(value);
}
} else {
_cells[index].value = value;
}
notifyListeners();
}

void addNoteToCell(Cell cell, int note) {
cell.notes.add(note);
void clearCell(Cell cell) {
cell.clear();
notifyListeners();
}

void removeNoteFromCell(Cell cell, int note) {
cell.notes.remove(note);
void clearAll() {
for (var cell in _cells) {
cell.clear();
}
notifyListeners();
}

void clearCell(Cell cell) {
cell.value = null;
cell.notes = [];
void selectCell(Cell cell) {
selectedCell = cell;
notifyListeners();
}

void selectCell(Cell cell) {
selectedCell = cell;
void toogleInputMode(InputMode inputMode) {
this.inputMode = inputMode;
notifyListeners();
}
}
Loading

0 comments on commit e6f0874

Please sign in to comment.