Skip to content

Commit

Permalink
added delete todo
Browse files Browse the repository at this point in the history
  • Loading branch information
yagizdo committed Jul 25, 2022
1 parent 082f47b commit 035b17e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/bloc/todo_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ class TodoBloc extends Bloc<TodoEvent, TodoState> {
emit(LoadingState(false));
emit(GetTodosState(todos));
});

on<DeleteTodo>((event, emit) async {
await networkService.deleteTodo(event.todo);
emit(LoadingState(true));
todos = await networkService.getAllTodos();
emit(LoadingState(false));
emit(GetTodosState(todos));
});
}
}
6 changes: 6 additions & 0 deletions lib/bloc/todo_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ class AddTodo extends TodoEvent {

AddTodo(this.todo);
}

class DeleteTodo extends TodoEvent {
final Todo todo;

DeleteTodo(this.todo);
}
1 change: 1 addition & 0 deletions lib/core/i_network_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import '../model/todo.dart';
abstract class INetworkService {
Future<List<Todo>> getAllTodos();
Future<void> addTodo(Todo todo);
Future<void> deleteTodo(Todo todo);
}
9 changes: 9 additions & 0 deletions lib/core/network_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ class NetworkService extends NetworkClient implements INetworkService {
"Authorization": "Bearer $apiKey",
}));
}

@override
Future<void> deleteTodo(Todo todo) async {
await dio.delete('$baseUrl/v0/$appID/Todos/${todo.id}',
options: Options(headers: {
"Content-Type": "application/json",
"Authorization": "Bearer $apiKey",
}));
}
}
8 changes: 8 additions & 0 deletions lib/widget/todo_list.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:todo_app_airtable/bloc/todo_bloc.dart';

class TodoList extends StatelessWidget {
const TodoList({Key? key, required this.state}) : super(key: key);
Expand All @@ -13,6 +15,12 @@ class TodoList extends StatelessWidget {
return ListTile(
title: Text(todo.fields!.title ?? 'No title'),
subtitle: Text(todo.fields!.description ?? ''),
trailing: IconButton(
icon: const Icon(Icons.delete),
onPressed: () {
BlocProvider.of<TodoBloc>(context).add(DeleteTodo(todo));
},
),
);
});
}
Expand Down

0 comments on commit 035b17e

Please sign in to comment.