-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main.dart
166 lines (150 loc) · 4.68 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// ignore_for_file: prefer_interpolation_to_compose_strings
import 'package:flutter/material.dart';
import 'package:flutter_qjs/flutter_qjs.dart';
import 'package:flutter/services.dart';
import 'package:rxdb/rxdb.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await RxDatabaseState.init('flutter-rxdb-heroes');
const app = MyApp();
runApp(app);
}
class RxHeroDocType {
final String id, name, color;
RxHeroDocType({required this.id, required this.name, required this.color});
}
class RxCollectionsOfDatabase {
late RxCollection<RxHeroDocType> heroes;
}
class RxDatabaseState {
static late RxDatabase database;
static bool initDone = false;
static late RxCollection<RxHeroDocType> collection;
static Future<RxDatabase> init(String databaseName) async {
if (initDone) {
return database;
}
initDone = true;
database = await getRxDatabase("javascript/dist/index.js", databaseName);
collection = database.getCollection<RxHeroDocType>('heroes');
return database;
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
RxQuery<RxHeroDocType> query = RxDatabaseState.collection.find({});
List<RxDocument<RxHeroDocType>> documents = [];
final nameController = TextEditingController();
final colorController = TextEditingController();
_MyHomePageState() {
query.$().listen((newResults) {
setState(() {
documents = newResults;
});
});
}
void saveNewHero() async {
print("saveNewHero() called");
var collection = RxDatabaseState.collection;
await collection.insert({
"id": "zflutter-${DateTime.now()}",
"name": nameController.text,
"color": colorController.text
});
nameController.clear();
colorController.clear();
}
void removeHero(RxDocument<RxHeroDocType> heroDocument) async {
print("removeHero() called");
await heroDocument.remove();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: [
SizedBox(
height: 400,
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: documents.length,
itemBuilder: (BuildContext ctxt, int index) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: ListTile(
key: Key(
'list-tile-' + documents[index].data['name']),
leading: Text(documents[index].data['name']),
title: Text(
'color: ' + documents[index].data['color'])),
),
IconButton(
key: Key(
'button-delete-' + documents[index].data['name']),
icon: const Icon(Icons.remove_circle),
onPressed: () {
documents[index].remove();
},
)
],
);
}),
),
SizedBox(
width: 300,
height: 100,
child: TextFormField(
key: const Key('input-name'),
controller: nameController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Name',
),
),
),
SizedBox(
width: 300,
child: TextFormField(
key: const Key('input-color'),
controller: colorController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Color',
),
),
)
],
),
floatingActionButton: FloatingActionButton(
key: const Key('button-save'),
onPressed: saveNewHero,
tooltip: 'Save',
child: const Icon(Icons.add),
),
);
}
}