Skip to content

Commit

Permalink
Configuraçao do database
Browse files Browse the repository at this point in the history
  • Loading branch information
GamaGustavo committed Feb 3, 2022
1 parent 05f4f10 commit 6d0bd01
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 9 deletions.
46 changes: 46 additions & 0 deletions lib/database/app_database.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:byte_banck/models/contact.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

Future<Database> createDatabase() {
return getDatabasesPath().then((dbPath) {
final String path = join(dbPath, 'bytebanck.db');
return openDatabase(path, onCreate: (database, version) {
database.execute(
'CREATE TABLE contacts( '
'id INTEGER PRIMARY KEY, '
'full_name TEXT, '
'account_number INTEGER)');
}, version: 1);
});
}

Future<int> save(Contact contact) {
return createDatabase().then((database) {
Map<String, dynamic> contactMap = {};
contactMap['full_name'] = contact.fullName;
contactMap['account_number'] = contact.accountNumber;
return database.insert('contacts', contactMap);
});
}

Future<List<Contact>> findAll() {
return createDatabase().then((database) {
return database.query('contacts').then((maps) {
{
List<Contact> contacts = [];
for (Map<String, dynamic> map in maps) {
contacts.add(
Contact(
map['id'],
map['full_name'],
map['account_number'],
),
);
}
return contacts;
}
});
}
);
}
8 changes: 7 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import 'package:byte_banck/database/app_database.dart';
import 'package:flutter/material.dart';

import 'dashboard.dart';
import 'screans/dashboard.dart';

void main() {
runApp(const ByteBanck());

findAll().then((value) {
debugPrint(value.toString());
});

}

class ByteBanck extends StatelessWidget {
Expand Down
16 changes: 11 additions & 5 deletions lib/models/contact.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
class Contact {
final String fullname;
final int acountNumbe;
final int id;
final String fullName;
final int accountNumber;

Contact(
this.fullname,
this.acountNumbe,
this.id,
this.fullName,
this.accountNumber,
);

@override
String toString() {
return 'Contact{fullname: $fullname, acountNumbe: $acountNumbe}';
return '\nContact: {'
'id: $id, '
'fullName: $fullName, '
'acountNumbe: $accountNumber'
'}';
}
}
4 changes: 2 additions & 2 deletions lib/contact_form.dart → lib/screans/contact_form.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';

import 'models/contact.dart';
import '../models/contact.dart';

class ContactForm extends StatefulWidget {
const ContactForm({Key? key}) : super(key: key);
Expand Down Expand Up @@ -61,7 +61,7 @@ class _ContactFormState extends State<ContactForm> {
onPressed: () {
final String fullName = _fullName.text;
final int acountNumbe = int.parse(_acountNumber.text);
Navigator.of(context).pop(Contact(fullName,acountNumbe));
Navigator.of(context).pop(Contact(0,fullName,acountNumbe));
},
child: const Text('Create'),
),
Expand Down
File renamed without changes.
File renamed without changes.
24 changes: 23 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ packages:
source: hosted
version: "1.7.0"
path:
dependency: transitive
dependency: "direct main"
description:
name: path
url: "https://pub.dartlang.org"
Expand All @@ -114,6 +114,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
sqflite:
dependency: "direct main"
description:
name: sqflite
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
sqflite_common:
dependency: transitive
description:
name: sqflite_common
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
stack_trace:
dependency: transitive
description:
Expand All @@ -135,6 +149,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
synchronized:
dependency: transitive
description:
name: synchronized
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
term_glyph:
dependency: transitive
description:
Expand Down Expand Up @@ -165,3 +186,4 @@ packages:
version: "2.1.1"
sdks:
dart: ">=2.15.1 <3.0.0"
flutter: ">=1.10.0"
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ environment:
dependencies:
flutter:
sdk: flutter
sqflite:
path:


# The following adds the Cupertino Icons font to your application.
Expand Down

0 comments on commit 6d0bd01

Please sign in to comment.