Line data Source code
1 : import 'package:flutter/material.dart';
2 : import 'package:styx/styx.dart';
3 :
4 3 : final system = EntitySystem();
5 :
6 0 : void main() {
7 : // ignore: unused_local_variable
8 0 : var entity = system.create();
9 0 : entity += Counter(0);
10 0 : entity += Name('Flutter');
11 0 : runApp(const Entry());
12 : }
13 :
14 : class Entry extends StatelessWidget {
15 2 : const Entry({Key? key}) : super(key: key);
16 :
17 1 : @override
18 : Widget build(BuildContext context) {
19 : return const MaterialApp(
20 : debugShowCheckedModeBanner: false,
21 : home: Home(),
22 : );
23 : }
24 : }
25 :
26 : class Home extends StatelessWidget {
27 1 : const Home({Key? key}) : super(key: key);
28 :
29 : final EntityMatcher counterMatcher = const EntityMatcher(any: {Counter});
30 : final EntityMatcher personMatcher = const EntityMatcher(any: {Name});
31 :
32 1 : @override
33 : Widget build(BuildContext context) {
34 1 : return Scaffold(
35 1 : appBar: AppBar(title: const Text('Counter Example')),
36 1 : body: Column(
37 : mainAxisAlignment: MainAxisAlignment.center,
38 1 : children: [
39 1 : StreamBuilder<List<Entity>>(
40 1 : builder: (context, snapshot) {
41 1 : if (snapshot.hasData) {
42 0 : final counter = snapshot.data!.firstWhere(counterMatcher.matches);
43 0 : return StreamBuilder<int>(
44 0 : stream: counter.get<Counter>().value.stream,
45 0 : builder: (context, snapshot) {
46 0 : if (snapshot.hasData) {
47 0 : return Text('Counter: ${counter.get<Counter>().value()}');
48 : } else {
49 : return const Text('Loading...');
50 : }
51 : },
52 : );
53 : }
54 :
55 : return const SizedBox.shrink();
56 : },
57 3 : stream: system.entities.stream,
58 : ),
59 1 : StreamBuilder<List<Entity>>(
60 3 : stream: system.entities.stream,
61 1 : builder: (context, snapshot) {
62 1 : if (snapshot.hasData) {
63 0 : final person = snapshot.data!.firstWhere(personMatcher.matches);
64 0 : final counter = snapshot.data!.firstWhere(counterMatcher.matches);
65 0 : return Column(
66 0 : children: [
67 0 : StreamBuilder(
68 0 : stream: counter.get<Counter>().value.stream,
69 0 : builder: (context, snapshot) {
70 0 : if (snapshot.hasData) {
71 0 : return Text('Counter: ${counter.get<Counter>().value()}');
72 : } else {
73 : return const Text('Loading...');
74 : }
75 : },
76 : ),
77 0 : TextFormField(
78 0 : initialValue: person.get<Name>().value(),
79 0 : onChanged: (value) => person.get<Name>().value(value),
80 : decoration: const InputDecoration(
81 : labelText: 'Name',
82 : ),
83 : ),
84 : ],
85 : );
86 : }
87 : return const SizedBox.shrink();
88 : },
89 : ),
90 : ],
91 : ),
92 1 : floatingActionButton: StreamBuilder<List<Entity>>(
93 3 : stream: system.entities.stream,
94 1 : builder: (context, snapshot) {
95 1 : if (snapshot.hasData) {
96 0 : final counter = snapshot.data!.firstWhere(counterMatcher.matches);
97 0 : return FloatingActionButton(
98 0 : onPressed: counter.get<Counter>().increment,
99 : child: const Icon(Icons.add),
100 : );
101 : }
102 : return const SizedBox.shrink();
103 : },
104 : ),
105 : );
106 : }
107 : }
108 :
109 : class Counter extends Component {
110 0 : Counter(int initial) {
111 0 : value(initial);
112 : }
113 :
114 : final value = 0.bs;
115 :
116 0 : @override
117 : void onRemoved() {
118 0 : value.close();
119 : }
120 :
121 0 : void increment() {
122 0 : value(value() + 1);
123 : }
124 :
125 0 : void decrement() {
126 0 : value(value() - 1);
127 : }
128 : }
129 :
130 : class Name extends Component {
131 0 : Name(String initial) {
132 0 : value(initial);
133 : }
134 :
135 : final value = ''.bs;
136 :
137 0 : @override
138 : void onRemoved() {
139 0 : value.close();
140 : }
141 : }
|