TextSpan Widget in Flutter
Last Updated :
01 Jul, 2021
TextSpan is an immutable span of text. It has style property to give style to the text. It is also having children property to add more text to this widget and give style to the children. Let’s understand this with the help of an example.
Constructors:
Syntax:
TextSpan({String text,
List<InlineSpan> children,
TextStyle style,
GestureRecognizer recognizer,
String semanticsLabel})
Properties:
- text: The text contained in the span.
- children: More spans to include as children.
- style: The TextStyle given to the text.
- recognizer: The gesture detector when user hit the TextSpan widget.
- semanticsLabel: An alternative semantic label for this widget.
- hashCode: This parameter takes in an int value as the object to provide a hash code to the TextSpan widget. Hash code is an integer value that represents the state of the object that effect operator == comparison.
- runtimeType: This property takes in a Type as the object to represent the runtime type of the object. This property supports null safety.
Example:
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'TextSpan' ,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
debugShowCheckedModeBanner: false ,
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'GeeksforGeeks' ),
backgroundColor: Colors.green
),
body: Center(
child: Text.rich(
TextSpan(
text: 'This is textspan ' ,
children: <InlineSpan>[
TextSpan(
text: 'Widget in flutter' ,
style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold),
)
]
)
),
),
backgroundColor: Colors.lightBlue[50],
);
}
}
|
Output:
If the properties are defined as below:
TextSpan(
text: 'This is textspan ',
style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold),
children: <InlineSpan>[
TextSpan(
text: 'Widget in flutter',
)
]
)
The following design changes can be observed:

If the properties are defined as below:
TextSpan(
text: 'This is textspan ',
children: <InlineSpan>[
TextSpan(
text: 'Widget in flutter',
style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold),
)
]
)
The following design changes can be observed:

Explanation:
- Create TextSpan widget and wrap it with Text.rich() widget.
- Give the text to the TextSpan and add more inline children to it.
- Give style to the text and see the output.
Methods:
- build(ParagraphBuilder builder, {double textScaleFactor: 1.0, List<PlaceholderDimensions>? dimensions})
@override
void build (
ParagraphBuilder builder,
{double textScaleFactor: 1.0,
List<PlaceholderDimensions>? dimensions}
)
override
This build method helps in building drawing the paragraph objects. Paragraph can be obtained by applying the style, text and children of this object to the given ParagraphBuilder.
- compareTo(InlineSpan other) → RenderComparison
@override
RenderComparison compareTo (
InlineSpan other
)
override
This method returns the difference between this span and another, in terms of the damage that can be made to the rendering.
- toStringShort() → String
@override
String toStringShort ()
override
This method gives a concise description of the object, which are usually the runtimeType and the hashCode.
- visitChildren(InlineSpanVisitor visitor) → bool
@override
bool visitChildren (
InlineSpanVisitor visitor
)
override
This method moves this TextSpan and its child in pre-order and calls visitor for each span that has text.
Operator:
- operator ==(Object other) → bool
@override
bool operator == (
Object other
)
override
The equality operator.
The default action for all Objects is to return true if and only if this object and other are the same object. This method is overridden to define a different equality relation on a class.
Similar Reads
Flutter - RichText Widget
The RichText widget is used to display text that uses various different styles. The displayed text is described using a tree of TextSpan objects, each of which has its own associated style that is used for that subtree. Depending on the layout constraints the text might break across multiple lines o
3 min read
Opacity Widget in Flutter
The Opacity widget that makes its child partially transparent. This class colors its child into an intermediate buffer and then merges the child back into the scene partially transparent. For values of opacity other than 0.0 and 1.0, this class is relatively expensive as it needs coloring the child
2 min read
RotatedBox Widget in Flutter
The RotatedBox widget is used to rotate its child by an integral number of quarter turns. It is used to orient its child widgets into either horizontal or vertical orientation. Furthermore, it is very lightweight and can be used for designing various UI as it gives flexibility to the user over the D
2 min read
Flutter - TextButton Widget
TextButton is a built-in widget in Flutter which derives its design from Googleâs Material Design Library. It is a simple Button without any border that listens for onPressed and onLongPress gestures. It has a style property that accepts ButtonStyle as value, using this style property developers can
3 min read
What is Widgets in Flutter?
Flutter is Google's UI toolkit for crafting beautiful, natively compiled iOS and Android apps from a single code base. To build any application we start with widgets - The building block of Flutter applications. Widgets describe what their view should look like given their current configuration and
5 min read
OffStage Widget in Flutter
Flutter provides an inbuilt widget called âOffstageâ, which is been used to hide or show any widget programmatically depending on user action/event. Offstage Widget is a widget that lays the child out as if it was in the tree, but without painting anything, without making the child available for hit
4 min read
PageView Widget in Flutter
The PageView widget allows the user to transition between different screens in their flutter application. All you need to set it up are a PageViewController and a PageView. Constructor of PageView class:Syntax: PageView({Key key, Axis scrollDirection, bool reverse, PageController controller, ScrollP
3 min read
Flutter - Inherited Widget
If you are a flutter developer then you know how easy is to create Flutter UI. But when you have lots of widgets in your app and you want to pass data from one widget to another widget, this will be a pain for many developers,s especially for the newbie, this will be really hard for them to pass the
6 min read
Flutter - InkWell Widget
InkWell is the material widget in flutter. It responds to the touch action as performed by the user. Inkwell will respond when the user clicks the button. There are so many gestures like double-tap, long press, tap down, etc. Below are the so many properties of this widget. We can set the radius of
2 min read
Flutter - Padding Widget
Padding widget in flutter does exactly what its name says, it adds padding or empty space around a widget or a bunch of widgets. We can apply padding around any widget by placing it as the child of the Padding widget. The size of the child widget inside padding is constrained by how much space is re
3 min read