build method

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

Build the internal widget.

The context used during the build is passed as is.

Also, WidgetRef is passed to ref to update the state.

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

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

また、refに状態を更新するためのPageRefが渡されます。

Implementation

@override
Widget build(BuildContext context, PageRef ref) {
  // Describes the process of loading
  // and defining variables required for the page.
  final adapter = MasamuneModuleSimpleblogMasamuneAdapter.primary;
  final user = UserModel.document(adapter.auth.userId).watch(ref)..load();
  final controller = UserModel.form(
    user.value ?? const UserModel(name: ""),
  ).watch(ref);

  // Describes the structure of the page.
  return UniversalScaffold(
    appBar: UniversalAppBar(
      leadingWhenDisabledPop: CloseButton(
        onPressed: () {
          adapter.page.index().push(adapter.router);
        },
      ),
      title: Text(ml().$(ml().edit).of.$(ml().profile)),
      actions: [
        IconButton(
          onPressed: () async {
            if (!controller.validateAndSave()) {
              return;
            }
            try {
              await user.save(controller.value).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: [
            FormMedia(
              style: FormStyle(
                padding: 16.p,
                contentPadding: 0.0.px,
                width: 128,
                height: 128,
                borderRadius: 16.r,
              ),
              form: controller,
              initialValue: controller.value.image?.toFormMediaValue(),
              builder: (context, value) {
                return Image(
                  image: Asset.image(value.uri.toString()),
                  fit: BoxFit.cover,
                );
              },
              onTap: (ref) async {
                final uri = await adapter.option.onPickerSelected?.call();
                if (uri == null) {
                  return;
                }
                ref.update(uri, FormMediaType.image);
              },
              onSaved: (value) => controller.value.copyWith(
                image: value.toModelImageUri(),
              ),
            ),
            FormLabel(ml().name),
            FormTextField(
              form: controller,
              initialValue: controller.value.name,
              onSaved: (value) {
                return controller.value.copyWith(name: value);
              },
            ),
            const Divider(),
          ],
        ),
      ),
    ),
  );
}