Master the Art of Text Centering in Flutter: 6 Proven Technique

siddhardha
4 min readJan 15, 2023

--

Flutter’s text widget is the ultimate powerhouse of displaying text in your mobile application. It’s a Swiss Army knife of text customization, boasting an array of features that make it a breeze to use. From different text styles to internationalization support and even interactive elements, the text widget has got you covered. But that’s not all, the text widget also boasts high-quality rendering capabilities that ensure your text always looks crisp and clear on the screen. In short, the text widget is a crucial and versatile part of the Flutter framework, and a must-have tool for building visually striking and functional mobile apps.

Learn more about the capabilities of Flutter’s text widget in the official documentation: https://flutter.dev/docs/cookbook/design/fonts

As a designer, developer or user, you want to make sure that your text is aesthetically pleasing, balanced and easy to read. One way to achieve this is by centering the text. By centering the text, you can make it look more even and visually appealing. It also helps in aligning the text with other elements on the screen such as pictures or buttons. This can make your app look more organized and professional. Lastly, centering the text can make it easier to read, as it helps the reader’s eye track the text as they move down the page. In conclusion, centering text within a Flutter text widget can be a useful way to enhance the look and feel of your phone app, making it more user-friendly and visually pleasing.

With the help of this article, you will learn 6 proven techniques to center text in Flutter that will make your app look professional. So, take your design skills to the next level by mastering the art of text centering in Flutter with these techniques.

Centering text in Flutter can be a bit tricky, but with the right techniques, it can be done easily.

In this article, we will explore six proven techniques to center text in Flutter.

  • Using the Center widget: The Center widget is a simple way to center a child within itself. The child passed to the Center widget will be centered both horizontally and vertically. Here's an example of using the Center widget to center a text:
Center(
child: Text("Hello World"),
)
  • Using the Align widget: The Align widget is another way to align a child within a specified position. The Alignment.center property is used to center the text within the Align widget. Here's an example of using the Align widget to center a text:
Align(
alignment: Alignment.center,
child: Text("Hello World"),
)
  • Using Row and Column widget with mainAxisAlignment and crossAxisAlignment property: If you want to center text inside a Row or Column widget then you can use mainAxisAlignment: MainAxisAlignment.center and crossAxisAlignment: CrossAxisAlignment.center properties. Here's an example of using the Row widget to center a text:
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("Hello World"),
],
)
  • Using Flex widget with mainAxisAlignment and crossAxisAlignment property: Just like Row and Column you can use mainAxisAlignment: MainAxisAlignment.center and crossAxisAlignment: CrossAxisAlignment.center properties to center text inside a Flex widget. Here's an example of using the Flex widget to center a text:
Flex(
direction: Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("Hello World"),
],
)
  • Using Expanded widget with Flex widget: You can use the Expanded widget with Flex widget to center text. By wrapping the text widget in Expanded and set Flex mainAxisAlignment to MainAxisAlignment.center and crossAxisAlignment to CrossAxisAlignment.center
Flex(
direction: Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: Text("Hello World"),
),
],
)
  • Using Container widget with alignment property : You can use the Container widget with alignment: Alignment.center property to center text.
Container(
alignment: Alignment.center,
child: Text("Hello World"),
)

These are just a few examples of how to center text in Flutter. Depending on your specific use case, you may need to adjust the code to fit your needs. But with these techniques, you should be able to center text in Flutter with ease. It’s good practice to experiment with different techniques and choose the one that works best for your specific use case.

Learn more about text centering techniques in Flutter by following this tutorial: https://flutter.dev/docs/development/ui/layout/tutorial

In conclusion, centering text in Flutter can be achieved in multiple ways, such as using the Center widget, Align widget, Row, Column, Flex and Container widget with alignment property. Each method has its own advantages and disadvantages, it’s good to understand the concept behind them and choose the one that best fits your use case.

Thank you for taking the time to read this blog. I hope you found the information provided to be helpful and informative. If you have any further questions or comments, please don’t hesitate to reach out. Thank you again for reading!

If you liked this article, don’t forget to leave a clap and follow!

--

--

siddhardha

Computer Science Engineering Undergrad | Freelance Coder | Writer | Flutter developer | Python developer | C/C++ programmer | UI design | Blockchain Developer