forked from bhism/sql-app-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.dart
94 lines (91 loc) · 2.9 KB
/
add.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
import 'dart:math';
import 'package:flutter/material.dart';
import 'list.dart';
import 'dbhelper.dart';
import 'model.dart';
class AddScreen extends StatefulWidget {
@override
_AddScreenState createState() => _AddScreenState();
}
class _AddScreenState extends State<AddScreen> {
final _formKey = GlobalKey<FormState>();
String title = "";
String description = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Add todo'),
),
body: Center(
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
decoration: const InputDecoration(
hintText: 'title',
),
onChanged: (value) {
setState(() {
title = value;
});
},
),
TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
decoration: const InputDecoration(
hintText: 'Description',
),
onChanged: (value) {
setState(() {
description = value;
});
},
),
ElevatedButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
await DatabaseHandler()
.inserttodo(todo(
title: title,
description: description,
id: Random().nextInt(50)))
.whenComplete(() => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ListScreen()),
));
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Processing Data')),
);
}
},
child: const Text(
'Add',
style: TextStyle(
fontSize: 20,
),
),
),
],
),
),
),
);
}
}