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 theme = MiuixTheme.of(context);
  final colors =
      dropdownColors ?? MiuixDropdownDefaults.dropdownColors(context);
  final disabledColor = theme.colors.disabledOnSecondaryVariant;

  final additionalTopPadding = (!dialogMode && isFirst)
      ? MiuixDropdownDefaults.firstLastVerticalPadding
      : MiuixDropdownDefaults.middleVerticalPadding;
  final additionalBottomPadding = (!dialogMode && isLast)
      ? MiuixDropdownDefaults.firstLastVerticalPadding
      : MiuixDropdownDefaults.middleVerticalPadding;

  final backgroundColor = isSelected
      ? colors.selectedContainerColor
      : colors.containerColor;

  final checkColor = !isSelected
      ? const Color(0x00000000)
      : (!enabled ? disabledColor : colors.selectedIndicatorColor);

  final titleColor = !enabled
      ? disabledColor
      : (isSelected ? colors.selectedContentColor : colors.contentColor);

  final summaryColor = !enabled
      ? disabledColor
      : (isSelected ? colors.selectedSummaryColor : colors.summaryColor);

  // 子菜单箭头使用更柔和的 summaryColor,避免触发行与选中/叶子项抢视觉。
  final chevronColor = !enabled
      ? disabledColor
      : (isSelected ? colors.selectedContentColor : colors.summaryColor);

  // 内部图标+文本行:弹窗模式限宽 216,对话框模式不限。
  Widget innerRow = Row(
    mainAxisSize: MainAxisSize.min,
    crossAxisAlignment: CrossAxisAlignment.center,
    mainAxisAlignment: MainAxisAlignment.start,
    children: <Widget>[
      if (item.icon != null)
        ConstrainedBox(
          constraints: const BoxConstraints(
            minWidth: MiuixDropdownDefaults.iconMinSize,
            minHeight: MiuixDropdownDefaults.iconMinSize,
          ),
          child: Padding(
            padding: const EdgeInsetsDirectional.only(
              end: MiuixDropdownDefaults.iconEndPadding,
            ),
            child: item.icon,
          ),
        ),
      Flexible(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            MiuixText(
              item.text,
              fontSize: theme.textStyles.body1.fontSize,
              fontWeight: FontWeight.w500,
              color: titleColor,
            ),
            if (item.summary != null)
              MiuixText(
                item.summary!,
                fontSize: theme.textStyles.body2.fontSize,
                color: summaryColor,
              ),
          ],
        ),
      ),
    ],
  );
  if (!dialogMode) {
    innerRow = ConstrainedBox(
      constraints: const BoxConstraints(
        maxWidth: MiuixDropdownDefaults.maxItemTextWidth,
      ),
      child: innerRow,
    );
  }

  // 尾部图标:子菜单触发器显示箭头(10x16),否则显示勾号(20x20)。
  final Widget trailing = hasSubmenu
      ? Padding(
          padding: const EdgeInsetsDirectional.only(
            start: MiuixDropdownDefaults.checkIconStartPadding,
          ),
          child: SizedBox(
            width: MiuixDropdownDefaults.chevronSize.width,
            height: MiuixDropdownDefaults.chevronSize.height,
            child: CustomPaint(
              painter: _MiuixVectorIconPainter(
                paths: _arrowRightPaths,
                viewport: _arrowViewport,
                color: chevronColor,
              ),
            ),
          ),
        )
      : Padding(
          padding: const EdgeInsetsDirectional.only(
            start: MiuixDropdownDefaults.checkIconStartPadding,
          ),
          child: SizedBox(
            width: MiuixDropdownDefaults.checkIconSize,
            height: MiuixDropdownDefaults.checkIconSize,
            child: CustomPaint(
              painter: _MiuixVectorIconPainter(
                paths: _checkPaths,
                viewport: _checkViewport,
                color: checkColor,
              ),
            ),
          ),
        );

  // 外层行:弹窗模式 wrap-content(MainAxisSize.min),对话框模式 fillMaxWidth。
  // 两种模式都用 SpaceBetween:弹窗模式下若父级 min 宽度约束存在(列表列)则
  // 箭头被推到尾部;无约束时排布退化为紧贴。
  final Widget row = Row(
    crossAxisAlignment: CrossAxisAlignment.center,
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    mainAxisSize: dialogMode ? MainAxisSize.max : MainAxisSize.min,
    children: <Widget>[innerRow, trailing],
  );

  final padding = EdgeInsets.only(
    left: dialogMode
        ? MiuixDropdownDefaults.dialogHorizontalPadding
        : MiuixDropdownDefaults.insideHorizontalPadding,
    right: dialogMode
        ? MiuixDropdownDefaults.dialogHorizontalPadding
        : MiuixDropdownDefaults.insideHorizontalPadding,
    top: additionalTopPadding,
    bottom: additionalBottomPadding,
  );

  Widget sized = Padding(padding: padding, child: row);
  if (dialogMode) {
    sized = ConstrainedBox(
      constraints: const BoxConstraints(
        minWidth: MiuixDropdownDefaults.minWidth,
        minHeight: MiuixDropdownDefaults.minHeight,
      ),
      child: sized,
    );
  }

  // 背景为纯矩形填充(对应 Compose drawBehind { drawRect }),无圆角、无按压涟漪。
  final Widget content = ColoredBox(color: backgroundColor, child: sized);

  return Semantics(
    selected: isSelected,
    button: hasSubmenu,
    inMutuallyExclusiveGroup: !hasSubmenu,
    enabled: enabled,
    child: GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: enabled ? () => onSelectedIndexChange(index) : null,
      child: content,
    ),
  );
}