build method

  1. @override
Widget build(
  1. BuildContext context,
  2. FormRef ref
)
override

Build the internal widget.

The context used during the build is passed as is.

It also inherits WidgetRef for updating the state in ref and is passed FormRef containing information for the form.

内部のウィジェットをビルドします。

ビルド中に使用されるcontextがそのまま渡されます。

また、refに状態を更新するためのWidgetRefを継承し、フォーム用の情報が含まれたFormRefが渡されます。

Implementation

@override
Widget build(BuildContext context, FormRef ref) {
  // Describes the process of loading
  // and defining variables required for the page.
  //
  // You can use [form.isAdding] or [form.isEditing] to determine if the form is currently adding new data or editing data.
  //
  // If editing is in progress, it is possible to get the ID of the item being edited with [form.editId].
  final note =
      ref.isAdding ? null : ref.model(NoteModel.document(ref.editId!))
        ?..load();
  final controller = ref.page.controller(NoteModel.form(
    note?.value ?? const NoteModel(title: "", text: ""),
  ));
  final adapter =
      MasamuneAdapterScope.of<SimpleBlogModuleMasamuneAdapter>(context);

  // Describes the structure of the page.
  return UniversalScaffold(
    breakpoint: Breakpoint.sm,
    appBar: UniversalAppBar(
      title: Text(ref.isAdding
          ? ml().$(ml().add).of.$(adapter?.words.article ?? ml().article)
          : ml().$(ml().edit).of.$(note?.value?.title)),
      actions: [
        if (ref.isEditing)
          IconButton(
            onPressed: () async {
              Modal.confirm(
                context,
                submitText: ml().yes,
                cancelText: ml().no,
                title: ml().confirmation,
                text: ml()
                    .youWillDelete
                    .$(adapter?.words.article ?? ml().article)
                    .onceDeletedItCannotBeUndone,
                onSubmit: () async {
                  try {
                    await note?.delete().showIndicator(context);
                    context.router.pop();
                  } catch (e) {
                    Modal.alert(
                      context,
                      submitText: ml().close,
                      title: ml().error,
                      text: ml().$(ml().delete).haveFailed,
                    );
                  }
                },
              );
            },
            icon: const Icon(Icons.delete),
          ),
        IconButton(
          onPressed: () async {
            if (!controller.validateAndSave()) {
              return;
            }
            try {
              final doc =
                  note ?? adapter!.ref.model(NoteModel.collection()).create();
              await doc.save(controller.value).showIndicator(context);
              // 追加時のみ送信
              if (ref.isAdding) {
                await adapter?.notification
                    ?.send(
                      controller.value.title,
                      controller.value.text,
                    )
                    .showIndicator(context);
              }
              context.router.pop();
            } catch (e) {
              Modal.alert(
                context,
                submitText: ml().close,
                title: ml().error,
                text: ml().$(ml().save).haveFailed,
              );
            }
          },
          icon: const Icon(Icons.save),
        ),
      ],
    ),
    body: SafeArea(
      child: UniversalContainer(
        child: Column(
          children: [
            const FormLabel("タイトル"),
            FormTextField(
              form: controller,
              initialValue: controller.value.title,
              onSaved: (value) {
                return controller.value.copyWith(title: value);
              },
            ),
            const FormLabel("日時"),
            FormDateTimeField(
              form: controller,
              initialValue: controller.value.time.value,
              onSaved: (value) {
                return controller.value.copyWith(
                  time: ModelTimestamp(value),
                );
              },
            ),
            const FormLabel("内容"),
            Expanded(
              child: FormTextField(
                form: controller,
                expands: true,
                keyboardType: TextInputType.multiline,
                style:
                    const FormStyle(textAlignVertical: TextAlignVertical.top),
                initialValue: controller.value.text,
                onSaved: (value) {
                  return controller.value.copyWith(text: value);
                },
              ),
            ),
            const Divider(),
            FormMultiMedia(
              form: controller,
              initialValue: controller.value.images
                  .map((e) =>
                      FormMediaValue(path: e, type: FormMediaType.image))
                  .toList(),
              builder: (context, value) {
                return AspectRatio(
                  aspectRatio: 1,
                  child: Image(
                    image: Asset.image(value.path),
                    fit: BoxFit.cover,
                  ),
                );
              },
              onTap: (onUpdate) async {
                await adapter!.picker
                    ?.pick(context, onUpdate)
                    .showIndicator(context);
              },
              onSaved: (value) => controller.value.copyWith(
                images: value.mapAndRemoveEmpty((e) => e.path),
              ),
            ),
          ],
        ),
      ),
    ),
  );
}