Created
March 15, 2019 00:55
-
-
Save felangel/d7d3e5b35cf6cc316352af194992c2fe to your computer and use it in GitHub Desktop.
[flutter_todos] details screen
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/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'package:todos_app_core/todos_app_core.dart'; | |
import 'package:flutter_todos/blocs/todos/todos.dart'; | |
import 'package:flutter_todos/screens/screens.dart'; | |
import 'package:flutter_todos/flutter_todos_keys.dart'; | |
class DetailsScreen extends StatelessWidget { | |
final String id; | |
DetailsScreen({Key key, @required this.id}) | |
: super(key: key ?? ArchSampleKeys.todoDetailsScreen); | |
@override | |
Widget build(BuildContext context) { | |
final todosBloc = BlocProvider.of<TodosBloc>(context); | |
return BlocBuilder( | |
bloc: todosBloc, | |
builder: (BuildContext context, TodosState state) { | |
final todo = (state as TodosLoaded) | |
.todos | |
.firstWhere((todo) => todo.id == id, orElse: () => null); | |
final localizations = ArchSampleLocalizations.of(context); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(localizations.todoDetails), | |
actions: [ | |
IconButton( | |
tooltip: localizations.deleteTodo, | |
key: ArchSampleKeys.deleteTodoButton, | |
icon: Icon(Icons.delete), | |
onPressed: () { | |
todosBloc.dispatch(DeleteTodo(todo)); | |
Navigator.pop(context, todo); | |
}, | |
) | |
], | |
), | |
body: todo == null | |
? Container(key: FlutterTodosKeys.emptyDetailsContainer) | |
: Padding( | |
padding: EdgeInsets.all(16.0), | |
child: ListView( | |
children: [ | |
Row( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Padding( | |
padding: EdgeInsets.only(right: 8.0), | |
child: Checkbox( | |
key: FlutterTodosKeys.detailsScreenCheckBox, | |
value: todo.complete, | |
onChanged: (_) { | |
todosBloc.dispatch( | |
UpdateTodo( | |
todo.copyWith(complete: !todo.complete), | |
), | |
); | |
}), | |
), | |
Expanded( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Hero( | |
tag: '${todo.id}__heroTag', | |
child: Container( | |
width: MediaQuery.of(context).size.width, | |
padding: EdgeInsets.only( | |
top: 8.0, | |
bottom: 16.0, | |
), | |
child: Text( | |
todo.task, | |
key: ArchSampleKeys.detailsTodoItemTask, | |
style: | |
Theme.of(context).textTheme.headline, | |
), | |
), | |
), | |
Text( | |
todo.note, | |
key: ArchSampleKeys.detailsTodoItemNote, | |
style: Theme.of(context).textTheme.subhead, | |
), | |
], | |
), | |
), | |
], | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
key: ArchSampleKeys.editTodoFab, | |
tooltip: localizations.editTodo, | |
child: Icon(Icons.edit), | |
onPressed: todo == null | |
? null | |
: () { | |
Navigator.of(context).push( | |
MaterialPageRoute( | |
builder: (context) { | |
return AddEditScreen( | |
key: ArchSampleKeys.editTodoScreen, | |
onSave: (task, note) { | |
todosBloc.dispatch( | |
UpdateTodo( | |
todo.copyWith(task: task, note: note), | |
), | |
); | |
}, | |
isEditing: true, | |
todo: todo, | |
); | |
}, | |
), | |
); | |
}, | |
), | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment