getTargetRect method
取得當下的index需要高亮的目標
Implementation
RRect? getTargetRect({
bool rootOverlay = false,
}) {
if (targetKey != null) {
var key = targetKey!;
final keyContext = key.currentContext;
if (keyContext == null) {
return null;
}
final renderBox = keyContext.findRenderObject() as RenderBox?;
if (renderBox == null) {
return null;
}
final size = renderBox.size;
BuildContext? parentContext;
if (rootOverlay) {
parentContext =
keyContext.findRootAncestorStateOfType<OverlayState>()?.context;
} else {
parentContext =
keyContext.findAncestorStateOfType<NavigatorState>()?.context;
}
Offset offset;
if (parentContext != null) {
offset = renderBox.localToGlobal(
Offset.zero,
ancestor: parentContext.findRenderObject(),
);
} else {
offset = renderBox.localToGlobal(Offset.zero);
}
final endOffset = offset.translate(size.width, size.height);
Rect rect;
if (targetPadding != null) {
final paddingStartOffset = offset.translate(
-targetPadding!.left,
-targetPadding!.top,
);
final paddingEndOffset = endOffset.translate(
targetPadding!.right,
targetPadding!.bottom,
);
rect = Rect.fromPoints(paddingStartOffset, paddingEndOffset);
} else {
rect = Rect.fromPoints(offset, endOffset);
}
if (borderRadius != null) {
return RRect.fromRectAndCorners(
rect,
topLeft: borderRadius!.topLeft,
topRight: borderRadius!.topRight,
bottomLeft: borderRadius!.bottomLeft,
bottomRight: borderRadius!.bottomRight,
);
} else {
return RRect.fromRectXY(rect, 0, 0);
}
} else {
return targetRect;
}
}