Baiju Chandran
Baiju Chandran

Reputation: 43

How do I align the Richtext to center as it is by default left aligned

How do I align the text (in the RichText) to center in flutter?

Here is my code:

import 'package:flutter/material.dart';


Widget appBar(BuildContext context) {
    return RichText(
        text: TextSpan(

        style: TextStyle(fontSize:22),

        children: <TextSpan >[
            TextSpan(text: 'My', style: TextStyle(fontWeight: FontWeight.w500, color: Colors.black54)),

            TextSpan(text: 'App', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.blue)),

            ],
        ),
    );

}

Upvotes: 1

Views: 2306

Answers (1)

Jay Dangar
Jay Dangar

Reputation: 3479

You can wrap your RichText as a child of center widget like following:

Widget appBar(BuildContext context) {
    return Center(
      child : RichText(
        text: TextSpan(

        style: TextStyle(fontSize:22),

        children: <TextSpan >[
            TextSpan(text: 'My', style: TextStyle(fontWeight: FontWeight.w500, color: Colors.black54)),

            TextSpan(text: 'App', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.blue)),

            ],
        ),
    )
    ); 
}

Upvotes: 2

Related Questions