Created
June 7, 2022 11:32
-
-
Save ayansg/f35f30f509118813952d34afc2b59907 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:math'; | |
| import 'dart:ui'; | |
| import 'package:flutter/material.dart'; | |
| void main() { | |
| runApp( | |
| const MyApp(), | |
| ); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return const MaterialApp( | |
| home: MyCanvas(), | |
| ); | |
| } | |
| } | |
| class MyCanvas extends StatelessWidget { | |
| /* | |
| A(-0.1383995, 0.6430893) | |
| B(0.3046019, 0.5874474) | |
| C(0.2837419, 0.1830814) | |
| D(-0.1851889, 0.2113544) | |
| */ | |
| static const List<Point<double>> points = [ | |
| Point(-0.1383995, 0.6430893), | |
| Point(0.3046019, 0.5874474), | |
| Point(0.2837419, 0.1830814), | |
| Point(-0.1851889, 0.2113544), | |
| ]; | |
| const MyCanvas({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Custom paint Demo'), | |
| ), | |
| body: const Center( | |
| child: CustomPaint( | |
| size: Size(500, 500), | |
| painter: LinePainter(points), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| extension PointExtension on Point<double> { | |
| Offset toOffset() => Offset(x * 100.0, y * 100.0); | |
| } | |
| class LinePainter extends CustomPainter { | |
| final List<Point<double>> points; | |
| const LinePainter(this.points); | |
| @override | |
| void paint(Canvas canvas, Size size) { | |
| var paint = Paint() | |
| ..color = Colors.teal | |
| ..strokeWidth = 1; | |
| canvas.drawPoints( | |
| PointMode.polygon, | |
| points | |
| .map( | |
| (e) => e.toOffset(), | |
| ) | |
| .toList() | |
| ..add(points[0].toOffset()), | |
| paint, | |
| ); | |
| } | |
| @override | |
| bool shouldRepaint(CustomPainter oldDelegate) { | |
| return false; | |
| } | |
| void _getScale() { | |
| var x1 = 0.3046019; | |
| var y1 = 0.5874474; | |
| var x2 = -0.1383995; | |
| var y2 = 0.6430893; | |
| var l = sqrt(pow((x2 - x1), 2.0) + pow((y2 - y1), 2.0)); | |
| var v = 47.6573; | |
| var s = v / l; | |
| //print(s); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment