Skip to content

Commit 709f588

Browse files
committed
Add new properties.
1 parent ebdb8c3 commit 709f588

File tree

11 files changed

+232
-85
lines changed

11 files changed

+232
-85
lines changed

patterns/abstract_factory/tool_panel_factory/factories/circle_factory.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,37 @@ import '../shapes/circle_shape.dart';
77
import '../pattern/shape.dart';
88

99
class CircleFactory extends ToolFactory with IconBoxMixin {
10+
var _radius = 50.0;
11+
var _isFilled = false;
12+
1013
@override
1114
Shape createShape(double x, double y, Color color) {
1215
return CircleShape(
13-
radius: 50,
16+
radius: _radius,
17+
isFilled: _isFilled,
1418
x: x,
1519
y: y,
1620
color: color,
1721
);
1822
}
1923

2024
@override
21-
Iterable<Property> get properties => [];
25+
Iterable<Property> get properties {
26+
return [
27+
Property(
28+
name: 'radius',
29+
value: () => _radius,
30+
onChange: (val) {
31+
_radius = val;
32+
},
33+
),
34+
Property(
35+
name: 'filled',
36+
value: () => _isFilled,
37+
onChange: (val) {
38+
_isFilled = val;
39+
},
40+
),
41+
];
42+
}
2243
}

patterns/abstract_factory/tool_panel_factory/factories/line_factory.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,37 @@ import '../pattern/shape.dart';
77
import '../shapes/line_shape.dart';
88

99
class LineFactory extends ToolFactory with IconBoxMixin {
10+
var _isMirror = true;
11+
var _length = 100.0;
12+
1013
@override
1114
Shape createShape(double x, double y, Color color) {
1215
return LineShape(
13-
length: 100,
16+
length: _length,
17+
isMirror: _isMirror,
1418
x: x,
1519
y: y,
1620
color: color,
1721
);
1822
}
1923

2024
@override
21-
Iterable<Property> get properties => [];
25+
Iterable<Property> get properties {
26+
return [
27+
Property(
28+
name: 'mirror',
29+
value: () => _isMirror,
30+
onChange: (val) {
31+
_isMirror = val;
32+
},
33+
),
34+
Property(
35+
name: 'length',
36+
value: () => _length,
37+
onChange: (val) {
38+
_length = val;
39+
},
40+
),
41+
];
42+
}
2243
}

patterns/abstract_factory/tool_panel_factory/factories/star_factory.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,37 @@ import '../pattern/shape.dart';
77
import '../shapes/star_shape.dart';
88

99
class StarFactory extends ToolFactory with IconBoxMixin {
10+
var _radius = 80.0;
11+
var _isFilled = false;
12+
1013
@override
1114
Shape createShape(double x, double y, Color color) {
1215
return StarShape(
13-
radius: 80,
16+
radius: _radius,
17+
isFilled: _isFilled,
1418
x: x,
1519
y: y,
1620
color: color,
1721
);
1822
}
1923

2024
@override
21-
Iterable<Property> get properties => [];
25+
Iterable<Property> get properties {
26+
return [
27+
Property(
28+
name: 'radius',
29+
value: () => _radius,
30+
onChange: (val) {
31+
_radius = val;
32+
},
33+
),
34+
Property(
35+
name: 'filled',
36+
value: () => _isFilled,
37+
onChange: (val) {
38+
_isFilled = val;
39+
},
40+
),
41+
];
42+
}
2243
}

patterns/abstract_factory/tool_panel_factory/factories/triangle_factory.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,35 @@ import '../pattern/shape.dart';
77
import '../shapes/triangle_shape.dart';
88

99
class TriangleFactory extends ToolFactory with IconBoxMixin {
10+
var _isFilled = false;
11+
var _sideLength = 120.0;
12+
1013
@override
1114
Shape createShape(double x, double y, Color color) {
1215
return TriangleShape(
13-
sideLength: 120,
16+
sideLength: _sideLength,
17+
isFilled: _isFilled,
1418
x: x,
1519
y: y,
1620
color: color,
1721
);
1822
}
1923

2024
@override
21-
Iterable<Property> get properties => [];
25+
Iterable<Property> get properties => [
26+
Property(
27+
name: 'sideLength',
28+
value: () => _sideLength,
29+
onChange: (val) {
30+
_sideLength = val;
31+
},
32+
),
33+
Property(
34+
name: 'filled',
35+
value: () => _isFilled,
36+
onChange: (val) {
37+
_isFilled = val;
38+
},
39+
),
40+
];
2241
}

