Give a ClosedPath class like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import 'dart:math'; class ClosedPath { List <Point> _points = []; // we create new array starting on new point void moveTo(Point point) { _points = [point]; } // add a point to our array so we can make a line from the previous index point to this point. void lineTo(Point point) { _points.add(point); } } |
In the main, we call its functions to draw a shape, you’ll do something like so:
1 2 3 4 5 6 7 8 9 10 11 |
import 'path.dart'; import 'dart:math'; void main() { final path = ClosedPath(); path.moveTo(Point(0,0)); path.lineTo(Point(2,0)); path.lineTo(Point(2,2)); path.lineTo(Point(0,2)); path.lineTo(Point(0,0)); } |
However, using the cascade operator, we can simply it to this:
1 2 3 4 5 6 |
ClosedPath() ..moveTo(Point(0,0)) ..lineTo(Point(2,0)) ..lineTo(Point(2,2)) ..lineTo(Point(0,2)) ..lineTo(Point(0,0)); |
Notice there is no semi-colon in the end. Instead, we use .. operator to depict calling another function on the same instance.