buildContent method

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

子类需要实现的方法:构建主体内容

Implementation

@override
Widget buildContent(BuildContext context) {
  // 一次性获取隐藏配置,避免重复遍历 widget tree
  // Get hidden config once to avoid repeated widget tree traversal
  final hiddenElements = SlotManager.getHiddenElements(
    context,
    SlotType.topBar,
  );

  // 获取屏幕方向,用于区分横竖屏布局
  final orientation = MediaQuery.of(context).orientation;
  final isLandscape = orientation == Orientation.landscape;

  // 横屏时:左侧按钮左间距34,右侧按钮之间间距34
  // 竖屏时:保持原有样式,水平间距20
  final horizontalMargin = isLandscape
      ? PlayerStyles.kTopBarLandscapeHorizontalMargin
      : PlayerStyles.kTopBarPortraitHorizontalMargin;
  final leftButtonLeftMargin =
      isLandscape ? PlayerStyles.kTopBarLandscapeBackButtonLeftMargin : 0.0;
  final rightButtonRightMargin =
      isLandscape ? PlayerStyles.kTopBarLandscapeRightButtonRightMargin : 0.0;
  const rightButtonsSpacing = PlayerStyles.kIconHorizontalMargin;

  // 根据配置构建可见元素,避免过早创建 Widget
  // Build visible elements based on config, avoiding premature Widget creation
  final slotElements = <Widget>[];

  // 返回按钮
  if (hiddenElements.isElementVisible(TopBarElements.back)) {
    slotElements.add(
      GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: onBackPressed,
        child: Container(
          margin: EdgeInsets.only(
            left: leftButtonLeftMargin,
            right: isLandscape ? 0.0 : 6.0,
          ),
          child: Image.asset(
            PlayerAssetsScope.iconPath(context, PlayerIcons.back),
            fit: BoxFit.cover,
          ),
        ),
      ),
    );
  }

  // 标题
  if (hiddenElements.isElementVisible(TopBarElements.title)) {
    slotElements.add(
      Expanded(
        child: title == null || title!.isEmpty
            ? const SizedBox.shrink()
            : Container(
                margin: const EdgeInsets.symmetric(vertical: 12),
                child: Text(
                  title!,
                  style: TextStyle(
                    color: PlayerStyles.colorOnPrimary,
                    fontSize: PlayerStyles.fontSizeTitle,
                    fontWeight: PlayerStyles.fontWeightTitle,
                    shadows: [
                      Shadow(
                        blurRadius: 2,
                        color: Colors.black.withOpacity(0.4),
                      ),
                    ],
                  ),
                ),
              ),
      ),
    );
  } else {
    // 隐藏时仍需保持弹性布局
    // Must maintain flex layout when hidden
    slotElements.add(const Expanded(child: SizedBox.shrink()));
  }

  // 截图按钮,可选
  if (onSnapshotPressed != null &&
      hiddenElements.isElementVisible(TopBarElements.snapshot)) {
    slotElements.add(
      CustomIconButton(
        iconName: PlayerIcons.snapshot,
        margin: const EdgeInsets.symmetric(
          horizontal: rightButtonsSpacing,
        ),
        onPressed: () => onSnapshotPressed?.call(),
      ),
    );
  }

  // 下载按钮,可选
  if (downloadVisible &&
      hiddenElements.isElementVisible(TopBarElements.download)) {
    slotElements.add(
      CustomIconButton(
        iconName: PlayerIcons.download,
        margin: const EdgeInsets.symmetric(
          horizontal: rightButtonsSpacing,
        ),
        onPressed: () => onDownloadPressed?.call(),
      ),
    );
  }

  // PIP 按钮,可选
  if (onPIPPressed != null &&
      hiddenElements.isElementVisible(TopBarElements.pip)) {
    // TODO: PIP 按钮
  }

  // 设置按钮,可选(最右侧按钮,使用右边距)
  if (onSettingsPressed != null &&
      hiddenElements.isElementVisible(TopBarElements.settings)) {
    slotElements.add(
      CustomIconButton(
        iconName: PlayerIcons.settings,
        margin: EdgeInsets.only(
          left: rightButtonsSpacing,
          right: rightButtonRightMargin,
        ),
        onPressed: () => onSettingsPressed?.call(),
      ),
    );
  }

  return SafeArea(
    child: Container(
      height: PlayerStyles.kTopBarHeight,
      margin: EdgeInsets.symmetric(horizontal: horizontalMargin),
      alignment: Alignment.center,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: slotElements,
      ),
    ),
  );
}