Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:todos_app_core/todos_app_core.dart';
  4. import 'package:flutter_todos/models/models.dart';
  5.  
  6. typedef OnSaveCallback = Function(String task, String note);
  7.  
  8. class AddEditScreen extends StatefulWidget {
  9. final bool isEditing;
  10. final OnSaveCallback onSave;
  11. final Todo todo;
  12.  
  13. AddEditScreen({
  14. Key key,
  15. @required this.onSave,
  16. @required this.isEditing,
  17. this.todo,
  18. }) : super(key: key ?? ArchSampleKeys.addTodoScreen);
  19.  
  20. @override
  21. _AddEditScreenState createState() => _AddEditScreenState();
  22. }
  23.  
  24. class _AddEditScreenState extends State<AddEditScreen> {
  25. static final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  26.  
  27. String _task;
  28. String _note;
  29.  
  30. bool get isEditing => widget.isEditing;
  31.  
  32. @override
  33. Widget build(BuildContext context) {
  34. final localizations = ArchSampleLocalizations.of(context);
  35. final textTheme = Theme.of(context).textTheme;
  36.  
  37. return Scaffold(
  38. appBar: AppBar(
  39. title: Text(
  40. isEditing ? localizations.editTodo : localizations.addTodo,
  41. ),
  42. ),
  43. body: Padding(
  44. padding: EdgeInsets.all(16.0),
  45. child: Form(
  46. key: _formKey,
  47. child: ListView(
  48. children: [
  49. TextFormField(
  50. initialValue: isEditing ? widget.todo.task : '',
  51. key: ArchSampleKeys.taskField,
  52. autofocus: !isEditing,
  53. style: textTheme.headline,
  54. decoration: InputDecoration(
  55. hintText: localizations.newTodoHint,
  56. ),
  57. validator: (val) {
  58. return val.trim().isEmpty
  59. ? localizations.emptyTodoError
  60. : null;
  61. },
  62. onSaved: (value) => _task = value,
  63. ),
  64. TextFormField(
  65. initialValue: isEditing ? widget.todo.note : '',
  66. key: ArchSampleKeys.noteField,
  67. maxLines: 10,
  68. style: textTheme.subhead,
  69. decoration: InputDecoration(
  70. hintText: localizations.notesHint,
  71. ),
  72. onSaved: (value) => _note = value,
  73. )
  74. ],
  75. ),
  76. ),
  77. ),
  78. floatingActionButton: FloatingActionButton(
  79. key:
  80. isEditing ? ArchSampleKeys.saveTodoFab : ArchSampleKeys.saveNewTodo,
  81. tooltip: isEditing ? localizations.saveChanges : localizations.addTodo,
  82. child: Icon(isEditing ? Icons.check : Icons.add),
  83. onPressed: () {
  84. if (_formKey.currentState.validate()) {
  85. _formKey.currentState.save();
  86. widget.onSave(_task, _note);
  87. Navigator.pop(context);
  88. }
  89. },
  90. ),
  91. );
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement