Untitled

mail@pastecode.io avatar
unknown
dart
a year ago
1.6 kB
2
Indexable
Never
import 'dart:math';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Six Pointed Star')),
        body: Center(child: StarPainterWidget()),
      ),
    );
  }
}

class StarPainterWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: StarPainter(),
      child: Container(
        width: 200,
        height: 200,
      ),
    );
  }
}

class StarPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..color = Colors.blue
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0;

    final double centerX = size.width / 2;
    final double centerY = size.height / 2;
    final double radius = min(centerX, centerY);

    // Calculate points for the star
    List<Offset> starPoints = [];
    for (int i = 0; i < 6; i++) {
      double x = centerX + radius * cos(i * pi / 3);
      double y = centerY + radius * sin(i * pi / 3);
      starPoints.add(Offset(x, y));
    }

    // Draw the star using the points
    for (int i = 0; i < 6; i += 2) {
      canvas.drawLine(starPoints[i], starPoints[(i + 3) % 6], paint);
    }
    for (int i = 1; i < 6; i += 2) {
      canvas.drawLine(starPoints[i], starPoints[(i + 3) % 6], paint);
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}