layout method

  1. @override
void layout(
  1. LayoutExpansion parentLayoutExpansion
)

Implementation

@override
void layout(LayoutExpansion parentLayoutExpansion) {
  // Save a few repeated values, calculated the width given to LabelContainer,
  //   and create the LabelContainer.
  double indicatorSquareSide = _options.legendOptions.legendColorIndicatorWidth;
  double indicatorToLabelPad = _options.legendOptions.legendItemIndicatorToLabelPad;
  double betweenLegendItemsPadding = _options.legendOptions.betweenLegendItemsPadding;
  double labelMaxWidth =
      parentLayoutExpansion.width - (indicatorSquareSide + indicatorToLabelPad + betweenLegendItemsPadding);
  if (enableSkipOnDistressedSize && labelMaxWidth <= 0.0) {
    isDistressed = true;
    layoutSize = ui.Size.zero;
    return;
  }
  _labelContainer = LabelContainer(
    label: _label,
    labelMaxWidth: labelMaxWidth,
    labelTiltMatrix: vector_math.Matrix2.identity(), // No tilted labels in LegendItemContainer
    labelStyle: _labelStyle,
  );
  _labelContainer.layout(LayoutExpansion.unused());

  // Layout legend item elements (indicator, pad, label) flowing from left:

  // 1. layout the _labelContainer - this also provides height
  ui.Size labelContainerSize = _labelContainer.layoutSize;
  // 2. Y Center the indicator and label on same horizontal Y level
  //   ind stands for "indicator" - the series color indicator square
  double indAndLabelCenterY = math.max(
        labelContainerSize.height,
        indicatorSquareSide,
      ) /
      2.0;
  double indOffsetY = indAndLabelCenterY - indicatorSquareSide / 2.0;
  double labelOffsetY = indAndLabelCenterY - labelContainerSize.height / 2.0;

  // 3. Calc the X offset to both indicator and label, so indicator is left,
  //    then padding, then the label
  double indOffsetX = 0.0; // indicator starts on the left
  double labelOffsetX = indOffsetX + indicatorSquareSide + indicatorToLabelPad;

  // 4. Create the indicator square, and place it within this container
  //   (this is applyParentOffset for the indicator, if it was an object)
  _indicatorRect = ui.Rect.fromLTWH(
    indOffsetX,
    indOffsetY,
    indicatorSquareSide,
    indicatorSquareSide,
  );

  // 5. Place the label within this container
  _labelContainer.applyParentOffset(ui.Offset(
    labelOffsetX,
    labelOffsetY,
  ));

  // 6. And store the layout size on member
  layoutSize = ui.Size(
    _indicatorRect.width + indicatorToLabelPad + _labelContainer.layoutSize.width + betweenLegendItemsPadding,
    math.max(
      labelContainerSize.height,
      _indicatorRect.height,
    ),
  );

  // Make sure we fit all available width
  assert(parentLayoutExpansion.width + 1.0 >= layoutSize.width); // todo-2 within epsilon
}