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 titleWidget;

  Widget? textWidget;
  if (config.titleConfig.text != null) {
    textWidget = Text(
      config.titleConfig.text!,
      style: config.titleConfig.textStyle,
    );
  } else if (title != null) {
    textWidget = Text(
      title ?? '',
      style: TextStyle(
        color: RCKThemeProvider().themeColor.textPrimary,
        fontSize: appbarFontSize,
        fontWeight: appbarFontWeight,
      ),
    );
  }

  titleWidget = GestureDetector(
    onTap: onTitleTap,
    child: Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: config.titleConfig.alignment,
      children: [
        if (config.titleConfig.prefixIcon != null)
          config.titleConfig.prefixIcon!,
        if (config.titleConfig.prefixIcon != null && textWidget != null)
          SizedBox(width: config.titleConfig.spacing),
        if (textWidget != null) textWidget,
        if (textWidget != null && config.titleConfig.suffixIcon != null)
          SizedBox(width: config.titleConfig.spacing),
        if (config.titleConfig.suffixIcon != null)
          config.titleConfig.suffixIcon!,
      ],
    ),
  );

  // 构建操作按钮
  List<Widget> actions = [];

  // 使用配置的操作按钮
  for (var i = 0; i < config.actionsConfig.items.length; i++) {
    var item = config.actionsConfig.items[i];
    actions.add(
      Padding(
        padding: item.padding,
        child: InkWell(
          onTap: onActionPressed != null && i < onActionPressed!.length
              ? onActionPressed![i]
              : null,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              item.icon,
              if (item.text != null)
                Padding(
                  padding: EdgeInsets.only(left: item.spacing),
                  child: Text(item.text!, style: item.textStyle),
                ),
            ],
          ),
        ),
      ),
    );

    // 添加间距(最后一个不需要)
    if (item != config.actionsConfig.items.last) {
      actions.add(SizedBox(width: config.actionsConfig.spacing));
    }
  }

  // 如果操作按钮为空,则添加一个占位符,与标题栏左边保持对称
  if (actions.isEmpty) {
    actions.add(const SizedBox(width: 100));
  }

  // 构建AppBar
  return AppBar(
    backgroundColor: config.backgroundConfig.color ??
        (RCKThemeProvider().currentTheme == RCIMIWAppTheme.dark
            ? RCKThemeProvider().themeColor.bgRegular
            : RCKThemeProvider().themeColor.bgAuxiliary1), // 使用与原AppBar相同的背景色
    leadingWidth: 100, // 设置固定宽度,与原AppBar保持一致
    flexibleSpace: config.backgroundConfig.image != null ||
            config.backgroundConfig.gradient != null ||
            config.backgroundConfig.border != null ||
            config.backgroundConfig.borderRadius != null ||
            config.backgroundConfig.boxShadow != null
        ? Container(
            decoration: BoxDecoration(
              image: config.backgroundConfig.image,
              gradient: config.backgroundConfig.gradient,
              border: config.backgroundConfig.border,
              borderRadius: config.backgroundConfig.borderRadius,
              boxShadow: config.backgroundConfig.boxShadow,
            ),
          )
        : null,
    automaticallyImplyLeading: false,
    leading: leadingBuilder?.call(context, config.leadingConfig) ??
        LeadingWidget(
          config: config.leadingConfig,
          onPressed: onLeadingPressed,
        ),
    title: titleWidget,
    centerTitle: config.centerTitle,
    actions: actions,
    bottom: PreferredSize(
      preferredSize: const Size.fromHeight(1),
      child: Container(
        height: 1,
        color: RCKThemeProvider().currentTheme == RCIMIWAppTheme.dark
            ? const Color(0xFF1D1D1D)
            : RCKThemeProvider().themeColor.outline,
      ),
    ),
  );
}