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) {
  final String infoLabel = JunnyTranslationConfig.widgetsProxy.info(context);
  final String editLabel = JunnyTranslationConfig.widgetsProxy.edit(context);
  final String startProcessLabel =
      JunnyTranslationConfig.workflowProxy.startProcess(context);
  final String monitorProcessLabel =
      JunnyTranslationConfig.widgetsProxy.monitorProcess(context);
  final String monitorProcessTipsTwo =
      JunnyTranslationConfig.widgetsProxy.monitorProcessTips2(context);
  final String monitorProcessTipsThree =
      JunnyTranslationConfig.workflowProxy.monitorProcessTips3(context);

  final Map<String, VoidCallback?> subActions = <String, VoidCallback?>{
    // 查看详情
    if (onInfo != null) infoLabel: onInfo,
  };

  // 流程可操作状态(未审批或者不是流程类型的列表)
  final bool isFlowEditable = flowEditableGetter();

  // 添加编辑
  if (isActionEnabled &&
      BusinessUtils.isActionAvailable(actions, key: 'update') &&
      isFlowEditable &&
      onEdit != null) {
    subActions[editLabel] = onEdit;
  }

  // 添加提交流程
  if (isActionEnabled &&
      BusinessUtils.isActionAvailable(actions, key: 'startProcess') &&
      isFlowEditable &&
      onStartProcess != null) {
    subActions[startProcessLabel] = onStartProcess;
  }

  // 添加流程监控
  if (BusinessUtils.isActionAvailable(actions, key: 'monitorProcess')) {
    subActions[monitorProcessLabel] = () {
      if (taskId != null) {
        BusinessUtils.checkProcessLogs(taskId);
      } else {
        if (WorkflowStatusUtils.isEditableFlowStatus(flowStatus)) {
          // 该流程未提交
          showAppToast(monitorProcessTipsTwo);
        } else {
          // 该数据没有流程
          showAppToast(monitorProcessTipsThree);
        }
      }
    };
  }

  // 添加删除
  if (isActionEnabled &&
      BusinessUtils.isActionAvailable(actions, key: 'delete') &&
      // 没有流程状态的列表或者流程启动状态
      isFlowEditable &&
      onDelete != null) {
    subActions[MaterialLocalizations.of(context).deleteButtonTooltip] =
        onDelete;
  }

  return ClipRRect(
    borderRadius: BorderRadius.circular(4),
    child: Container(
      decoration: BoxDecoration(
        color: context.theme.cardColor,
        borderRadius: BorderRadius.circular(4),
      ),
      padding: const EdgeInsets.all(15),
      child: Column(
        children: <Widget>[
          // 上半部分信息
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              // 标题
              Expanded(
                child: Text(
                  title ?? '',
                  style: context.textTheme.bodyMedium
                      ?.copyWith(fontWeight: FontWeight.w600),
                ),
              ),
              // 状态
              StatusColoredBox<String?>(
                status: flowStatus,
                statusDict: statusDict,
              ),
            ],
          ),
          const SizedBox(height: 5),
          // 子标题
          () {
            Widget result = const SizedBox.shrink();
            if (subtitle is String) {
              assert(
                subtitleLabel != null,
                'subtitleLabel should not be null when subtitle provided',
              );
              result = Row(
                children: <Widget>[
                  Expanded(
                    flex: 2,
                    child: Text(
                      subtitleLabel ?? '',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: context.textTheme.bodyMedium
                          ?.copyWith(fontSize: 12),
                    ),
                  ),
                  const SizedBox(width: 5),
                  Expanded(
                    flex: 5,
                    child: Text(
                      subtitle ?? '',
                      style: context.textTheme.bodySmall,
                    ),
                  ),
                  if (info.length > 2) const SizedBox(width: 32),
                ],
              );
            } else if (subtitle is Widget) {
              if (subtitleLabel != null) {
                result = Row(
                  children: <Widget>[
                    Expanded(
                      flex: 2,
                      child: Text(
                        subtitleLabel ?? '',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: context.textTheme.bodyMedium
                            ?.copyWith(fontSize: 12),
                      ),
                    ),
                    const SizedBox(width: 5),
                    Expanded(
                      flex: 5,
                      child: subtitle,
                    ),
                    if (info.length > 2) const SizedBox(width: 32),
                  ],
                );
              } else {
                result = subtitle;
              }
            }
            return result;
          }(),
          // 有标题或者子标题或者状态且有信息
          if ((title != null || subtitle != null || flowStatus != null) &&
              info.isNotEmpty)
            const Divider(),
          if (info.isNotEmpty)
            // 信息展示栏
            ExpandableInfoTile(data: info),
          if (subActions.isNotEmpty ||
              (extraActions?.isNotEmpty ?? false)) ...<Widget>[
            const Divider(),
            // 按钮操作栏
            Align(
              alignment: Alignment.centerRight,
              child: FoldableActions(
                itemSpace: 10,
                menuActions: <Widget>[
                  ...subActions.keys.take(2).map(
                    (String e) {
                      // 查看详情按钮
                      final bool isInfoButton = e == infoLabel;
                      if (isInfoButton) {
                        return PrimaryButton(
                          onTap: subActions[e],
                          title: e,
                        );
                      } else {
                        // 删除按钮
                        final bool isDeleteButton = e ==
                            MaterialLocalizations.of(context)
                                .deleteButtonTooltip;
                        return ThemeButton(
                          onTap: subActions[e],
                          border: Border.all(
                            color: isDeleteButton
                                ? Colors.red
                                : context.themeColor,
                            width: .5,
                          ),
                          textColor: isDeleteButton ? Colors.red : null,
                          title: e,
                        );
                      }
                    },
                  ),
                  ...?extraActions,
                  if (subActions.length > 2)
                    ...subActions.keys
                        .whereIndexed(
                          (int index, String element) => index > 1,
                        )
                        .map(
                          (String e) => ThemeButton(
                            onTap: () => subActions[e]?.call(),
                            textColor: e ==
                                    MaterialLocalizations.of(context)
                                        .deleteButtonTooltip
                                ? Colors.red
                                : null,
                            title: e,
                          ),
                        ),
                ]
                    .map(
                      (Widget e) => BusinessUtils.transformMenuItemParam(
                        context,
                        e,
                      ),
                    )
                    .toList(),
              ),
            ),
          ],
        ],
      ),
    ),
  );
}