Text Widget
The Text widget displays a string of text with various styles and properties. It supports rich text formatting, alignment, overflow handling, and more.
Properties
text
: string - The text to displaystyle
: TextStyle - Text styling propertiesnew TextStyle({ color?: string, // Text color fontSize?: number, // Font size in pixels fontWeight?: string, // Font weight (e.g., "normal", "bold") fontFamily?: string, // Font family name fontStyle?: FontStyle, // FontStyle.normal or FontStyle.italic height?: number, // Line height as multiple of font size inherit?: boolean // Whether to inherit parent style })
textAlign
: TextAlign - Text alignmentTextAlign.left; TextAlign.right; TextAlign.center; TextAlign.start; // Leading edge (left in LTR, right in RTL) TextAlign.end; // Trailing edge (right in LTR, left in RTL)
overflow
: TextOverflow - How to handle text overflowTextOverflow.clip; // Clip overflowing text TextOverflow.visible; // Show overflowing text TextOverflow.ellipsis; // Show ellipsis (...) for overflow
softWrap
: boolean - Whether text should break at soft line breakstextDirection
: TextDirection - The directionality of the texttextWidthBasis
: TextWidthBasis - How to measure the width of the rendered textTextWidthBasis.parent; // Use parent width as basis TextWidthBasis.longestLine; // Use width of longest line
Common Use Cases
-
Basic Text:
Text("Hello Flitter");
-
Styled Text:
Text("Styled Text", { style: new TextStyle({ fontSize: 24, color: "blue", fontWeight: "bold", }), });
-
Centered Text:
Text("Centered Text", { textAlign: TextAlign.center, style: new TextStyle({ fontSize: 18, }), });
-
Text with Overflow:
Text("This is a very long text...", { overflow: TextOverflow.ellipsis, style: new TextStyle({ fontSize: 16, }), });
-
Text with Line Wrapping:
Text("This is a long text that will wrap to the next line when it reaches the container's width", { softWrap: true, style: new TextStyle({ fontSize: 16, }), });
-
Text with Width Basis:
Text("Multi-line text\nwith varying line lengths", { textWidthBasis: TextWidthBasis.longestLine, style: new TextStyle({ fontSize: 16, }), });
Best Practices
-
Styling Guidelines:
- Use TextStyle for consistent text styling
- Consider font scaling for accessibility
- Use appropriate font sizes for readability
- Keep color contrast in mind
-
Layout Tips:
- Use textAlign for proper alignment
- Consider overflow handling
- Use appropriate line height for readability
- Handle text direction when needed
- Use softWrap for automatic line breaking
- Choose appropriate textWidthBasis for layout calculations
-
Performance Tips:
- Cache TextStyle objects when possible
- Use const constructors
- Consider text measurement impact
- Avoid unnecessary style changes
Common Patterns
-
Title Text:
Text("Title", { style: new TextStyle({ fontSize: 24, fontWeight: "bold", color: "black", }), });
-
Caption Text:
Text("Caption", { style: new TextStyle({ fontSize: 12, color: "gray", height: 1.2, }), });
-
Error Message:
Text("Error: Something went wrong", { style: new TextStyle({ fontSize: 14, color: "red", fontWeight: "bold", }), });
Tips and Tricks
-
Style Tips:
- Use font weight for emphasis
- Consider line height for readability
- Use appropriate font families
- Keep text styles consistent
-
Layout Tips:
- Use Container for text width constraints
- Consider text alignment with parent
- Handle overflow appropriately
- Use padding for text spacing
-
Accessibility Tips:
- Use semantic font sizes
- Maintain good color contrast
- Consider text scaling
- Use appropriate line height