Design Converter
Education
Last updated on Feb 25, 2025
•7 mins read
Last updated on Feb 15, 2024
•7 mins read
Software Development Executive - II
A Flutter and iOS developer.
Software Development Executive - II
Passionate Flutter developer crafting engaging mobile experiences. Turning coffee into beautiful UIs and bad jokes into commit messages. When not coding, he is probably debugging his life decisions.
Flutter, the popular UI toolkit for crafting natively compiled applications, offers a multitude of widgets to create visually compelling and highly functional apps. Among these widgets, the RichText widget is a powerful tool for displaying text with complex styling. In this blog, we'll delve deep into the intricacies of Rich text and how you can leverage it to enhance your app's text presentation.
Rich text is a text that displays multiple styles within a single cohesive block. In Flutter, rich text is facilitated by the RichText widget, which allows you to combine text spans with various styles into a single paragraph. This is particularly useful when displaying text with multiple styles on the same line or across multiple lines within your app.
The RichText widget is a versatile class that enables text creation with varied formatting. Unlike the basic Text widget, which is suitable for simple text content, the RichText widget can handle complex, styled text that requires different text styles within a single paragraph.
To begin with, let's look at a simple example of a RichText widget in action:
1RichText( 2 text: TextSpan( 3 text: 'Hello ', 4 style: DefaultTextStyle.of(context).style, 5 children: <TextSpan>[ 6 TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)), 7 TextSpan(text: ' Flutter!'), 8 ], 9 ), 10) 11
In the above example, the word "bold" is emphasized with a bold font weight, while the rest of the text follows the default text style.
When it comes to styling the text within a RichText widget, you have many options. Each TextSpan object within the RichText can have its own unique style, allowing for a wide range of visual effects. You might use various properties such as font size, color, weight, and more to achieve the desired look.
1TextSpan( 2 text: 'Flutter is ', 3 style: TextStyle(fontSize: 18), 4 children: <TextSpan>[ 5 TextSpan( 6 text: 'amazing', 7 style: TextStyle(fontWeight: FontWeight.bold, color: Colors.blue), 8 ), 9 TextSpan(text: ' for app development.'), 10 ], 11) 12
In this snippet, the word "amazing" is styled differently from the rest of the sentence, showcasing the flexibility of text styles within a RichText widget.
The RichText widget is just one part of the larger widget tree in a Flutter application. It can be nested within other widgets to create a structured layout. For instance, you might place a RichText widget inside a Padding widget to add some space around the text, or inside an Align widget to adjust its alignment within its parent.
To align text horizontally, you can use the textAlign property of the RichText widget. This property allows you to align the text to the start, end, center, justify, or left and right, depending on the text direction.
1RichText( 2 textAlign: TextAlign.center, 3 text: TextSpan( 4 text: 'Centered ', 5 style: TextStyle(fontSize: 18, color: Colors.black), 6 children: <TextSpan>[ 7 TextSpan( 8 text: 'RichText', 9 style: TextStyle(fontWeight: FontWeight.bold, color: Colors.blue), 10 ), 11 TextSpan( 12 text: ' alignment.', 13 ), 14 ], 15 ), 16) 17
When dealing with rich text, you must consider how the text will be displayed within the layout constraints. If the text is too long to fit within its container, you can use properties like softWrap and overflow to control how the text should behave.
1RichText( 2 text: TextSpan(text: 'This is a very long text that might not fit in one line.'), 3 softWrap: true, 4 overflow: TextOverflow.ellipsis, 5) 6
In this code, if the text exceeds the width of its container, it will wrap onto multiple lines, and if it still overflows, it will end with an ellipsis.
A recent addition to the Flutter framework is the ability to make rich text selectable. Users can select and copy text directly from your app's UI. To enable this feature, the RichText widget must be wrapped within a SelectionArea widget or have a SelectionRegistrar assigned.
1SelectableText.rich( 2 TextSpan( 3 text: 'You can ', 4 children: <TextSpan>[ 5 TextSpan(text: 'select', style: TextStyle(fontWeight: FontWeight.bold)), 6 TextSpan(text: ' this text.'), 7 ], 8 ), 9) 10
With the SelectableText.rich constructor, users can interact with the text just like in a text document.
TextSpan objects are the backbone of the RichText widget. They represent the content and style for a section of text. By nesting TextSpan objects, you can create a style hierarchy that defines the formatting for each part of your rich text.
1TextSpan( 2 text: 'Flutter ', 3 style: TextStyle(color: Colors.blue), 4 children: <TextSpan>[ 5 TextSpan( 6 text: 'RichText ', 7 style: TextStyle(fontWeight: FontWeight.bold, color: Colors.red), 8 ), 9 TextSpan( 10 text: 'example', 11 style: TextStyle(fontStyle: FontStyle.italic), 12 ), 13 ], 14) 15
In this example, each word within the sentence has a different style, demonstrating the flexibility of TextSpan objects.
Flutter's text rendering system offers many ways to customize the appearance of text. From font size and color to letter spacing and shadows, you can use various styles and properties to achieve the exact look you want for your app's text.
To apply multiple styles to your text, you simply define a TextStyle for each TextSpan within your RichText widget. This allows for precise control over the formatting of each piece of text. Let's look at an example where we want to highlight various words within a sentence with different styles.
1RichText( 2 text: TextSpan( 3 text: 'Welcome to ', 4 style: TextStyle(fontSize: 18, color: Colors.black), 5 children: <TextSpan>[ 6 TextSpan( 7 text: 'Flutter ', 8 style: TextStyle(fontWeight: FontWeight.bold, color: Colors.blue), 9 ), 10 TextSpan( 11 text: 'development. ', 12 style: TextStyle(fontStyle: FontStyle.italic, color: Colors.grey), 13 ), 14 TextSpan( 15 text: 'Enjoy ', 16 style: TextStyle(decoration: TextDecoration.underline, color: Colors.green), 17 ), 18 TextSpan( 19 text: 'coding!', 20 style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.orange), 21 ), 22 ], 23 ), 24) 25
Layout constraints are crucial in how text is displayed in your app. Flutter provides properties such as maxLines and textWidthBasis to help you manage how text is laid out, whether you want it on a single line, multiple lines, or within a specific width constraint.
1RichText( 2 text: TextSpan(text: 'This text will not exceed one line.'), 3 maxLines: 1, 4 overflow: TextOverflow.ellipsis, 5) 6
Here, the maxLines property ensures that the text will not wrap beyond a single line, and the overflow is handled gracefully with an ellipsis.
The BuildContext context is pivotal in styling text within a RichText widget. It allows you to access and apply the default text styles to your text, ensuring consistency across your app.
1DefaultTextStyle.of(context).style, 2
By using the DefaultTextStyle.of(context).style, you can apply the ambient text style to your RichText, which can then be overridden for individual TextSpan objects as needed.
Making text selectable enhances the user experience by allowing users to copy and share content from your app. The RichText widget can be configured to support text selection, allowing users to interact with the text more deeply.
To enable text selection in a RichText widget, wrap it in a SelectableArea widget or use a SelectionRegistrar. Here's an example of how to make a RichText widget selectable:
1SelectionArea( 2 child: RichText( 3 text: TextSpan( 4 text: 'You can select ', 5 style: TextStyle(color: Colors.black), 6 children: <TextSpan>[ 7 TextSpan( 8 text: 'this text', 9 style: TextStyle(fontWeight: FontWeight.bold, color: Colors.blue), 10 ), 11 TextSpan( 12 text: ' to copy or share.', 13 ), 14 ], 15 ), 16 ), 17) 18
Rich text is a powerful tool for developers creating sophisticated text layouts with various styles and formatting options. By understanding and utilizing the RichText widget and its associated classes, you can craft text that is not only visually appealing but also interactive and well-integrated within the overall design of your app.
Remember to experiment with different styles, properties, and configurations to discover the full potential of rich text in your Flutter applications. Whether you're building a simple app or a complex interface, the RichText widget is invaluable for displaying text with flair and functionality.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.