| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import 'package:flutter/cupertino.dart';
- import 'package:gobang/models/position.dart';
- import 'package:gobang/utils/constants.dart';
- /// 棋盘 + 棋子绘制
- class BoardPainter extends CustomPainter {
- BoardPainter({required this.pieces, required this.version});
- final List<Position> pieces;
- final int version;
- @override
- void paint(Canvas canvas, Size size) {
- final cell = size.width / (kBoardSize - 1);
- final bg = Paint()
- ..isAntiAlias = true
- ..style = PaintingStyle.fill
- ..color = const Color(0x77cdb175);
- canvas.drawRect(Offset.zero & size, bg);
- final line = Paint()
- ..style = PaintingStyle.stroke
- ..color = CupertinoColors.systemGrey6
- ..strokeWidth = 1;
- for (var i = 0; i < kBoardSize; i++) {
- final o = cell * i;
- canvas.drawLine(Offset(0, o), Offset(size.width, o), line);
- canvas.drawLine(Offset(o, 0), Offset(o, size.height), line);
- }
- final radius = cell / 2 - 2;
- final fill = Paint()..style = PaintingStyle.fill;
- for (final p in pieces) {
- fill.color = p.chess.color;
- final center = Offset(p.dx, p.dy);
- if (p.chessShape.shape == 1) {
- canvas.drawCircle(center, radius, fill);
- } else {
- canvas.drawRect(
- Rect.fromCircle(center: center, radius: radius),
- fill,
- );
- }
- }
- }
- @override
- bool shouldRepaint(covariant BoardPainter oldDelegate) {
- return oldDelegate.version != version;
- }
- }
|