patterns/abstract_factory/tool_panel_factory/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class _ToolPanelFactoryAppState extends State<ToolPanelFactoryApp> {
2424
shapes: Shapes([]),
2525
tools: Tools(
2626
factories: [
27-
TextFactory(),
2827
LineFactory(),
2928
CircleFactory(),
3029
TriangleFactory(),
3130
StarFactory(),
31+
TextFactory(),
3232
],
3333
colors: [
3434
Colors.white,

patterns/abstract_factory/tool_panel_factory/pattern/shape.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ abstract class Shape {
2525
_y -= height / 2;
2626
}
2727

28+
void translate(double x, double y) {
29+
_x += x;
30+
_y += y;
31+
}
32+
2833
double _x;
2934
double _y;
3035
}

patterns/abstract_factory/tool_panel_factory/shapes/circle_shape.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import '../pattern/shape.dart';
66

77
class CircleShape extends Shape {
88
final double radius;
9+
final bool isFilled;
910

1011
CircleShape({
1112
required this.radius,
13+
required this.isFilled,
1214
required double x,
1315
required double y,
1416
required Color color,
@@ -23,8 +25,10 @@ class CircleShape extends Shape {
2325
final pos = width / 2;
2426
can.drawCircle(
2527
Offset(pos, pos),
26-
radius,
27-
Paint()..color = color,
28+
radius - 2,
29+
Paint()
30+
..style = isFilled ? PaintingStyle.fill : PaintingStyle.stroke
31+
..color = color,
2832
);
2933
}
3034

patterns/abstract_factory/tool_panel_factory/shapes/line_shape.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class LineShape extends Shape {
88
final double length;
99

1010
LineShape({
11+
required bool isMirror,
1112
required this.length,
1213
required double x,
1314
required double y,
@@ -16,17 +17,22 @@ class LineShape extends Shape {
1617
x: x,
1718
y: y,
1819
color: color,
19-
);
20+
) {
21+
if (isMirror) {
22+
point1 = Offset(0, length);
23+
point2 = Offset(length, 0);
24+
} else {
25+
point1 = Offset(0, 0);
26+
point2 = Offset(length, length);
27+
}
28+
}
29+
30+
late final Offset point1;
31+
late final Offset point2;
2032

2133
@override
2234
void paint(Canvas can) {
23-
can.drawLine(
24-
Offset(0, length),
25-
Offset(length, 0),
26-
Paint()
27-
..color = color
28-
..strokeWidth = 3,
29-
);
35+
can.drawLine(point1, point2, Paint()..color = color);
3036
}
3137

3238
@override

patterns/abstract_factory/tool_panel_factory/shapes/star_shape.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,32 @@ import '../pattern/shape.dart';
77

88
class StarShape extends Shape {
99
final double radius;
10+
final bool isFilled;
1011

1112
StarShape({
1213
required this.radius,
14+
required this.isFilled,
1315
required double x,
1416
required double y,
1517
required Color color,
1618
}) : super(
1719
x: x,
1820
y: y,
1921
color: color,
20-
);
22+
) {
23+
_starPath = Path()..addPolygon(_createStar(), true);
24+
}
25+
26+
late final Path _starPath;
2127

2228
@override
2329
void paint(Canvas can) {
24-
final path = Path()..addPolygon(_createStar(), true);
25-
can.drawPath(path, Paint()..color = color);
30+
can.drawPath(
31+
_starPath,
32+
Paint()
33+
..style = isFilled ? PaintingStyle.fill : PaintingStyle.stroke
34+
..color = color,
35+
);
2636
}
2737

2838
@override

patterns/abstract_factory/tool_panel_factory/shapes/triangle_shape.dart

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,16 @@ import '../pattern/shape.dart';
77

88
class TriangleShape extends Shape {
99
final double sideLength;
10+
final bool isFilled;
1011

1112
TriangleShape({
1213
required this.sideLength,
14+
required this.isFilled,
1315
required double x,
1416
required double y,
1517
required Color color,
16-
}) : super(
17-
x: x,
18-
y: y,
19-
color: color,
20-
);
21-
22-
@override
23-
void paint(Canvas can) {
24-
final path = Path()
18+
}) : super(x: x, y: y, color: color) {
19+
_trianglePath = Path()
2520
..addPolygon(
2621
[
2722
Offset(0, height),
@@ -30,15 +25,17 @@ class TriangleShape extends Shape {
3025
],
3126
true,
3227
);
28+
}
3329

30+
late final Path _trianglePath;
31+
32+
@override
33+
void paint(Canvas can) {
3434
can.drawPath(
35-
path,
36-
Paint()
37-
..strokeJoin = StrokeJoin.round
38-
..style = PaintingStyle.stroke
39-
..color = color
40-
..strokeWidth = 5,
41-
);
35+
_trianglePath,
36+
Paint()
37+
..style = isFilled ? PaintingStyle.fill : PaintingStyle.stroke
38+
..color = color);
4239
}
4340

4441
@override

0 commit comments

Comments
 (0)