Paint the map

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-08-30 17:21:48 +02:00
parent 93ba2b8486
commit d1b99c2af2
+67 -13
View File
@@ -18,12 +18,7 @@ const int mapY = 8;
const int mapSize = 32;
void main() {
runApp(
const Directionality(
textDirection: TextDirection.ltr,
child: MainApp(),
),
);
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
@@ -31,25 +26,84 @@ class MainApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const Row(
spacing: mapSize / 1,
children: [
Flexible(child: Map()),
Flexible(child: Background()),
],
return Directionality(
textDirection: TextDirection.ltr,
child: LayoutBuilder(
builder: (context, constraints) => const Row(
spacing: mapSize / 1,
textDirection: TextDirection.ltr,
children: [
Flexible(child: Map()),
Flexible(child: Background()),
],
),
),
);
}
}
// * Map
class Map extends StatelessWidget {
const new({super.key});
@override
Widget build(BuildContext context) {
return const Placeholder();
const double width = mapX * mapSize / 1;
const double height = mapY * mapSize / 1;
return const Center(
child: FittedBox(
fit: BoxFit.contain,
child: CustomPaint(
size: Size(width, height),
painter: MapPainter(
mapData: map,
mapX: mapX,
mapY: mapY,
tileSize: mapSize / 1,
),
),
),
);
}
}
class MapPainter extends CustomPainter {
final List<int> mapData;
final int mapX;
final int mapY;
final double tileSize;
const new({
required this.mapData,
required this.mapX,
required this.mapY,
required this.tileSize,
});
@override
void paint(Canvas canvas, Size size) {
final paintWhite = Paint()..color = Colors.white;
final paintBlack = Paint()..color = Colors.black;
for (int i = 0; i < mapData.length; i++) {
final double x = (i % mapX) * tileSize;
final double y = (i ~/ mapX) * tileSize;
canvas.drawRect(
Rect.fromLTWH(x, y, tileSize - 1, tileSize - 1),
mapData[i] == 0 ? paintBlack : paintWhite,
);
}
}
@override
bool shouldRepaint(covariant oldDelegate) => false;
@override
bool shouldRebuildSemantics(covariant oldDelegate) => false;
}
// * Background
class Background extends StatelessWidget {
const new({
super.key,