add_edit_screen.dart 3.13 KB
Newer Older
1 2 3 4
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Brian Egan's avatar
Brian Egan committed
5
import 'package:flutter_architecture_samples/flutter_architecture_samples.dart';
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import 'package:vanilla/models.dart';
import 'package:vanilla/widgets/typedefs.dart';

class AddEditScreen extends StatelessWidget {
  static final GlobalKey<FormState> formKey = new GlobalKey<FormState>();
  static final GlobalKey<FormFieldState<String>> taskKey =
      new GlobalKey<FormFieldState<String>>();
  static final GlobalKey<FormFieldState<String>> noteKey =
      new GlobalKey<FormFieldState<String>>();

  final Todo todo;
  final TodoAdder addTodo;
  final TodoUpdater updateTodo;

  AddEditScreen({
    Key key,
    @required this.addTodo,
    @required this.updateTodo,
    this.todo,
  })
Brian Egan's avatar
Brian Egan committed
26
      : super(key: key ?? ArchSampleKeys.addTodoScreen);
27 28 29 30 31 32

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(isEditing
Brian Egan's avatar
Brian Egan committed
33 34
            ? ArchSampleLocalizations.of(context).editTodo
            : ArchSampleLocalizations.of(context).addTodo),
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      ),
      body: new Padding(
        padding: new EdgeInsets.all(16.0),
        child: new Form(
          key: formKey,
          autovalidate: false,
          onWillPop: () {
            return new Future(() => true);
          },
          child: new ListView(
            children: [
              new TextFormField(
                initialValue: todo != null ? todo.task : '',
                key: taskKey,
                autofocus: isEditing ? false : true,
                style: Theme.of(context).textTheme.headline,
                decoration: new InputDecoration(
52
                    hintText: ArchSampleLocalizations.of(context).newTodoHint),
53
                validator: (val) => val.trim().isEmpty
Brian Egan's avatar
Brian Egan committed
54
                    ? ArchSampleLocalizations.of(context).emptyTodoError
55 56 57 58 59 60 61 62
                    : null,
              ),
              new TextFormField(
                initialValue: todo != null ? todo.note : '',
                key: noteKey,
                maxLines: 10,
                style: Theme.of(context).textTheme.subhead,
                decoration: new InputDecoration(
Brian Egan's avatar
Brian Egan committed
63
                  hintText: ArchSampleLocalizations.of(context).notesHint,
64 65 66 67 68 69 70 71
                ),
              )
            ],
          ),
        ),
      ),
      floatingActionButton: new FloatingActionButton(
          tooltip: isEditing
Brian Egan's avatar
Brian Egan committed
72 73
              ? ArchSampleLocalizations.of(context).saveChanges
              : ArchSampleLocalizations.of(context).addTodo,
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
          child: new Icon(isEditing ? Icons.check : Icons.add),
          onPressed: () {
            final form = formKey.currentState;
            if (form.validate()) {
              final task = taskKey.currentState.value;
              final note = noteKey.currentState.value;

              if (isEditing) {
                updateTodo(todo, task: task, note: note);
              } else {
                addTodo(new Todo(
                  task,
                  note: note,
                ));
              }

              Navigator.pop(context);
            }
          }),
    );
  }

  bool get isEditing => todo != null;
}