Skip to content

Commit 48f98c2

Browse files
committed
Implemented state using ChangeNotifier.
1 parent 30c95f8 commit 48f98c2

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

lib/dart_comfy_frontend.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ export 'src/workflow.dart' show Workflow;
77
export 'src/workflow/node.dart' show WorkflowNode;
88
export 'src/workflow/input.dart' show WorkflowInput;
99
export 'src/workflow/output.dart' show WorkflowOutput;
10+
export 'src/workflow/model.dart' show WorkflowModel;
11+
export 'src/workflow/view.dart' show WorkflowView;

lib/src/workflow/model.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import 'dart:convert';
2+
import 'dart:collection';
3+
import 'package:flutter/material.dart';
4+
import '../workflow.dart';
5+
import 'node.dart';
6+
7+
class WorkflowModel extends ChangeNotifier {
8+
9+
Workflow? _workflow;
10+
11+
UnmodifiableListView<WorkflowNode> get nodes => UnmodifiableListView(_workflow?.nodes ?? []);
12+
13+
// void add(Item item) {
14+
// _items.add(item);
15+
// // This call tells the widgets that are listening to this model to rebuild.
16+
// notifyListeners();
17+
// }
18+
//
19+
// /// Removes all items from the cart.
20+
// void removeAll() {
21+
// _items.clear();
22+
// // This call tells the widgets that are listening to this model to rebuild.
23+
// notifyListeners();
24+
// }
25+
26+
void loadExample() {
27+
const documentJson = '''
28+
{
29+
"last_node_id": 1,
30+
"last_link_id": 1,
31+
"nodes": [
32+
{
33+
"id": 67,
34+
"type": "Reroute",
35+
"pos": [
36+
351.7936096191406,
37+
1119.392822265625
38+
],
39+
"size": [
40+
82,
41+
26
42+
],
43+
"flags": {},
44+
"order": 95,
45+
"mode": 4,
46+
"inputs": [
47+
{
48+
"name": "",
49+
"type": "*",
50+
"link": 175
51+
}
52+
],
53+
"outputs": [
54+
{
55+
"name": "MODEL",
56+
"type": "MODEL",
57+
"links": [
58+
257
59+
],
60+
"slot_index": 0
61+
}
62+
],
63+
"properties": {
64+
"showOutputText": true,
65+
"horizontal": false
66+
},
67+
"color": "#223",
68+
"bgcolor": "#335"
69+
}
70+
71+
],
72+
"links": [],
73+
"groups": [],
74+
"config": {},
75+
"extra": {},
76+
"version": 0.4
77+
}
78+
''';
79+
80+
_workflow = Workflow.fromJSON(jsonDecode(documentJson));
81+
notifyListeners();
82+
}
83+
}

lib/src/workflow/view.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:flutter/material.dart';
2+
import '../workflow/model.dart';
3+
4+
class WorkflowView extends StatelessWidget {
5+
6+
final WorkflowModel workflow;
7+
8+
WorkflowView(this.workflow);
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
return Column(
13+
children: [
14+
Stack(
15+
children: workflow.nodes.map<Text>((item) => Text(item.type)).toList()
16+
),
17+
TextButton(
18+
onPressed: () => workflow.loadExample(),
19+
child: Text('Load example'),
20+
)
21+
]
22+
);
23+
}
24+
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ environment:
88

99
# Add regular dependencies here.
1010
dependencies:
11+
provider: ^6.1.5
1112
# path: ^1.8.0
1213

1314
dev_dependencies:

0 commit comments

Comments
 (0)