Created
March 16, 2019 18:04
-
-
Save felangel/59d0c34a84a53c2aadc8e13d3623122d to your computer and use it in GitHub Desktop.
[flutter_todo] filtered todos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/widgets.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'package:todos_app_core/todos_app_core.dart'; | |
import 'package:flutter_todos/blocs/blocs.dart'; | |
import 'package:flutter_todos/widgets/widgets.dart'; | |
import 'package:flutter_todos/screens/screens.dart'; | |
import 'package:flutter_todos/flutter_todos_keys.dart'; | |
class FilteredTodos extends StatelessWidget { | |
FilteredTodos({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
final todosBloc = BlocProvider.of<TodosBloc>(context); | |
final filteredTodosBloc = BlocProvider.of<FilteredTodosBloc>(context); | |
final localizations = ArchSampleLocalizations.of(context); | |
return BlocBuilder( | |
bloc: filteredTodosBloc, | |
builder: ( | |
BuildContext context, | |
FilteredTodosState state, | |
) { | |
if (state is FilteredTodosLoading) { | |
return LoadingIndicator(key: ArchSampleKeys.todosLoading); | |
} else if (state is FilteredTodosLoaded) { | |
final todos = state.filteredTodos; | |
return ListView.builder( | |
key: ArchSampleKeys.todoList, | |
itemCount: todos.length, | |
itemBuilder: (BuildContext context, int index) { | |
final todo = todos[index]; | |
return TodoItem( | |
todo: todo, | |
onDismissed: (direction) { | |
todosBloc.dispatch(DeleteTodo(todo)); | |
Scaffold.of(context).showSnackBar(DeleteTodoSnackBar( | |
key: ArchSampleKeys.snackbar, | |
todo: todo, | |
onUndo: () => todosBloc.dispatch(AddTodo(todo)), | |
localizations: localizations, | |
)); | |
}, | |
onTap: () async { | |
final removedTodo = await Navigator.of(context).push( | |
MaterialPageRoute(builder: (_) { | |
return DetailsScreen(id: todo.id); | |
}), | |
); | |
if (removedTodo != null) { | |
Scaffold.of(context).showSnackBar(DeleteTodoSnackBar( | |
key: ArchSampleKeys.snackbar, | |
todo: todo, | |
onUndo: () => todosBloc.dispatch(AddTodo(todo)), | |
localizations: localizations, | |
)); | |
} | |
}, | |
onCheckboxChanged: (_) { | |
todosBloc.dispatch( | |
UpdateTodo(todo.copyWith(complete: !todo.complete)), | |
); | |
}, | |
); | |
}, | |
); | |
} else { | |
return Container(key: FlutterTodosKeys.filteredTodosEmptyContainer); | |
} | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment