buildHandle method Null safety

  1. @override
Widget buildHandle(
  1. BuildContext context,
  2. TextSelectionHandleType type,
  3. double textLineHeight,
  4. [VoidCallback? onTap,
  5. double? startGlyphHeight,
  6. double? endGlyphHeight]
)
override

Builder for iOS text selection edges.

Implementation

@override
Widget buildHandle(BuildContext context, TextSelectionHandleType type, double textLineHeight, [VoidCallback? onTap, double? startGlyphHeight, double? endGlyphHeight]) {
  // iOS selection handles do not respond to taps.

  // We want a size that's a vertical line the height of the text plus a 18.0
  // padding in every direction that will constitute the selection drag area.
  startGlyphHeight = startGlyphHeight ?? textLineHeight;
  endGlyphHeight = endGlyphHeight ?? textLineHeight;

  final Size desiredSize;
  final Widget handle;

  final Widget customPaint = CustomPaint(
    painter: _TextSelectionHandlePainter(CupertinoTheme.of(context).primaryColor),
  );

  // [buildHandle]'s widget is positioned at the selection cursor's bottom
  // baseline. We transform the handle such that the SizedBox is superimposed
  // on top of the text selection endpoints.
  switch (type) {
    case TextSelectionHandleType.left:
      desiredSize = getHandleSize(startGlyphHeight);
      handle = SizedBox.fromSize(
        size: desiredSize,
        child: customPaint,
      );
      return handle;
    case TextSelectionHandleType.right:
      desiredSize = getHandleSize(endGlyphHeight);
      handle = SizedBox.fromSize(
        size: desiredSize,
        child: customPaint,
      );
      return Transform(
        transform: Matrix4.identity()
          ..translate(desiredSize.width / 2, desiredSize.height / 2)
          ..rotateZ(math.pi)
          ..translate(-desiredSize.width / 2, -desiredSize.height / 2),
        child: handle,
      );
    // iOS doesn't draw anything for collapsed selections.
    case TextSelectionHandleType.collapsed:
      return const SizedBox();
  }
}