Skip to content

Commit 39be84b

Browse files
authored
Merge pull request #51 from ilopX/add-properties-to-tool-factory
Add properties to tool factory.
2 parents f3d74d6 + a5f8712 commit 39be84b

35 files changed

+656
-194
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.23.12
2+
- Add properties bar to "Tool Panel Factory".
3+
14
## 0.23.0
25
- Add "Tool Panel Factory" flutter example
36

bin/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MyApp extends StatelessWidget {
1414
return MaterialApp(
1515
title: 'Refactoring Guru: Flutter launcher',
1616
theme: ThemeData(primarySwatch: Colors.pink),
17-
initialRoute: '/abstract_factory/tool_panel',
17+
initialRoute: '/abstract_factory/tool_panel_factory',
1818
routes: {
1919
'/observer/subscriber_flutter_widget': (_) => SubscriberFlutterApp(),
2020
'/adapter/flutter_adapter': (_) => FlutterAdapterApp(),

patterns/abstract_factory/tool_panel_factory/README.md

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,34 @@ Tutorial: [here](https://refactoring.guru/design-patterns/abstract-factory).
77
### Online demo:
88
Click on the picture to see the [demo](https://RefactoringGuru.github.io/design-patterns-dart/#/abstract_factory/tool_panel_factory).
99

10-
[![image](https://user-images.githubusercontent.com/8049534/168668992-369a1bab-9f97-4333-a20e-ffd06bf91b54.png)](https://refactoringguru.github.io/design-patterns-dart/#/abstract_factory/tool_panel_factory)
10+
[![image](https://user-images.githubusercontent.com/8049534/169530318-0ce7ee6a-3538-4398-a2ab-e6e85f2132b5.png)](https://refactoringguru.github.io/design-patterns-dart/#/abstract_factory/tool_panel_factory)
11+
12+
### About
13+
![image](https://user-images.githubusercontent.com/8049534/169521422-052cc59a-7b3d-4889-8d76-5664a75b271a.png)
1114

1215
### Diagram:
13-
![image](https://user-images.githubusercontent.com/8049534/168672053-73ae1c9c-8fad-45ae-9247-429f7b5da565.png)
16+
![image](https://user-images.githubusercontent.com/8049534/169528461-d7c5279d-7e7f-4ce7-b8d2-87388ec2d32f.png)
1417

15-
### Client code (using the "createShape" method):
18+
### Client code:
1619
```dart
17-
final app = App(
18-
tools: Tools(
19-
factories: [
20-
TxtFactory(),
21-
LineFactory(),
22-
CircleFactory(),
23-
TriangleFactory(),
24-
StarFactory(),
25-
],
26-
),
27-
);
28-
2920
class App {
3021
void addShape(double x, double y) {
31-
final newShape = activeFactory.createShape(x, y, activeColor);
22+
final newShape = activeToolFactory.createShape(x, y, activeColor);
3223
shapes.add(newShape);
3324
}
3425
}
3526
36-
mixin IconBoxMixin implements FactoryTool {
37-
Image? _icon;
3827
39-
@override
40-
Image get icon => _icon!;
28+
class PropertyPanel extends StatelessWidget {
29+
final PropertyWidgetFactories factories;
4130
42-
Future<void> updateIcon(Color color) async {
43-
final shape = createShape(0, 0, color);
44-
final pngBytes = await _pngImageFromShape(shape);
45-
_icon = Image.memory(pngBytes);
31+
@override
32+
Widget build(BuildContext context) {
33+
return Row(
34+
children: propertyWidgetFactories
35+
.createListWidgetsFrom(activeToolFactory.properties)
36+
.toList(),
37+
);
4638
}
4739
}
4840
```
49-

patterns/abstract_factory/tool_panel_factory/app/app.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class App {
1212

1313
void addShape(double x, double y) {
1414
final activeColor = tools.activeColor.value;
15-
final activeFactory = tools.activeFactory.value!;
15+
final activeFactory = tools.activeFactory.value;
1616

1717
final newShape = activeFactory.createShape(x, y, activeColor);
1818
newShape.centerToFit();

patterns/abstract_factory/tool_panel_factory/app/tools.dart

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,18 @@ class Tools {
1010
final List<ToolFactory> factories;
1111
final List<Color> colors;
1212

13-
final activeFactory = ValueNotifier<ToolFactory?>(null);
13+
late final ValueNotifier<ToolFactory> activeFactory;
1414

15-
final activeColor = ValueNotifier(Color(0x0FFFFFFFF));
15+
late final ValueNotifier<Color> activeColor;
1616

1717
Future<bool> get iconsReady => _iconsInitCompleter.future;
1818

19-
Tools({required this.factories, required this.colors}) {
19+
Tools({required this.factories, required this.colors})
20+
: assert(factories.isNotEmpty),
21+
assert(colors.isNotEmpty) {
22+
activeFactory = ValueNotifier(factories.first);
23+
activeColor = ValueNotifier(colors.first);
2024
_initIconsFromShapes();
21-
22-
if (factories.isNotEmpty) {
23-
activeFactory.value = factories.first;
24-
}
25-
26-
if (colors.isNotEmpty) {
27-
activeColor.value = colors.first;
28-
}
2925
}
3026

3127
final _iconsInitCompleter = Completer<bool>();
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
import 'dart:ui';
22

33
import '../mixin/icon_box_mixin.dart';
4+
import '../pattern/property.dart';
45
import '../pattern/tool_factory.dart';
56
import '../shapes/circle_shape.dart';
67
import '../pattern/shape.dart';
78

89
class CircleFactory extends ToolFactory with IconBoxMixin {
10+
var _radius = 50.0;
11+
var _isFilled = false;
12+
913
@override
1014
Shape createShape(double x, double y, Color color) {
1115
return CircleShape(
12-
radius: 50,
16+
radius: _radius,
17+
isFilled: _isFilled,
1318
x: x,
1419
y: y,
1520
color: color,
1621
);
1722
}
23+
24+
@override
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+
}
1843
}
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
import 'dart:ui';
22

33
import '../mixin/icon_box_mixin.dart';
4+
import '../pattern/property.dart';
45
import '../pattern/tool_factory.dart';
56
import '../pattern/shape.dart';
67
import '../shapes/line_shape.dart';
78

89
class LineFactory extends ToolFactory with IconBoxMixin {
10+
var _isMirror = true;
11+
var _length = 100.0;
12+
913
@override
1014
Shape createShape(double x, double y, Color color) {
1115
return LineShape(
12-
length: 100,
16+
length: _length,
17+
isMirror: _isMirror,
1318
x: x,
1419
y: y,
1520
color: color,
1621
);
1722
}
23+
24+
@override
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+
}
1843
}
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
import 'dart:ui';
22

33
import '../mixin/icon_box_mixin.dart';
4+
import '../pattern/property.dart';
45
import '../pattern/tool_factory.dart';
56
import '../pattern/shape.dart';
67
import '../shapes/star_shape.dart';
78

89
class StarFactory extends ToolFactory with IconBoxMixin {
10+
var _radius = 80.0;
11+
var _isFilled = false;
12+
913
@override
1014
Shape createShape(double x, double y, Color color) {
1115
return StarShape(
12-
radius: 80,
16+
radius: _radius,
17+
isFilled: _isFilled,
1318
x: x,
1419
y: y,
1520
color: color,
1621
);
1722
}
23+
24+
@override
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+
}
1843
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'dart:ui';
2+
3+
import '../mixin/icon_box_mixin.dart';
4+
import '../pattern/property.dart';
5+
import '../pattern/tool_factory.dart';
6+
import '../shapes/text_shape.dart';
7+
import '../pattern/shape.dart';
8+
9+
class TextFactory extends ToolFactory with IconBoxMixin {
10+
var _text = 'Text';
11+
var _fontSize = 50.0;
12+
13+
@override
14+
Shape createShape(double x, double y, Color color) {
15+
return TextShape(
16+
text: _text,
17+
fontSize: _fontSize,
18+
x: x,
19+
y: y,
20+
color: color,
21+
);
22+
}
23+
24+
@override
25+
Iterable<Property> get properties {
26+
return [
27+
Property(
28+
name: 'text',
29+
value: () => _text,
30+
onChange: (value) {
31+
_text = value;
32+
},
33+
),
34+
Property(
35+
name: 'fontSize',
36+
value: () => _fontSize,
37+
onChange: (value) {
38+
_fontSize = value;
39+
},
40+
),
41+
];
42+
}
43+
}
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
import 'dart:ui';
22

33
import '../mixin/icon_box_mixin.dart';
4+
import '../pattern/property.dart';
45
import '../pattern/tool_factory.dart';
56
import '../pattern/shape.dart';
67
import '../shapes/triangle_shape.dart';
78

89
class TriangleFactory extends ToolFactory with IconBoxMixin {
10+
var _isFilled = false;
11+
var _sideLength = 120.0;
12+
913
@override
1014
Shape createShape(double x, double y, Color color) {
1115
return TriangleShape(
12-
sideLength: 120,
16+
sideLength: _sideLength,
17+
isFilled: _isFilled,
1318
x: x,
1419
y: y,
1520
color: color,
1621
);
1722
}
23+
24+
@override
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+
];
1841
}

0 commit comments

Comments
 (0)