RichText

  • Usage

    RichText in Flutter allows you to create text with multiple styles within a single widget. It's useful when you want to display text with different fonts, colors, or text styles in a single Text widget.

    
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('RichText Example'),
            ),
            body: Center(
              child: RichText(
                text: TextSpan(
                  style: TextStyle(
                    fontSize: 18.0,
                    color: Colors.black,
                  ),
                  children: <TextSpan>[
                    TextSpan(text: 'This is a '),
                    TextSpan(
                      text: 'bold',
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                    TextSpan(text: ' and '),
                    TextSpan(
                      text: 'italic',
                      style: TextStyle(fontStyle: FontStyle.italic),
                    ),
                    TextSpan(text: ' text example.'),
                    TextSpan(
                      text: ' Colored Text',
                      style: TextStyle(color: Colors.blue),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    
    
    

    In this example:

    RichText is used to create a text widget with different styles.
    TextSpan allows you to define different spans of text with varying styles within the RichText.
    Various TextSpan widgets are used to represent different portions of text with specific styles such as bold, italic, and colored text.