Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 46 additions & 22 deletions lib/helpers/db_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,79 @@ class DBHelper {
sql.Batch batch = db.batch();
//task_list table
batch.execute(
'CREATE TABLE task_list(id TEXT PRIMARY KEY,title TEXT NOT NULL)');
'CREATE TABLE IF NOT EXISTS task_list(id TEXT PRIMARY KEY,title TEXT NOT NULL)');
//task table
batch.execute(
'CREATE TABLE task(id TEXT PRIMARY KEY,title TEXT NOT NULL,isDone INTEGER NOT NULL,task_list_id TEXT,FOREIGN KEY (task_list_id) REFERENCES task_list(id) ON UPDATE CASCADE ON DELETE CASCADE)');
'CREATE TABLE IF NOT EXISTS task(id TEXT PRIMARY KEY,title TEXT NOT NULL,isDone INTEGER NOT NULL,task_list_id TEXT,FOREIGN KEY (task_list_id) REFERENCES task_list(id) ON UPDATE CASCADE ON DELETE CASCADE)');
List<dynamic> result = await batch.commit();
result.forEach((element) {
print(element);
});
}, version: 1);
}

//Insert Data
static Future<void> insert(String table, Map<String, Object> data) async {
final db = await DBHelper.database();
await db.insert(table, data,
conflictAlgorithm: sql.ConflictAlgorithm.replace);
try{
final db = await DBHelper.database();
await db.insert(table, data,
conflictAlgorithm: sql.ConflictAlgorithm.replace);
}on sql.DatabaseException catch(e){
print(e.toString());
}
try{}on sql.DatabaseException catch(e){
print(e.toString());
}
}

//Fetch Data for Task List
static Future<List<Map<String, dynamic>>> getData(String table) async {
final db = await DBHelper.database();
return db.query(table);
List<Map<String, dynamic>> resultSet=[];
try{
final db = await DBHelper.database();
resultSet= await db.query(table);
}on sql.DatabaseException catch(e){
print(e.toString());
}
return resultSet;
}

//Fetch data for Task by their foreign key task_list_id
static Future<List<Map<String, dynamic>>> getTaskData(
String table, List<String> whereArgs) async {
final db = await DBHelper.database();
return await db
.query(table, where: 'task_list_id=?', whereArgs: [whereArgs[0]]);
List<Map<String, dynamic>> resultSet=[];
try{
final db = await DBHelper.database();
resultSet= await db
.query(table, where: 'task_list_id=?', whereArgs: [whereArgs[0]]);
}on sql.DatabaseException catch(e){
print(e.toString());
}
return resultSet;
}

//Update
//TODO:Get where and where args from the model
static Future<void> update(String table, Map<String, Object> data) async {
final db = await DBHelper.database();
await db.update(table, data,
where: 'id=?',
whereArgs: ['id'],
conflictAlgorithm: sql.ConflictAlgorithm.replace);
try{
await db.update(table, data,
where: 'id=?',
whereArgs: ['id'],
conflictAlgorithm: sql.ConflictAlgorithm.replace);
}on sql.DatabaseException catch(e){
print(e.toString());
}
}

//Delete
static Future<void> delete(String table, Map<String, Object> data) async {
final db = await DBHelper.database();
int result = await db.delete(
table,
where: 'id=?',
whereArgs: [data['id']],
);
try{
await db.delete(
table,
where: 'id=?',
whereArgs: [data['id']],
);
}on sql.DatabaseException catch(e){
print(e.toString());
}
}
}
11 changes: 6 additions & 5 deletions lib/models/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import 'dart:core';

class Task {
final String id;
final String? name;
final String name;
final String taskListId;
bool isDone;

Task(
{required this.id,
this.name,
required this.taskListId,
this.isDone = false});
{
required this.id,
required this.name,
required this.taskListId,
this.isDone = false});

void toggleDone() {
isDone = !isDone;
Expand Down
4 changes: 2 additions & 2 deletions lib/models/task_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class TaskData extends ChangeNotifier {
}

//We update the whole row
void updateTask(Task task) {
void updateTask(Task task) async{
task.toggleDone();
notifyListeners();
DBHelper.update('task', {
await DBHelper.update('task', {
'id': task.id.toString(),
'isDone': task.isDone == true ? 1 : 0,
'title': task.name.toString(),
Expand Down
1 change: 0 additions & 1 deletion lib/models/task_home_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class TaskListHome extends ChangeNotifier {
});
notifyListeners();
}

void deleteTask(TaskList task) async {
taskList.remove(task);
await DBHelper.delete('task_list', {
Expand Down
114 changes: 61 additions & 53 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:provider/provider.dart';
import 'package:u_do/models/task_home_data.dart';
import 'package:u_do/screens/preferences_screen.dart';
Expand Down Expand Up @@ -51,10 +52,6 @@ class Home extends StatelessWidget {

//This widgets contains all the TaskList for users
class TaskHome extends StatelessWidget {
final List<Map> myProducts =
List.generate(3, (index) => {"id": index, "name": "Product $index"})
.toList();

@override
Widget build(BuildContext context) {
double textSize = MediaQuery.of(context).size.width / 20;
Expand All @@ -73,8 +70,8 @@ class TaskHome extends StatelessWidget {
child: Center(
child: Text("No task group. Add a task"),
),
builder: (context, tasklistItems, child) {
return tasklistItems.taskList.length == 0
builder: (context, taskListItems, child) {
return taskListItems.taskList.length == 0
? child!
: GridView.builder(
gridDelegate:
Expand All @@ -83,56 +80,67 @@ class TaskHome extends StatelessWidget {
childAspectRatio: 3 / 3,
crossAxisSpacing: 20,
mainAxisSpacing: 20),
itemCount: tasklistItems.taskLength,
itemCount: taskListItems.taskLength,
itemBuilder: (BuildContext context, index) {
return GestureDetector(
//Delete a taskList on longPress
onLongPress: () {
Provider.of<TaskListHome>(context,
return Slidable(
key: Key(index.toString()),
actionPane: SlidableDrawerActionPane(),
actionExtentRatio: 0.25,
secondaryActions: <Widget>[
IconSlideAction(
caption: 'Delete',
color: Colors.red,
icon: Icons.delete,
onTap: () {
Provider.of<TaskListHome>(context,
listen: false)
.deleteTask(
tasklistItems.taskList[index]);
},
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => TasksScreen(
tasklistItems
.taskList[index].id!)));
},
child: Card(
elevation: 5.0,
child: Column(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Icon(
Icons.alarm,
color: Theme.of(context).canvasColor,
size: 50.0,
),
SizedBox(
height: 20.0,
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 10.0),
child: Text(
tasklistItems
.taskList[index].title!,
style: TextStyle(
color: Theme.of(context)
.canvasColor,
fontSize: textSize > 10
? textSize
: 10,
fontWeight: FontWeight.w800),
textAlign: TextAlign.center,
.deleteTask(
taskListItems.taskList[index]);
},
),
],
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => TasksScreen(
taskListItems
.taskList[index].id!)));
},
child: Card(
elevation: 5.0,
child: Column(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Icon(
Icons.alarm,
color: Theme.of(context).canvasColor,
size: 50.0,
),
SizedBox(
height: 20.0,
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 10.0),
child: Text(
taskListItems
.taskList[index].title!,
style: TextStyle(
color: Theme.of(context)
.canvasColor,
fontSize: textSize > 10
? textSize
: 10,
fontWeight: FontWeight.w800),
textAlign: TextAlign.center,
),
),
),
]),
color: Theme.of(context).accentColor,
]),
color: Theme.of(context).accentColor,
),
),
);
});
Expand Down
13 changes: 2 additions & 11 deletions lib/widgets/task_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@ import 'package:flutter/material.dart';
class TaskTile extends StatelessWidget {
final bool? isChecked;
final String? taskTitle;
final Function? checkboxCallback;
final Function? longPressCallback;

TaskTile(
{this.isChecked,
this.taskTitle,
this.checkboxCallback,
this.longPressCallback});

});
//Added new things
@override
Widget build(BuildContext context) {
return ListTile(
onLongPress: longPressCallback as void Function()?,
title: Text(
taskTitle!,
style: TextStyle(
decoration: isChecked! ? TextDecoration.lineThrough : null),
),
trailing: Checkbox(
activeColor: Theme.of(context).primaryColor,
value: isChecked,
onChanged: checkboxCallback as void Function(bool?)?,
),
);
}
}
31 changes: 22 additions & 9 deletions lib/widgets/tasks_list.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:u_do/screens/add_task_screen.dart';
import 'package:u_do/widgets/task_tile.dart';
import 'package:provider/provider.dart';
import 'package:u_do/models/task_data.dart';
Expand Down Expand Up @@ -31,15 +33,26 @@ class TasksList extends StatelessWidget {
: ListView.builder(
itemBuilder: (context, index) {
final task = taskData.tasks[index];
return TaskTile(
taskTitle: task.name,
isChecked: task.isDone,
checkboxCallback: (checkboxState) {
taskData.updateTask(task);
},
longPressCallback: () {
taskData.deleteTask(task);
},
return Slidable(
key: Key(index.toString()),
actionPane: SlidableDrawerActionPane(),
actionExtentRatio: 0.25,
secondaryActions: <Widget>[
IconSlideAction(
caption: 'Delete',
color: Colors.red,
icon: Icons.delete,
onTap: () {
Provider.of<TaskData>(context,
listen: false)
.deleteTask(task);
},
),
],
child: TaskTile(
taskTitle: task.name,
isChecked: task.isDone,
),
);
},
itemCount: taskData.taskCount,
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
provider: ^5.0.0
sqflite: ^2.0.0+3
path: ^1.8.0
flutter_slidable: ^0.6.0

dev_dependencies:
flutter_test:
Expand Down