All_Project: Refactored 💫

This commit is contained in:
Hadi Mottale 2025-06-11 11:37:51 +03:30
parent 4b31ec316a
commit f847a97d03
3 changed files with 54 additions and 61 deletions

View File

@ -1,6 +1,7 @@
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:path/path.dart';
import '../models/contact.dart'; import '../models/contact.dart';
import 'package:path/path.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
class DatabaseHelper { class DatabaseHelper {
static final DatabaseHelper _instance = DatabaseHelper._internal(); static final DatabaseHelper _instance = DatabaseHelper._internal();

View File

@ -1,6 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:contacts_dataist_ir/screens/contact_list_screen.dart';
void main() { void main() {
WidgetsFlutterBinding.ensureInitialized();
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
runApp(const MyApp()); runApp(const MyApp());
} }
@ -10,34 +15,12 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Contacts Dataist IR',
theme: ThemeData( theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
), ),
home: const MyHomePage(title: ''), home: const ContactListScreen(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(child: Column()),
); );
} }
} }

View File

@ -1,5 +1,5 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:contacts_dataist_ir/models/contact.dart'; import 'package:contacts_dataist_ir/models/contact.dart';
import 'package:contacts_dataist_ir/database/database_helper.dart'; import 'package:contacts_dataist_ir/database/database_helper.dart';
@ -48,40 +48,49 @@ class _ContactListScreenState extends State<ContactListScreen> {
title: const Text('Contacts Dataist IR'), title: const Text('Contacts Dataist IR'),
backgroundColor: Theme.of(context).colorScheme.primary, backgroundColor: Theme.of(context).colorScheme.primary,
), ),
body: _isLoading body:
? const Center(child: CircularProgressIndicator()) _isLoading
: _contacts.isEmpty ? const Center(child: CircularProgressIndicator())
? const Center( : _contacts.isEmpty
child: Text('No contacts found. Add a new one!', style: TextStyle(fontSize: 18)), ? const Center(
) child: Text(
: ListView.builder( 'No contacts found. Add a new one!',
itemCount: _contacts.length, style: TextStyle(fontSize: 18),
itemBuilder: (context, index) { ),
final contact = _contacts[index]; )
return Card( : ListView.builder(
margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 5), itemCount: _contacts.length,
child: ListTile( itemBuilder: (context, index) {
title: Text(contact.name), final contact = _contacts[index];
subtitle: Text(contact.phone ?? contact.email ?? 'No phone or email'), return Card(
onTap: () { margin: const EdgeInsets.symmetric(
if (kDebugMode) { horizontal: 10,
print('Tapped on ${contact.name}'); vertical: 5,
} ),
}, child: ListTile(
trailing: IconButton( title: Text(contact.name),
icon: const Icon(Icons.delete, color: Colors.red), subtitle: Text(
onPressed: () async { contact.phone ?? contact.email ?? 'No phone or email',
if (kDebugMode) { ),
print('Delete ${contact.name}'); onTap: () {
} if (kDebugMode) {
await _dbHelper.deleteContact(contact.id!); print('Tapped on ${contact.name}');
_loadContacts(); }
},
trailing: IconButton(
icon: const Icon(Icons.delete, color: Colors.red),
onPressed: () async {
if (kDebugMode) {
print('Delete ${contact.name}');
}
await _dbHelper.deleteContact(contact.id!);
_loadContacts();
},
),
),
);
}, },
), ),
),
);
},
),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () { onPressed: () {
if (kDebugMode) { if (kDebugMode) {
@ -92,4 +101,4 @@ class _ContactListScreenState extends State<ContactListScreen> {
), ),
); );
} }
} }