Open In App

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 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:

Example:




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:



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.

Article Tags :