build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  Widget inputWidget = Container(
    decoration: BoxDecoration(
      color: PlexTheme.getActiveTheme(context).splashColor,
      border: Border.all(color: errorController?.data != null ? PlexTheme.inputErrorColor : Theme.of(context).colorScheme.outline, width: 1),
      borderRadius: BorderRadius.circular(properties.cornerRadius),
    ),
    child: Padding(
      padding: const EdgeInsets.symmetric(horizontal: PlexDim.small, vertical: PlexDim.smallest),
      child: Row(
        children: [
          Expanded(
            child: PlexWidget<DateTime?>(
              controller: getController(),
              createWidget: (context, data) {
                return TextField(
                  readOnly: true,
                  showCursor: false,
                  onTap: () {
                    if (!properties.enabled) return;
                    if (properties.enabled == false) return;
                    if (type == PlexFormFieldDateType.typeDate) {
                      showDatePicker(
                        context: context,
                        initialDate: getController().data ?? DateTime.now(),
                        firstDate: minDatetime ?? DateTime(1970, 1, 1),
                        lastDate: maxDatetime ?? DateTime(5000, 12, 31),
                        useRootNavigator: true,
                      ).then((value) {
                        if (value != null) {
                          getController().setValue(value as DateTime?);
                          onSelect?.call(value);
                        }
                      });
                    } else if (type == PlexFormFieldDateType.typeTime) {
                      showTimePicker(
                        context: context,
                        initialTime: TimeOfDay.fromDateTime(getController().data ?? DateTime.now()),
                        useRootNavigator: true,
                      ).then((value) {
                        if (value != null) {
                          DateTime dateTime = getController().data ?? DateTime.now();
                          dateTime = DateTime(
                            dateTime.year,
                            dateTime.month,
                            dateTime.day,
                            value.hour,
                            value.minute,
                          );
                          if (minDatetime != null && dateTime.isBefore(minDatetime!)) {
                            context.showMessageError("Invalid Time Selection");
                            return;
                          }
                          if (maxDatetime != null && dateTime.isAfter(maxDatetime!)) {
                            context.showMessageError("Invalid Time Selection");
                            return;
                          }
                          getController().setValue(dateTime as DateTime?);
                          onSelect?.call(value);
                        }
                      });
                    } else if (type == PlexFormFieldDateType.typeDateTime) {
                      showDatePicker(
                        context: context,
                        initialDate: getController().data ?? DateTime.now(),
                        firstDate: minDatetime ?? DateTime(1970, 1, 1),
                        lastDate: maxDatetime ?? DateTime(5000, 12, 31),
                        useRootNavigator: true,
                      ).then((selectedDate) {
                        if (selectedDate != null) {
                          showTimePicker(
                            context: context,
                            initialTime: TimeOfDay.fromDateTime(selectedDate),
                            useRootNavigator: true,
                            builder: (context, child) {
                              return MediaQuery(
                                data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
                                child: child!,
                              );
                            },
                          ).then((value) {
                            if (value != null) {
                              var dateTime = DateTime(
                                selectedDate.year,
                                selectedDate.month,
                                selectedDate.day,
                                value.hour,
                                value.minute,
                              );
                              if (minDatetime != null && dateTime.isBefore(minDatetime!)) {
                                context.showMessageError("Invalid Time Selection");
                                return;
                              }
                              if (maxDatetime != null && dateTime.isAfter(maxDatetime!)) {
                                context.showMessageError("Invalid Time Selection");
                                return;
                              }
                              getController().setValue(dateTime as DateTime?);
                              onSelect?.call(value);
                            }
                          });
                        }
                      });
                    }
                  },
                  enabled: properties.enabled,
                  controller: TextEditingController(
                    text: type == PlexFormFieldDateType.typeDate
                        ? (data as DateTime?)?.toDateString()
                        : type == PlexFormFieldDateType.typeTime
                            ? (data as DateTime?)?.toTimeString()
                            : type == PlexFormFieldDateType.typeDateTime
                                ? (data as DateTime?)?.toDateTimeString()
                                : "N/A",
                  ),
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    prefixIcon: const Icon(Icons.calendar_month_outlined, color: Colors.grey),
                    labelText: properties.title ?? "",
                    helperText: properties.helperText,
                    errorText: errorController?.data?.toString(),
                    filled: false,
                  ),
                );
              },
            ),
          ),
          if(cancellable) ...{
            IconButton(
              icon: const Icon(Icons.close),
              color: Colors.grey,
              onPressed: () {
                getController().setValue(null);
              },
            ),
            // const Icon(Icons.arrow_drop_down, color: Colors.grey),
          }
        ],
      ),
    ),
  );

  Widget finalWidget;
  if (errorController != null) {
    finalWidget = PlexWidget(
      controller: errorController!,
      createWidget: (context, data) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            inputWidget,
            if (errorController!.data != null) ...{
              Padding(
                padding: EdgeInsets.only(left: PlexDim.medium, right: PlexDim.medium, top: PlexDim.small),
                child: Text(errorController!.data!.toString(), textAlign: TextAlign.left, style: TextStyle(color: PlexTheme.inputErrorColor)),
              )
            },
          ],
        );
      },
    );
  } else {
    finalWidget = inputWidget;
  }

  if (properties.useMargin) {
    return Padding(
      padding: properties.margin,
      child: finalWidget,
    );
  }
  return finalWidget;
}