GLOBAL FUNCTION
Future<int> addDriver(
String name, String nic, String license, String contact) async {
Database db = await database;
String query = '''
insert into Driver values ('$nic','$name','$license','$contact','',0)
''';
int rowId = await db.rawInsert(query);
return rowId;
Future<int> insertDriver(DriverModel driver) async {
Database db = await database;
int rowId = await db.insert('Driver', driver.toMap());
return rowId;
Future<List<Map<String, dynamic>>> allDrivers() async {
Database db = await database;
List<Map<String, dynamic>> rows =
await db.query('Driver', orderBy: "isDeleted");
return rows;
Future<List<DriverModel>> getAllDrivers() async {
Database db = await database;
List<Map<String, dynamic>> rows = await db.query('Driver');
// List<DriverModel> driver_list=[];
// for(int i=0;i<rows.length;i++)
// {
// driver_list.add(DriverModel.fromMap(rows[i]));
// }
// return driver_list;
return rows.map((row) => DriverModel.fromMap(row)).toList();
Future<int> delteDriver(String nic) async {
Database db = await database;
// String query='''
// update driver set isDelted=1 where cnic='${nic}'
// ''';
// int id=await db.rawUpdate(query);
// return id;
Map<String, dynamic> updateMap = {"isDeleted": 1};
// db.delete('Driver',where: "cnic=?",whereArgs: [nic]);
int id = await db
.update('Driver', updateMap, where: "cnic = ?", whereArgs: [nic]);
return id;
DriverModel
class DriverModel {
late String nic, name, license, contact, image;
late int isDeleted;
// Constructor
DriverModel({
required this.contact,
required this.name,
required this.nic,
required this.license,
this.image = "",
this.isDeleted = 0,
});
// Factory constructor for creating a DriverModel from a Map
DriverModel.fromMap(Map<String, dynamic> row) {
// Ensure the fields are safely assigned, and provide defaults for possible nulls
nic = row["cnic"] ?? '';
name = row["name"] ?? '';
license = row["licensenum"] ?? '';
contact = row["contact"] ?? '';
image = row["image"] ?? ''; // Default empty string if null
isDeleted = row["isDeleted"] ?? 0; // Default to 0 if null
// Method to convert DriverModel to a Mapy
Map<String, dynamic> toMap() {
return {
"cnic": nic,
"name": name,
"licensenum": license,
"contact": contact,
"image": image,
"isDeleted": isDeleted,
};
AllDriverScreen
class AllDriverScreen extends StatefulWidget {
const AllDriverScreen({super.key});
@override
State<AllDriverScreen> createState() => _AllDriverScreenState();
}
class _AllDriverScreenState extends State<AllDriverScreen> {
List<Map<String, dynamic>> rows = [];
List<Map<String, dynamic>> filterList = [];
int selection = 1; // 1 for all , 2 for working , 3 for left
fetchAllDrivers() async {
rows = await DBHelper.instance.allDrivers();
filterList = rows;
setState(() {});
@override
void initState() {
fetchAllDrivers();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Drivers'),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
await Navigator.push(context, MaterialPageRoute(builder: (context) {
return NewDriver();
}));
fetchAllDrivers();
},
child: Text(
'+',
style: TextStyle(fontSize: 40),
),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
GestureDetector(
onTap: () {
selection = 1;
setState(() {
filterList = rows;
});
},
child: Container(
height: 50,
width: 100,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(5),
color: selection == 1 ? Colors.blue : Colors.grey),
child: Center(
child: Text(
'All',
style: TextStyle(color: Colors.white, fontSize: 30),
)),
),
),
GestureDetector(
onTap: () {
selection = 2;
setState(() {
filterList = rows
.where((element) => element["isDeleted"] == 0)
.toList();
});
},
child: Container(
height: 50,
width: 150,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(5),
color: selection == 2 ? Colors.blue : Colors.grey),
child: Center(
child: Text(
'Working',
style: TextStyle(color: Colors.white, fontSize: 30),
)),
),
),
GestureDetector(
onTap: () {
selection = 3;
setState(() {
filterList = rows
.where((element) => element["isDeleted"] == 1)
.toList();
});
},
child: Container(
height: 50,
width: 150,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(5),
color: selection == 3 ? Colors.blue : Colors.grey),
child: Center(
child: Text(
'Left',
style: TextStyle(color: Colors.white, fontSize: 30),
)),
),
),
],
),
),
SizedBox(
height: 10,
),
Expanded(
child: Container(
child: listView(),
))
],
),
));
ListView listView() {
return ListView.builder(
itemCount: filterList.length,
itemBuilder: (context, index) {
Map<String, dynamic> row = filterList[index];
return Card(
color: row["isDeleted"] == 0
? const Color.fromARGB(255, 227, 203, 232)
: const Color.fromARGB(255, 233, 148, 142),
elevation: 2,
margin: EdgeInsets.all(5),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
row["name"],
style: TextStyle(fontSize: 20),
),
Text(
"NIC: ${row["cnic"]}",
style: TextStyle(fontSize: 20),
],
),
SizedBox(
width: 5,
),
IconButton(
onPressed: () async {
await showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Are you sure to delete?'),
actions: [
TextButton(
onPressed: () async {
await DBHelper.instance
.delteDriver(row["cnic"]);
Navigator.pop(context);
},
child: Text('Yes')),
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('No')),
],
);
});
fetchAllDrivers();
},
icon: Icon(Icons.delete))
],
),
);
});
NewDriver
import 'package:flutter/material.dart';
import 'package:travelagency/DBHelper/dbhelper.dart';
class NewDriver extends StatefulWidget {
const NewDriver({super.key});
@override
State<NewDriver> createState() => _NewDriverState();
}
class _NewDriverState extends State<NewDriver> {
TextEditingController nicController = TextEditingController();
TextEditingController nameController = TextEditingController();
TextEditingController licensenumConroller = TextEditingController();
TextEditingController contactController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('New Driver'),
),
body: Padding(
padding: EdgeInsets.all(10),
child: Column(
children: [
TextFormField(
controller: nameController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Name',
labelText: 'Name'),
),
SizedBox(
height: 10,
),
TextFormField(
controller: nicController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'NIC',
labelText: 'NIC'),
),
SizedBox(
height: 10,
),
TextFormField(
controller: licensenumConroller,
keyboardType: TextInputType.number,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'License#',
labelText: 'License#'),
),
SizedBox(
height: 10,
),
TextFormField(
controller: contactController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Contact',
labelText: 'Contact'),
),
SizedBox(
height: 10,
),
ElevatedButton(
onPressed: () async {
int rowId = await DBHelper.instance.addDriver(
nameController.text,
nicController.text,
licensenumConroller.text,
contactController.text);
if (rowId > 0) {
print('Data inserted..');
} else {
print('Error in insertion');
},
child: Text('Save'))
], ), ) ; ); }}
HomeScreen
import 'package:flutter/material.dart';
import 'package:travelagency/Screens/alldriver.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ABC Travel Agency Home'),
),
body: Padding(
padding: EdgeInsets.all(10),
child: Column(
children: [
SizedBox(
height: 50,
width: 200,
child: ElevatedButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return AllDriverScreen();
}));
},
child: Text(
'Driver',
style: TextStyle(fontSize: 20),
)), ) ], ), ), ); }}
HomeScreen
import 'package:flutter/material.dart';
import 'package:travelagency/Screens/home.dart';
void main() {
runApp(const MyApp());
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(
appBarTheme: AppBarTheme(
backgroundColor: Colors.blue,
titleTextStyle: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white, // Text color
backgroundColor: Colors.blue, // Button color
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
textStyle: TextStyle(
fontSize: 16,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
textTheme: TextTheme(
bodyLarge: TextStyle(fontSize: 14.0, color: Colors.black),
bodyMedium: TextStyle(fontSize: 14.0, color: Colors.black),
bodySmall: TextStyle(fontSize: 14.0, color: Colors.black38),
),
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: HomeScreen());