From d1b99c2af267f53424a63c36e40d845113488080 Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Sun, 30 Aug 2026 17:21:48 +0200 Subject: [PATCH] Paint the map Signed-off-by: Hans Kokx --- lib/main.dart | 80 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 13 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 607b614..1fafa6c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 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,