Skip to content
Related Articles

Related Articles

Flutter – RichText Widget

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 06 Jun, 2022
View Discussion
Improve Article
Save Article

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 or might all be displayed on the same line.

Constructors:

Syntax:

RichText(
{Key key,
@required InlineSpan text,
TextAlign textAlign: TextAlign.start, 
TextDirection textDirection, 
bool softWrap: true, 
TextOverflow overflow: 
TextOverflow.clip, 
double textScaleFactor: 1.0, 
int maxLines, 
Locale locale, 
StrutStyle strutStyle, 
TextWidthBasis textWidthBasis: TextWidthBasis.parent, 
TextHeightBehavior textHeightBehavior,

Properties:

  • children: The widgets below this widget in the tree.
  • hashCode: The hash code for this object.
  • key: Controls how one widget replaces another widget in the tree.
  • runtimeType: A representation of the runtime type of the object.
  • text: The text to display in this widget.
  • textAlign: How the text should be aligned horizontally.
  • local: This property takes in Locale class as the object. It controls the font used for the text depending on the language used.
  • maxLines: The maxLines property takes in an int value as the object. It controls the maximum number of lines that can be there for the text to expand and wrap.
  • overflow: TextOverflow enum is the object given to its class it controls the text in case of overflow.
  • softWrap: This property takes in a boolean value as the object. If it is set to false the gulphs in the text become wider.
  • textDirection: This property takes in TextDirection class as the object to decide the direction of the text. It can be either from left-to-right or right-to-left.
  • textHightBehaviour: TextHeightBehavior class is the object given to this property. It controls how the text will be highlighted.
  • textScaleFactor: This property is taken in a double value as the object to determine the relative size of the font.
  • textWidthBasis: TextWidthBasis enum is the object of this property. It controls the width of a single line of text being measured.

Example:

Dart




import 'package:flutter/material.dart';
 
void main() {
  runApp(const MyApp());
}
 
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
 
  // This widget is
  //the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ClipOval',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePAGE(),
      debugShowCheckedModeBanner: false,
    );
  }
}
 
class MyHomePAGE extends StatefulWidget {
  const MyHomePAGE({Key? key}) : super(key: key);
 
  @override
  // ignore: library_private_types_in_public_api
  _MyHomePAGEState createState() => _MyHomePAGEState();
}
 
class _MyHomePAGEState extends State<MyHomePAGE> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GeeksforGeeks'),
        backgroundColor: Colors.green,
      ),
      body: Center(
          child: RichText(
        // Controls visual overflow
        overflow: TextOverflow.clip,
 
        // Controls how the text should be aligned horizontally
        textAlign: TextAlign.end,
 
        // Control the text direction
        textDirection: TextDirection.rtl,
 
        // Whether the text should break at soft line breaks
        softWrap: true,
 
        // Maximum number of lines for the text to span
        maxLines: 1,
 
        // The number of font pixels for each logical pixel
        textScaleFactor: 1,
        text: TextSpan(
          text: 'Hello ',
          style: DefaultTextStyle.of(context).style,
          children: const <TextSpan>[
            TextSpan(
                text: 'Geeks', style: TextStyle(fontWeight: FontWeight.bold)),
          ],
        ),
      )),
      backgroundColor: Colors.lightBlue[50],
    );
  }
}
 
class MyClip extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return const Rect.fromLTWH(0, 0, 100, 100);
  }
 
  @override
  bool shouldReclip(oldClipper) {
    return false;
  }
}

Output:

Richtext in flutter

Output explanation:

  1. Create a stateless widget as the main widget of the app.
  2. Define a class representing the home screen using a stateful widget.
  3. Define the appbar inside a scaffold widget.
  4. Place the Richtext widget in the center.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!