Line data Source code
1 : import 'package:flutter/material.dart';
2 :
3 : /// A widget that displays an icon alongside text in a horizontal row.
4 : ///
5 : /// The [CustomIconText] widget is useful for creating UI elements that combine
6 : /// an icon and text, such as buttons, labels, or informational displays. It
7 : /// provides flexibility in styling and alignment, allowing for consistent
8 : /// presentation across different parts of the application.
9 : ///
10 : /// Example usage:
11 : /// ```dart
12 : /// CustomIconText(
13 : /// icon: Icons.home,
14 : /// text: 'Home',
15 : /// color: Colors.blue,
16 : /// textStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
17 : /// iconSize: 24.0,
18 : /// spacing: 12.0,
19 : /// );
20 : /// ```
21 : class CustomIconText extends StatelessWidget {
22 : /// The icon to display.
23 : ///
24 : /// This icon appears to the left of the text. It is required and cannot be null.
25 : final IconData icon;
26 :
27 : /// The text to display alongside the icon.
28 : ///
29 : /// This text appears to the right of the icon. It is required and cannot be null.
30 : final String text;
31 :
32 : /// The color of the icon and text.
33 : ///
34 : /// If [color] is provided, it overrides the default colors from the current
35 : /// theme for both the icon and text. If not specified, the icon color defaults
36 : /// to the theme's [IconThemeData.color], and the text color defaults to the
37 : /// theme's [TextTheme.bodyMedium.color].
38 : final Color? color;
39 :
40 : /// The alignment of the icon and text within the row.
41 : ///
42 : /// Controls how the children are placed along the main axis (horizontal).
43 : /// Defaults to [MainAxisAlignment.center].
44 : final MainAxisAlignment mainAxisAlignment;
45 :
46 : /// The text style to apply to the [text].
47 : ///
48 : /// If [textStyle] is provided, it overrides the default text style from the
49 : /// current theme. If not specified, it defaults to the theme's [TextTheme.bodyMedium].
50 : final TextStyle? textStyle;
51 :
52 : /// The size of the icon.
53 : ///
54 : /// If [iconSize] is provided, it overrides the default icon size. If not
55 : /// specified, the icon size defaults to the font size of the [textStyle].
56 : final double? iconSize;
57 :
58 : /// The horizontal spacing between the icon and the text.
59 : ///
60 : /// Defaults to 8.0 logical pixels.
61 : final double spacing;
62 :
63 : /// Creates a [CustomIconText] widget.
64 : ///
65 : /// The [icon] and [text] parameters are required and must not be null.
66 : /// The [mainAxisAlignment] defaults to [MainAxisAlignment.center].
67 : /// The [spacing] defaults to 8.0.
68 0 : const CustomIconText({
69 : super.key,
70 : required this.icon,
71 : required this.text,
72 : this.color,
73 : this.mainAxisAlignment = MainAxisAlignment.center,
74 : this.textStyle,
75 : this.iconSize,
76 : this.spacing = 8.0,
77 : });
78 :
79 0 : @override
80 : Widget build(BuildContext context) {
81 : // Determine the effective text style by merging the provided [textStyle]
82 : // with the theme's default if [textStyle] is not provided.
83 0 : final TextStyle effectiveTextStyle = textStyle ??
84 0 : Theme.of(context).textTheme.bodyMedium!.copyWith(
85 0 : color: color ?? Theme.of(context).textTheme.bodyMedium!.color,
86 : );
87 :
88 0 : return Row(
89 : mainAxisSize: MainAxisSize.min,
90 0 : mainAxisAlignment: mainAxisAlignment,
91 : crossAxisAlignment: CrossAxisAlignment.center,
92 0 : children: [
93 : /// The icon widget.
94 : ///
95 : /// Uses the [iconSize] if provided; otherwise, defaults to the font size
96 : /// of the [textStyle]. The color defaults to the theme's icon color
97 : /// unless [color] is specified.
98 0 : Icon(
99 0 : icon,
100 0 : size: iconSize ?? effectiveTextStyle.fontSize,
101 0 : color: color ?? Theme.of(context).iconTheme.color,
102 : ),
103 :
104 : /// Spacer between the icon and text.
105 0 : SizedBox(width: spacing),
106 :
107 : /// The text widget.
108 : ///
109 : /// Uses the [effectiveTextStyle] and ensures that long text is truncated
110 : /// with an ellipsis if it overflows.
111 0 : Flexible(
112 0 : child: Text(
113 0 : text,
114 : style: effectiveTextStyle,
115 : overflow: TextOverflow.ellipsis,
116 : ),
117 : ),
118 : ],
119 : );
120 : }
121 : }
|