|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +import '../models/tooltip_action_config.dart'; |
| 4 | +import '../widget/action_widget.dart'; |
| 5 | +import '../widget/default_tooltip_text_widget.dart'; |
| 6 | + |
| 7 | +class ToolTipContent extends StatelessWidget { |
| 8 | + /// Builds the tooltip content layout based on action position configuration. |
| 9 | + /// |
| 10 | + /// Supports following layouts: |
| 11 | + /// - Vertical layout (actions at bottom) for [TooltipActionPosition.inside] |
| 12 | + /// - Horizontal layout (actions on left/right) for |
| 13 | + /// [TooltipActionPosition.insideLeft] and [TooltipActionPosition.insideRight] |
| 14 | + const ToolTipContent({ |
| 15 | + required this.description, |
| 16 | + required this.titleTextAlign, |
| 17 | + required this.descriptionTextAlign, |
| 18 | + required this.titleAlignment, |
| 19 | + required this.descriptionAlignment, |
| 20 | + required this.tooltipActionConfig, |
| 21 | + required this.tooltipActions, |
| 22 | + required this.textColor, |
| 23 | + this.title, |
| 24 | + this.titleTextStyle, |
| 25 | + this.descTextStyle, |
| 26 | + this.titlePadding, |
| 27 | + this.descriptionPadding, |
| 28 | + this.titleTextDirection, |
| 29 | + this.descriptionTextDirection, |
| 30 | + super.key, |
| 31 | + }) : assert( |
| 32 | + title != null || description != null, |
| 33 | + 'Either title or description must be provided', |
| 34 | + ); |
| 35 | + |
| 36 | + final String? title; |
| 37 | + final TextAlign titleTextAlign; |
| 38 | + final String? description; |
| 39 | + final TextAlign descriptionTextAlign; |
| 40 | + final AlignmentGeometry titleAlignment; |
| 41 | + final AlignmentGeometry descriptionAlignment; |
| 42 | + final TextStyle? titleTextStyle; |
| 43 | + final TextStyle? descTextStyle; |
| 44 | + final Color textColor; |
| 45 | + final EdgeInsets? titlePadding; |
| 46 | + final EdgeInsets? descriptionPadding; |
| 47 | + final TextDirection? titleTextDirection; |
| 48 | + final TextDirection? descriptionTextDirection; |
| 49 | + final TooltipActionConfig tooltipActionConfig; |
| 50 | + final List<Widget> tooltipActions; |
| 51 | + |
| 52 | + @override |
| 53 | + Widget build(BuildContext context) { |
| 54 | + final shouldShowActionsInside = |
| 55 | + tooltipActions.isNotEmpty && tooltipActionConfig.position.isInside; |
| 56 | + final textTheme = Theme.of(context).textTheme; |
| 57 | + |
| 58 | + // Build title widget |
| 59 | + Widget? titleWidget; |
| 60 | + if (title case final title?) { |
| 61 | + titleWidget = DefaultTooltipTextWidget( |
| 62 | + padding: titlePadding ?? EdgeInsets.zero, |
| 63 | + text: title, |
| 64 | + textAlign: titleTextAlign, |
| 65 | + alignment: titleAlignment, |
| 66 | + textColor: textColor, |
| 67 | + textDirection: titleTextDirection, |
| 68 | + textStyle: titleTextStyle ?? |
| 69 | + textTheme.titleLarge?.merge(TextStyle(color: textColor)), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + // Build description widget |
| 74 | + Widget? descriptionWidget; |
| 75 | + if (description case final desc?) { |
| 76 | + descriptionWidget = DefaultTooltipTextWidget( |
| 77 | + padding: descriptionPadding ?? EdgeInsets.zero, |
| 78 | + text: desc, |
| 79 | + textAlign: descriptionTextAlign, |
| 80 | + alignment: descriptionAlignment, |
| 81 | + textColor: textColor, |
| 82 | + textDirection: descriptionTextDirection, |
| 83 | + textStyle: descTextStyle ?? |
| 84 | + textTheme.titleSmall?.merge(TextStyle(color: textColor)), |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + // Build action widget |
| 89 | + Widget? actionWidget; |
| 90 | + if (shouldShowActionsInside) { |
| 91 | + actionWidget = ActionWidget( |
| 92 | + tooltipActionConfig: tooltipActionConfig, |
| 93 | + children: tooltipActions, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + // For vertical action positioning (default), use Column layout |
| 98 | + final contentColumn = Column( |
| 99 | + mainAxisSize: MainAxisSize.min, |
| 100 | + children: <Widget>[ |
| 101 | + if (titleWidget != null) titleWidget, |
| 102 | + if (descriptionWidget != null) descriptionWidget, |
| 103 | + if (actionWidget != null && |
| 104 | + tooltipActionConfig.position.isInsideVertical) |
| 105 | + actionWidget, |
| 106 | + ], |
| 107 | + ); |
| 108 | + |
| 109 | + // If no horizontal action positioning, return vertical layout |
| 110 | + if (actionWidget == null || |
| 111 | + !tooltipActionConfig.position.isInsideHorizontal) { |
| 112 | + return contentColumn; |
| 113 | + } |
| 114 | + |
| 115 | + // For horizontal action positioning, use Row layout |
| 116 | + final gap = SizedBox( |
| 117 | + width: tooltipActionConfig.gapBetweenContentAndAction, |
| 118 | + ); |
| 119 | + return Row( |
| 120 | + mainAxisSize: MainAxisSize.min, |
| 121 | + children: [ |
| 122 | + if (tooltipActionConfig.position.isInsideLeft) ...[ |
| 123 | + actionWidget, |
| 124 | + gap, |
| 125 | + ], |
| 126 | + Flexible(child: contentColumn), |
| 127 | + if (tooltipActionConfig.position.isInsideRight) ...[ |
| 128 | + gap, |
| 129 | + actionWidget, |
| 130 | + ], |
| 131 | + ], |
| 132 | + ); |
| 133 | + } |
| 134 | +} |
0 commit comments