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) {
  CyberDataRow? textBoundRow;
  String? textBoundField;
  CyberDataRow? visibilityBoundRow;
  String? visibilityBoundField;

  // Parse text binding
  if (text is CyberBindingExpression) {
    final expr = text as CyberBindingExpression;
    textBoundRow = expr.row;
    textBoundField = expr.fieldName;
  }

  // Parse visibility binding
  if (isVisible is CyberBindingExpression) {
    final expr = isVisible as CyberBindingExpression;
    visibilityBoundRow = expr.row;
    visibilityBoundField = expr.fieldName;
  }

  Widget buildLabel() {
    // ✅ Check visibility
    bool visible = true;
    if (visibilityBoundRow != null && visibilityBoundField != null) {
      visible = _parseBool(visibilityBoundRow[visibilityBoundField]);
    } else if (isVisible != null) {
      visible = _parseBool(isVisible);
    }

    // ✅ Nếu không visible thì return empty widget
    if (!visible) {
      return const SizedBox.shrink();
    }

    // ✅ Build content (text hoặc icon)
    dynamic value;
    if (textBoundRow != null && textBoundField != null) {
      value = textBoundRow[textBoundField];
    } else {
      value = text;
    }

    Widget contentWidget;

    if (isIcon) {
      // ✅ Icon mode - Parse text as icon code point
      contentWidget = _buildIconWidget(value);
    } else {
      // ✅ Text mode - Display as text
      contentWidget = _buildTextWidget(value);
    }

    // ✅ Nếu không có event thì return content trực tiếp
    if (!_hasEvents) {
      return contentWidget;
    }

    // ✅ Wrap với GestureDetector nếu có event
    final shouldShowRipple = showRipple ?? true;

    if (shouldShowRipple) {
      // Sử dụng InkWell để có ripple effect
      return InkWell(
        onTap: () => onLeaver?.call(""),
        splashColor:
            rippleColor?.withValues(alpha: 0.3) ??
            Theme.of(context).primaryColor.withValues(alpha: 0.2),
        highlightColor:
            rippleColor?.withValues(alpha: 0.1) ??
            Theme.of(context).primaryColor.withValues(alpha: 0.1),
        borderRadius: rippleBorderRadius ?? BorderRadius.circular(4),
        child: Padding(
          padding:
              tapPadding ??
              const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
          child: contentWidget,
        ),
      );
    } else {
      // Không có ripple, chỉ dùng GestureDetector
      return GestureDetector(
        onTap: () => onLeaver?.call(""),
        child: Padding(
          padding:
              tapPadding ??
              const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
          child: contentWidget,
        ),
      );
    }
  }

  // ✅ Nếu có bất kỳ binding nào (text hoặc visibility) thì listen
  if (textBoundRow != null || visibilityBoundRow != null) {
    // Nếu cả 2 cùng bind vào 1 row
    if (textBoundRow == visibilityBoundRow) {
      return ListenableBuilder(
        listenable: textBoundRow!,
        builder: (context, child) => buildLabel(),
      );
    }

    // Nếu bind vào 2 row khác nhau
    if (textBoundRow != null && visibilityBoundRow != null) {
      return ListenableBuilder(
        listenable: Listenable.merge([textBoundRow, visibilityBoundRow]),
        builder: (context, child) => buildLabel(),
      );
    }

    // Chỉ có 1 trong 2
    final row = textBoundRow ?? visibilityBoundRow;
    return ListenableBuilder(
      listenable: row!,
      builder: (context, child) => buildLabel(),
    );
  }

  return buildLabel();
}