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 [Link](query);
return rowId;
Future<int> insertDriver(DriverModel driver) async {
Database db = await database;
int rowId = await [Link]('Driver', [Link]());
return rowId;
Future<List<Map<String, dynamic>>> allDrivers() async {
Database db = await database;
List<Map<String, dynamic>> rows =
await [Link]('Driver', orderBy: "isDeleted");
return rows;
Future<List<DriverModel>> getAllDrivers() async {
Database db = await database;
List<Map<String, dynamic>> rows = await [Link]('Driver');
// List<DriverModel> driver_list=[];
// for(int i=0;i<[Link];i++)
// {
// driver_list.add([Link](rows[i]));
// }
// return driver_list;
return [Link]((row) => [Link](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 [Link](query);
// return id;
Map<String, dynamic> updateMap = {"isDeleted": 1};
// [Link]('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 [Link],
required [Link],
required [Link],
required [Link],
[Link] = "",
[Link] = 0,
});
// Factory constructor for creating a DriverModel from a Map
[Link](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({[Link]});
@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 [Link]();
filterList = rows;
setState(() {});
@override
void initState() {
fetchAllDrivers();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Drivers'),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
await [Link](context, MaterialPageRoute(builder: (context) {
return NewDriver();
}));
fetchAllDrivers();
},
child: Text(
'+',
style: TextStyle(fontSize: 40),
),
),
body: Padding(
padding: const [Link](8.0),
child: Column(
children: [
SingleChildScrollView(
scrollDirection: [Link],
child: Row(
children: [
GestureDetector(
onTap: () {
selection = 1;
setState(() {
filterList = rows;
});
},
child: Container(
height: 50,
width: 100,
decoration: BoxDecoration(
border: [Link](),
borderRadius: [Link](5),
color: selection == 1 ? [Link] : [Link]),
child: Center(
child: Text(
'All',
style: TextStyle(color: [Link], fontSize: 30),
)),
),
),
GestureDetector(
onTap: () {
selection = 2;
setState(() {
filterList = rows
.where((element) => element["isDeleted"] == 0)
.toList();
});
},
child: Container(
height: 50,
width: 150,
decoration: BoxDecoration(
border: [Link](),
borderRadius: [Link](5),
color: selection == 2 ? [Link] : [Link]),
child: Center(
child: Text(
'Working',
style: TextStyle(color: [Link], fontSize: 30),
)),
),
),
GestureDetector(
onTap: () {
selection = 3;
setState(() {
filterList = rows
.where((element) => element["isDeleted"] == 1)
.toList();
});
},
child: Container(
height: 50,
width: 150,
decoration: BoxDecoration(
border: [Link](),
borderRadius: [Link](5),
color: selection == 3 ? [Link] : [Link]),
child: Center(
child: Text(
'Left',
style: TextStyle(color: [Link], fontSize: 30),
)),
),
),
],
),
),
SizedBox(
height: 10,
),
Expanded(
child: Container(
child: listView(),
))
],
),
));
ListView listView() {
return [Link](
itemCount: [Link],
itemBuilder: (context, index) {
Map<String, dynamic> row = filterList[index];
return Card(
color: row["isDeleted"] == 0
? const [Link](255, 227, 203, 232)
: const [Link](255, 233, 148, 142),
elevation: 2,
margin: [Link](5),
child: Row(
mainAxisAlignment: [Link],
children: [
Column(
crossAxisAlignment: [Link],
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 [Link]
.delteDriver(row["cnic"]);
[Link](context);
},
child: Text('Yes')),
TextButton(
onPressed: () {
[Link](context);
},
child: Text('No')),
],
);
});
fetchAllDrivers();
},
icon: Icon([Link]))
],
),
);
});
NewDriver
import 'package:flutter/[Link]';
import 'package:travelagency/DBHelper/[Link]';
class NewDriver extends StatefulWidget {
const NewDriver({[Link]});
@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: [Link](10),
child: Column(
children: [
TextFormField(
controller: nameController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Name',
labelText: 'Name'),
),
SizedBox(
height: 10,
),
TextFormField(
controller: nicController,
keyboardType: [Link],
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'NIC',
labelText: 'NIC'),
),
SizedBox(
height: 10,
),
TextFormField(
controller: licensenumConroller,
keyboardType: [Link],
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'License#',
labelText: 'License#'),
),
SizedBox(
height: 10,
),
TextFormField(
controller: contactController,
keyboardType: [Link],
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Contact',
labelText: 'Contact'),
),
SizedBox(
height: 10,
),
ElevatedButton(
onPressed: () async {
int rowId = await [Link](
[Link],
[Link],
[Link],
[Link]);
if (rowId > 0) {
print('Data inserted..');
} else {
print('Error in insertion');
},
child: Text('Save'))
], ), ) ; ); }}
HomeScreen
import 'package:flutter/[Link]';
import 'package:travelagency/Screens/[Link]';
class HomeScreen extends StatelessWidget {
const HomeScreen({[Link]});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ABC Travel Agency Home'),
),
body: Padding(
padding: [Link](10),
child: Column(
children: [
SizedBox(
height: 50,
width: 200,
child: ElevatedButton(
onPressed: () {
[Link](context,
MaterialPageRoute(builder: (context) {
return AllDriverScreen();
}));
},
child: Text(
'Driver',
style: TextStyle(fontSize: 20),
)), ) ], ), ), ); }}
HomeScreen
import 'package:flutter/[Link]';
import 'package:travelagency/Screens/[Link]';
void main() {
runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({[Link]});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
appBarTheme: AppBarTheme(
backgroundColor: [Link],
titleTextStyle: TextStyle(
color: [Link],
fontSize: 20,
),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: [Link](
foregroundColor: [Link], // Text color
backgroundColor: [Link], // Button color
padding: [Link](horizontal: 24, vertical: 12),
textStyle: TextStyle(
fontSize: 16,
),
shape: RoundedRectangleBorder(
borderRadius: [Link](8),
),
),
),
textTheme: TextTheme(
bodyLarge: TextStyle(fontSize: 14.0, color: [Link]),
bodyMedium: TextStyle(fontSize: 14.0, color: [Link]),
bodySmall: TextStyle(fontSize: 14.0, color: Colors.black38),
),
colorScheme: [Link](seedColor: [Link]),
useMaterial3: true,
),
home: HomeScreen());