-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
180 lines (161 loc) · 5.27 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:sqlite3_async/sqlite3_async.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
initialRoute: LoadingPage.route,
routes: {
LoadingPage.route: (context) => const LoadingPage(),
HomePage.route: (context) =>
const HomePage(title: 'Flutter Demo Home Page'),
},
);
}
}
class HomePage extends StatefulWidget {
static const String route = "/home";
const HomePage({super.key, required this.title});
final String title;
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final myController = TextEditingController();
final formKey = GlobalKey<FormState>();
List<String> items = [];
@override
void dispose() {
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Form(
key: formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
TextFormField(
controller: myController,
decoration: const InputDecoration(labelText: "Item"),
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter some text";
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: ElevatedButton(
onPressed: insertItem,
child: const Text("Submit"),
),
),
],
),
),
Text("Items: ${items.toString()}"),
],
),
),
),
);
}
void insertItem() async {
if (formKey.currentState!.validate()) {
await StorageUtils.addItem(myController.text);
var readItems = await StorageUtils.readItems();
setState(() {
items = readItems;
});
}
}
}
class LoadingPage extends StatelessWidget {
static const String route = "/";
const LoadingPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
FutureBuilder<void>(
future: StorageUtils.init(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.none ||
snapshot.connectionState == ConnectionState.waiting) {
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox.fromSize(
size: const Size(16, 16),
child: const CircularProgressIndicator(),
),
],
);
}
SchedulerBinding.instance.addPostFrameCallback((_) {
Navigator.pushReplacementNamed(context, HomePage.route);
});
return Container();
}),
],
),
),
);
}
}
class StorageUtils {
static AsyncDatabase? _itemsDb;
static Future<void> init() async {
var databaseDir = await path_provider.getApplicationSupportDirectory();
if (!await databaseDir.exists()) {
await databaseDir.create(recursive: true);
}
var dbPath = path.join(databaseDir.path, "items.db");
_itemsDb = await AsyncDatabase.open(dbPath);
await _itemsDb!.execute("DROP TABLE IF EXISTS items");
await _itemsDb!.execute("CREATE TABLE items("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT NOT NULL)");
}
static Future<void> addItem(String name) {
return _itemsDb!.execute("INSERT INTO items (name) VALUES (?)", [name]);
}
static Future<List<String>> readItems() async {
return (await _itemsDb!.select("SELECT * FROM items"))
.map((e) => e["name"].toString())
.toList();
}
}