Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter – Wrap doesn't center RichText

Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: SingleChildScrollView(
          child: Center(
            child: SizedBox(
              width: screenWidth * 0.8,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    color: Colors.yellow,
                    padding: const EdgeInsetsDirectional.only(start: 16.0),
                    child: SizedBox(
                      width: double.infinity,
                      child: Wrap(
                        runAlignment: WrapAlignment.center,
                        crossAxisAlignment: WrapCrossAlignment.center,
                        alignment: WrapAlignment.center,
                        children: [
                          RichText(
                            text: TextSpan(
                              text: 'afda fd afd asf das fds',
                              children: [
                                TextSpan(
                                  text: 'fda fda fd asfd sa',
                                  style: TextStyle(
                                    color: Theme.of(context).primaryColor,
                                    decoration: TextDecoration.underline,
                                  ),
                                  recognizer: TapGestureRecognizer()
                                    ..onTap = () {
                                      // Handle the click action for terms and conditions link
                                      print("tap");
                                    },
                                ),
                                const TextSpan(
                                  text: ' fdsa ',
                                ),
                                TextSpan(
                                  text: 'fdsafdafda sf dsa',
                                  style: TextStyle(
                                    color: Theme.of(context).primaryColor,
                                    decoration: TextDecoration.underline,
                                  ),
                                  recognizer: TapGestureRecognizer()
                                    ..onTap = () async {
                                      await launchUrl(
                                          Uri.parse('https://flutter.dev'));
                                    },
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    )

This is the result:
enter image description here

I want the second line of text to be centered. If I put a few containers as children of Wrap, it does center them, but it doesn’t work for the RichText or Text.rich(…).
The app is rtl so the text is on the right.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To center the text, you need to use textAlign: TextAlign.center

RichText(
  textAlign: TextAlign.center,
  text: TextSpan(

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading