Skip to content

Commit

Permalink
Merge pull request #26 from dwyl/completed-tasks-#25
Browse files Browse the repository at this point in the history
Completed tasks
  • Loading branch information
nelsonic committed May 26, 2021
2 parents 288d0ff + 6344af7 commit deca900
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ coverage/
lib/generated_plugin_registrant.dart
# test file for coverage
test/coverage_helper_test.dart
.flutter_plugins
.flutter-plugins
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ Note that the `Column` and `Exapanded` widgets are "space" widgets.
Flutter provide a widget inspector where you can see the full tree
of the application:

![widget tree](https://user-images.githubusercontent.com/6057298/93480078-f6876b00-f8f4-11ea-95df-3c81321e8284.png)
![widget tree](https://user-images.githubusercontent.com/6057298/93480078-f6876b00-f8f4-11ea-95df-3c81321e8284.png)
6 changes: 2 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ import 'package:flutter/material.dart';
import 'package:todolist/screens/tasks/tasks.dart';

void main() {
return runApp(
MaterialApp(title: 'TodoList', home: Tasks())
);
}
return runApp(MaterialApp(title: 'TodoList', home: Tasks()));
}
4 changes: 4 additions & 0 deletions lib/models/todoList.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ class TodoListModel extends ChangeNotifier {
final json = jsonEncode(tasks);
prefs.setString('tasks', json);
}

List<TaskModel> getCompletedTasks() {
return tasks.where((t) => t.completed == true).toList();
}
}
22 changes: 22 additions & 0 deletions lib/screens/completed_tasks/completed_tasks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:flutter/material.dart';
import 'package:todolist/models/todoList.dart';

class CompletedTasks extends StatelessWidget {
final TodoListModel todoList;

CompletedTasks({this.todoList});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Completed Items'),
),
body: ListView(
children: todoList.getCompletedTasks().map((t) {
return Container(
height: 50,
child: Center(child: Text(t.text, style: TextStyle(fontSize: 20))));
}).toList()));
}
}
25 changes: 22 additions & 3 deletions lib/screens/tasks/tasks.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import 'package:provider/provider.dart';
import 'package:todolist/models/todoList.dart';
import 'package:todolist/screens/completed_tasks/completed_tasks.dart';
import 'package:todolist/screens/tasks/todolist.dart';
import 'package:flutter/material.dart';

class Tasks extends StatelessWidget {
// display completed tasks screen
void _goToCompletedTasks(context, todoList) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CompletedTasks(todoList: todoList)));
}

@override
Widget build(BuildContext context) {
// get tasks from shared preferences
final TodoListModel todoList = TodoListModel();
// getTasksFromSharedPrefs call notifyListeners
todoList.getTasksFromSharedPrefs();

return Scaffold(
appBar: AppBar(
title: Text('TodoList'),
actions: [
Padding(
padding: EdgeInsets.only(right: 20.0),
child: IconButton(
icon: Icon(Icons.check),
onPressed: () => _goToCompletedTasks(context, todoList)))
],
),
body: ChangeNotifierProvider.value(
value: todoList,
child: TodoListWidget() ,)
);
child: TodoListWidget(),
));
}
}
}
6 changes: 4 additions & 2 deletions lib/screens/tasks/todolist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ class TodoListWidget extends StatelessWidget {
);
})),
Consumer<TodoListModel>(
builder: (contexst, tasks, child) {
builder: (context, tasks, child) {
return TextField(
controller: _controller,
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.teal)),
labelText: 'new task'),
onSubmitted: (newTask) {
tasks.addTaks(TaskModel(text: newTask)); // create new instance of task changeNotifier model
tasks.addTaks(TaskModel(
text:
newTask)); // create new instance of task changeNotifier model
_controller.clear(); // clear the text input after adding taks
tasks.saveTasksToSharedPrefs();
},
Expand Down
29 changes: 29 additions & 0 deletions test/completed_tasks_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:todolist/models/task.dart';
import 'package:todolist/models/todoList.dart';
import 'package:todolist/screens/completed_tasks/completed_tasks.dart';

void main() {
testWidgets('Test Todolist widget', (WidgetTester tester) async {
final todoList = TodoListModel();
final task = TaskModel(text: "task 1");
final task2 = TaskModel(text: "task 2");
final task3 = TaskModel(text: "task 3", completed: true);
todoList.addTaks(task);
todoList.addTaks(task2);
todoList.addTaks(task3);
await tester.pumpWidget(
MaterialApp(
home: CompletedTasks(todoList: todoList )
),
);

final textTask1 = find.text('task 1');
final textTask2 = find.text('task 2');
final textTask3 = find.text('task 3');
expect(textTask1, findsNothing);
expect(textTask2, findsNothing);
expect(textTask3, findsOneWidget);
});
}
9 changes: 9 additions & 0 deletions test/tasks_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@ void main() {
// check input field to create new task exists
final textField = find.byType(TextField);
expect(textField, findsOneWidget);

// find completed icon
final completedIcon = find.byType(Icon);
expect(completedIcon, findsOneWidget);
await tester.tap(completedIcon);
await tester.pumpAndSettle();
final titleCompletedScreen = find.text('Completed Items');
expect(titleCompletedScreen, findsOneWidget);

});
}

0 comments on commit deca900

Please sign in to comment.