Skip to content

Commit

Permalink
Updated example project by the actions menu to allow filter tasks by …
Browse files Browse the repository at this point in the history
…type.
  • Loading branch information
dkaera committed Jul 20, 2022
1 parent 1c4e155 commit 9802af1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion example/lib/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 47 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FloorApp extends StatelessWidget {
}
}

class TasksWidget extends StatelessWidget {
class TasksWidget extends StatefulWidget {
final String title;
final TaskDao dao;

Expand All @@ -42,35 +42,72 @@ class TasksWidget extends StatelessWidget {
required this.dao,
}) : super(key: key);

@override
State<StatefulWidget> createState() => TasksWidgetState();
}

class TasksWidgetState extends State<TasksWidget> {
TaskType? _selectedType;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[
PopupMenuButton<int>(
itemBuilder: (context) {
return List.generate(4, (index) {
return PopupMenuItem<int>(
value: index,
child: Text(
index == 0 ? 'All' : _getMenuType(index).title,
),
);
});
},
onSelected: (index) {
setState(() {
_selectedType = index == 0 ? null : _getMenuType(index);
});
},
)
],
),
body: SafeArea(
child: Column(
children: <Widget>[
TasksListView(dao: dao),
TasksTextField(dao: dao),
TasksListView(
dao: widget.dao,
selectedType: _selectedType,
),
TasksTextField(dao: widget.dao),
],
),
),
);
}

TaskType _getMenuType(int index) => TaskType.values[index - 1];
}

class TasksListView extends StatelessWidget {
final TaskDao dao;
final TaskType? selectedType;

const TasksListView({
Key? key,
required this.dao,
required this.selectedType,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Expanded(
child: StreamBuilder<List<Task>>(
stream: dao.findAllTasksAsStream(),
stream: selectedType == null
? dao.findAllTasksAsStream()
: dao.findAllTasksByTypeAsStream(selectedType!),
builder: (_, snapshot) {
if (!snapshot.hasData) return Container();

Expand Down Expand Up @@ -141,12 +178,11 @@ class TaskListCell extends StatelessWidget {
statusMessage = 'Removed task';
break;
case DismissDirection.startToEnd:
const taskTypes = TaskType.values;
final nextIndex = task.type.index + 1;
final newType =
taskTypes[taskTypes.length == nextIndex ? 0 : nextIndex];
await dao.updateTask(task.copy(type: newType));
statusMessage = 'Updated task status by: ${newType.title}';
final tasksLength = TaskType.values.length;
final nextIndex = (tasksLength + task.type.index + 1) % tasksLength;
final taskCopy = task.copy(type: TaskType.values[nextIndex]);
await dao.updateTask(taskCopy);
statusMessage = 'Updated task status by: ${taskCopy.type.title}';
break;
default:
break;
Expand Down
2 changes: 1 addition & 1 deletion example/lib/task_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class TaskDao {
Stream<List<Task>> findAllTasksAsStream();

@Query('SELECT * FROM task WHERE type = :type')
Stream<List<Task>> findAllTasksByType(TaskType type);
Stream<List<Task>> findAllTasksByTypeAsStream(TaskType type);

@insert
Future<void> insertTask(Task task);
Expand Down

0 comments on commit 9802af1

Please sign in to comment.