build method

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

👷‍♂️ Method responsible for laying-out and rendering an IconToo, unique from an Icon in that icon may be non-square and shadows parameter is accepted.

Implementation

@override
Widget build(BuildContext context) {
  assert(_trueSize == null || !(_trueSize!.isEmpty));
  assert(this.textDirection != null || debugCheckHasDirectionality(context));
  final TextDirection iconDirection =
      textDirection ?? Directionality.of(context);
  final IconThemeData iconTheme = IconTheme.of(context);

  final double iconWidth = sizeX ?? iconTheme.size!;
  final double iconHeight = sizeY ?? iconTheme.size!;
  final double fontSize = min(iconWidth, iconHeight); // shortest side

  // A standard Icon uses its `size` field for
  // both width and height. Even without IconData, an IconToo
  // still ought to consider "true size" for Semantics and spacing.
  if (icon == null) {
    return Semantics(
      label: semanticLabel,
      child: SizedBox(width: iconWidth, height: iconHeight),
    );
  }

  // Checked `icon == null` before continuing with any other computation.
  final double? iconThemeOpacity = iconTheme.opacity;
  Color? iconColor = color ?? iconTheme.color;
  if (iconThemeOpacity != 1.0)
    iconColor = iconColor!.withOpacity(iconColor.opacity * iconThemeOpacity!);

  // In the context of Icons, IconButtons, and IconData,
  // the "icon" itself is a character code for a specific font.
  Widget iconGlyph = RichText(
    overflow: TextOverflow.visible, // Though `trueSize`d, could be bad values
    textDirection: iconDirection,
    text: TextSpan(
      text: String.fromCharCode(icon!.codePoint),
      style: TextStyle(
        inherit: false,
        package: icon!.fontPackage,
        fontFamily: icon!.fontFamily,
        fontSize: fontSize,
        color: iconColor,
        shadows: shadows, // optional 👥 List<Shadow>
      ),
    ),
  );

  // Wrap iconGlyph in a Transform and flip if the IconData thus dictates.
  // Hover over / see: `matchTextDirection`.
  if (icon!.matchTextDirection) {
    switch (iconDirection) {
      case TextDirection.rtl:
        // Mirrors over Y-axis
        iconGlyph = Transform(
          transform: Matrix4.identity()..scale(-1.0, 1.0, 1.0), // scales X
          alignment: Alignment.center,
          transformHitTests: false,
          child: iconGlyph,
        );
        break;
      case TextDirection.ltr:
        break;
    }
  }

  // TrueSized by design, this IconToo will side-align considering TextDirection.
  // A standard Icon center-aligns, which is the issue for non-square IconData.
  return Semantics(
    label: semanticLabel,
    child: ExcludeSemantics(
      child: SizedBox(
        width: iconWidth,
        height: iconHeight,
        child: Align(
          alignment: alignment ??
              (iconDirection == TextDirection.rtl
                  ? Alignment.centerRight
                  : Alignment.centerLeft),
          child: iconGlyph,
        ),
      ),
    ),
  );
}