CustomText class
A customizable text widget that extends Flutter's default Text widget with advanced styling and dynamic color handling.
The CustomText widget offers extended control over text appearance by providing a wide range of customizable parameters. It is designed to adapt to dark mode automatically (using darkColor and invertColor) and allows for fine‑tuning of typography including font size, weight, style, decorations, shadows, and more.
The effective text style is computed in the effectiveStyle method. This method takes into account the current platform brightness to adjust the text color, optionally using darkColor if the device is in dark mode. Additionally, if invertColor is set to true, the widget swaps between light and dark colors.
Customization Options
-
Text Content: The text to display is provided as the data parameter.
-
Font Size and Adjustment: Use fontSize to set the base size of the text and adjustSize to add a further adjustment (e.g. for dynamic scaling).
-
Color Handling: color sets the default text color; darkColor overrides color in dark mode. If invertColor is true, the widget uses the opposite of the expected color based on the current brightness.
-
Typography: Customize the text using fontFamily, fontWeight, fontStyle, and fontFamilyFallback.
-
Text Decorations: Control underlines, overlines, etc., with textDecoration, decorationColor, and decorationStyle. decorationThickness also allows you to adjust the thickness.
-
Spacing and Shadows: Use letterSpacing and wordSpacing for spacing adjustments, and shadows to add drop shadows.
-
Layout Options: The widget accepts parameters such as height for line height, backgroundColor,
inputContentPadding(if used in a similar context), and more advanced typography features like fontFeatures, fontVariations, and leadingDistribution. -
Additional Styling: Optional parameters like selectionColor, strutStyle, textAlign, textDirection, locale, textScaleFactor, and textScaler offer further control over text rendering.
-
Miscellaneous: Other parameters like package, debugLabel, and semanticsLabel provide additional customization and accessibility support.
Example Usage
CustomText(
"Hello, World!",
fontSize: 16,
adjustSize: 2,
color: Colors.black,
darkColor: Colors.white,
invertColor: false,
fontWeight: FontWeight.bold,
textDecoration: TextDecoration.underline,
)
If the style parameter is provided, it will be used as-is. Otherwise, the widget computes a default style using the provided customization parameters.
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatelessWidget
- CustomText
Constructors
-
CustomText(String data, {Key? key, double fontSize = 14.0, double adjustSize = 0.0, Color? color, Color? darkColor, bool invertColor = false, TextOverflow? overflow, String? fontFamily, FontWeight fontWeight = FontWeight.normal, FontStyle fontStyle = FontStyle.normal, TextDecoration textDecoration = TextDecoration.none, Color decorationColor = Colors.black, List<
Shadow> shadows = const [], double? height, double? letterSpacing, double? wordSpacing, TextDecorationStyle? decorationStyle, bool? softWrap, int? maxLines, TextStyle? style, Color? backgroundColor, List<String> ? fontFamilyFallback, List<FontFeature> ? fontFeatures, List<FontVariation> ? fontVariations, TextLeadingDistribution? leadingDistribution, String? package, TextBaseline? textBaseline, double? decorationThickness, String? debugLabel, TextHeightBehavior? textHeightBehavior, String? semanticsLabel, TextWidthBasis? textWidthBasis, Color? selectionColor, StrutStyle? strutStyle, TextAlign? textAlign, TextDirection? textDirection, Locale? locale, double? textScaleFactor, TextScaler? textScaler}) -
Creates a CustomText widget with advanced customization options.
const
Properties
- adjustSize → double
-
A value added to fontSize for further adjustment.
final
- backgroundColor → Color?
-
The background color for the text.
final
- color → Color?
-
The default text color.
final
- darkColor → Color?
-
The text color to use in dark mode.
final
- data → String
-
The text content to display.
final
- debugLabel → String?
-
A label for debugging purposes.
final
- decorationColor → Color
-
The color of the text decoration.
final
- decorationStyle → TextDecorationStyle?
-
The style of the text decoration (solid, double, dotted, dashed, wavy).
final
- decorationThickness → double?
-
The thickness of any text decoration.
final
- fontFamily → String?
-
The name of the font family.
final
-
fontFamilyFallback
→ List<
String> ? -
Fallback font families to use if fontFamily is not available.
final
-
fontFeatures
→ List<
FontFeature> ? -
Font features (such as small caps).
final
- fontSize → double
-
The base font size.
final
- fontStyle → FontStyle
-
The style of the font (normal or italic).
final
-
fontVariations
→ List<
FontVariation> ? -
Variations for variable fonts.
final
- fontWeight → FontWeight
-
The weight of the font.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- height → double?
-
The line height as a multiple of the font size.
final
- invertColor → bool
-
If true, the text color is inverted (e.g. dark becomes light and vice versa).
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- leadingDistribution → TextLeadingDistribution?
-
The distribution of line heights.
final
- letterSpacing → double?
-
The space between letters.
final
- locale → Locale?
-
The locale used to render the text.
final
- maxLines → int?
-
The maximum number of lines to display.
final
- overflow → TextOverflow?
-
How overflowing text should be handled.
final
- package → String?
-
The package where the font is located.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- selectionColor → Color?
-
The color of text selection.
final
- semanticsLabel → String?
-
A semantic label for accessibility.
final
-
shadows
→ List<
Shadow> -
Shadows to apply behind the text.
final
- softWrap → bool?
-
If true, text will wrap softly.
final
- strutStyle → StrutStyle?
-
The style of the strut (vertical spacing).
final
- style → TextStyle?
-
A complete TextStyle that overrides all other style parameters.
final
- textAlign → TextAlign?
-
How the text is aligned horizontally.
final
- textBaseline → TextBaseline?
-
The baseline to use for aligning text.
final
- textDecoration → TextDecoration
-
Decoration (underline, overline, etc.) for the text.
final
- textDirection → TextDirection?
-
The direction in which text flows.
final
- textHeightBehavior → TextHeightBehavior?
-
How the text should be adjusted vertically.
final
- textScaleFactor → double?
-
The scaling factor for the text.
final
- textScaler → TextScaler?
-
A custom text scaler.
final
- textWidthBasis → TextWidthBasis?
-
How the width of the text should be measured.
final
- wordSpacing → double?
-
The space between words.
final
Methods
-
build(
BuildContext context) → Widget -
Describes the part of the user interface represented by this widget.
override
-
createElement(
) → StatelessElement -
Creates a StatelessElement to manage this widget's location in the tree.
inherited
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
effectiveStyle(
BuildContext context) → TextStyle - Computes and returns the effective TextStyle for this widget.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited