Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement number selection via keyboard #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions sudoku/lib/alerts/numbers.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../styles.dart';

Expand All @@ -25,20 +26,21 @@ class AlertNumbers extends State<AlertNumbersState> {
static final List<int> numberList2 = [4, 5, 6];
static final List<int> numberList3 = [7, 8, 9];

void select(int numSelected) {
setState(() {
numberSelected = number = numSelected;
Navigator.pop(context);
});
}

List<SizedBox> createButtons(List<int> numberList) {
return <SizedBox>[
for (int numbers in numberList)
SizedBox(
width: 38,
height: 38,
child: TextButton(
onPressed: () => {
setState(() {
numberSelected = numbers;
number = numberSelected;
Navigator.pop(context);
})
},
onPressed: () => select(numbers),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(
Styles.secondaryBackgroundColor),
Expand Down Expand Up @@ -82,7 +84,26 @@ class AlertNumbers extends State<AlertNumbersState> {

@override
Widget build(BuildContext context) {
return AlertDialog(
return RawKeyboardListener(
focusNode: FocusNode(
onKey: (_, __) => KeyEventResult.handled
),
autofocus: true,
onKey: (ev) {
if (ev is! RawKeyUpEvent) return;
var key = ev.logicalKey;
if (key == LogicalKeyboardKey.escape) Navigator.pop(context);
else if (key == LogicalKeyboardKey.digit1) select(1);
else if (key == LogicalKeyboardKey.digit2) select(2);
else if (key == LogicalKeyboardKey.digit3) select(3);
else if (key == LogicalKeyboardKey.digit4) select(4);
else if (key == LogicalKeyboardKey.digit5) select(5);
else if (key == LogicalKeyboardKey.digit6) select(6);
else if (key == LogicalKeyboardKey.digit7) select(7);
else if (key == LogicalKeyboardKey.digit8) select(8);
else if (key == LogicalKeyboardKey.digit9) select(9);
},
child: AlertDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
backgroundColor: Styles.secondaryBackgroundColor,
title: Center(
Expand All @@ -95,6 +116,6 @@ class AlertNumbers extends State<AlertNumbersState> {
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: createRows(),
));
)));
}
}