Files
logic-puzzle/lib/ui/board.dart
T
2026-10-31 04:08:16 +01:00

228 lines
8.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import '../domain/puzzle.dart';
import 'paper.dart';
class Board extends StatelessWidget {
const Board({
super.key,
required this.puzzle,
required this.cells,
required this.onMark,
this.selectedArea = 0,
this.won = false,
});
final Puzzle puzzle;
final List<int> cells;
final ValueChanged<int> onMark;
final int selectedArea;
final bool won;
@override
Widget build(BuildContext context) => Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 350),
child: AspectRatio(
aspectRatio: 1,
child: LayoutBuilder(
builder: (context, c) {
final n = puzzle.size,
u = c.maxWidth / (n + 1.3),
r = puzzle.regions.isEmpty
? null
: puzzle.regions[selectedArea];
Widget clue(String text, bool complete) => SizedBox(
width: u,
height: u,
child: Center(
child: Text(
text,
style: hand(
(u * .42).clamp(16, 23),
color: complete ? blue : muted,
),
),
),
);
bool complete(Iterable<int> indices, int total) =>
indices.where((i) => cells[i] == 1).length == total;
return Align(
alignment: Alignment.center,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(width: u, height: u),
for (var col = 0; col < n; col++)
clue(
'${puzzle.cols[col]}',
complete(
List.generate(n, (i) => i * n + col),
puzzle.cols[col],
),
),
],
),
for (var row = 0; row < n; row++)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
clue(
'${puzzle.rows[row]}',
complete(
List.generate(n, (i) => row * n + i),
puzzle.rows[row],
),
),
for (var col = 0; col < n; col++)
Builder(
builder: (context) {
final i = row * n + col,
value = cells[i],
fixed = puzzle.given[i],
inside = r?.contains(i, n) ?? false;
final state = value == 0
? 'empty'
: value == 1
? 'lit'
: 'crossed out';
return Semantics(
label:
'Row ${row + 1}, column ${col + 1}, $state${fixed ? ', fixed clue' : ''}',
button: !fixed && !won,
enabled: !fixed && !won,
onTap: fixed || won ? null : () => onMark(i),
excludeSemantics: true,
child: GestureDetector(
key: ValueKey('cell-$i'),
behavior: HitTestBehavior.opaque,
onTap: fixed || won ? null : () => onMark(i),
child: SizedBox(
width: u,
height: u,
child: Padding(
padding: const EdgeInsets.all(2),
child: DecoratedBox(
decoration: BoxDecoration(
color: value == 1
? blue
: inside
? amber.withValues(alpha: .22)
: Colors.transparent,
borderRadius: BorderRadius.circular(
2,
),
border: Border.all(
color: inside
? const Color(0xffaa5b2a)
: muted.withValues(alpha: .48),
width: inside ? 1.4 : .65,
),
),
child: Stack(
children: [
if (value == 2)
Center(
child: Text(
'×',
style: hand(
u * .62,
color: muted,
),
),
),
if (fixed)
Positioned(
right: 3,
bottom: 3,
child: Container(
width: 4,
height: 4,
decoration: BoxDecoration(
color: value == 1
? cream
: muted,
shape: BoxShape.circle,
),
),
),
],
),
),
),
),
),
);
},
),
],
),
],
),
);
},
),
),
),
);
}
class PixelPicture extends StatelessWidget {
const PixelPicture(
this.picture, {
super.key,
required this.label,
this.height = 230,
});
final List<int> picture;
final String label;
final double height;
@override
Widget build(BuildContext context) => Semantics(
image: true,
label: 'Recovered pixel picture: $label',
child: Center(
child: SizedBox(
width: height,
height: height,
child: Padding(
padding: const EdgeInsets.all(24),
child: DecoratedBox(
decoration: BoxDecoration(
color: const Color(0xfffaf1d8),
boxShadow: [
BoxShadow(
color: ink.withValues(alpha: .15),
offset: const Offset(3, 4),
),
],
),
child: Padding(
padding: const EdgeInsets.all(16),
child: CustomPaint(painter: _Pixels(picture)),
),
),
),
),
),
);
}
class _Pixels extends CustomPainter {
_Pixels(this.picture);
final List<int> picture;
@override
void paint(Canvas c, Size s) {
final p = Paint()..color = blue, u = s.width / 7;
for (var i = 0; i < 49; i++) {
if (picture[i] == 1) {
c.drawRect(Rect.fromLTWH(i % 7 * u, i ~/ 7 * u, u + .3, u + .3), p);
}
}
}
@override
bool shouldRepaint(_Pixels old) => old.picture != picture;
}