Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to horizontally center a Text in flutter?

Tags:

I’m trying to horizontally center a text. Please check the below code.

import 'package:flutter/material.dart';  class LoginPage extends StatelessWidget {   @override   Widget build(BuildContext context) {     // TODO: implement build     return Container(         color: Colors.black,         child: Row(           children: <Widget>[             Column(               children: <Widget>[_buildTitle()],             ),           ],         ));   }    Widget _buildTitle() {     return      Center(child: Container(       margin: EdgeInsets.only(top: 100),       child: Column(                  children: <Widget>[           Text(             "something.xyz",             style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 25,),             textAlign: TextAlign.center,           ),         ],       ),     ),);        } } 

This did not center horizontally, instead gave the following output. The margins etc is fine.

enter image description here

How can I fix this?

like image 202
PeakGen Avatar asked Mar 14 '19 15:03

PeakGen


People also ask

How do you center text in a column in flutter?

In flutter, we cannot directly put a text widget at the center of a View layout. We have to use the Center widget with the Center text-align property of Text. Moreover, a combination of both of them makes our Text in the middle of View.


2 Answers

try this

import 'package:flutter/material.dart';  void main() => runApp(MaterialApp(home: LoginPage()));  class LoginPage extends StatelessWidget {   @override   Widget build(BuildContext context) {     // TODO: implement build     return Scaffold(       body: Container(           color: Colors.black,           child: Row(             mainAxisAlignment: MainAxisAlignment.center,             children: <Widget>[               Column(                 mainAxisAlignment: MainAxisAlignment.center,                 children: <Widget>[_buildTitle()],               ),             ],           )),     );   }    Widget _buildTitle() {     return Center(       child: Container(         margin: EdgeInsets.only(top: 100),         child: Column(           children: <Widget>[             Text(               "something.xyz",               style: TextStyle(                 color: Colors.white,                 fontWeight: FontWeight.bold,                 fontSize: 25,               ),               // textAlign: TextAlign.center,             ),           ],         ),       ),     );   } } 
like image 158
Rishabh Avatar answered Oct 24 '22 09:10

Rishabh


You can also solve it with a Container and TextAlign:

Container(    width: double.infinity,    child: Text(       'something.xyz',       textAlign: TextAlign.center,    ), ) 

In this case, the container takes up the entire width with double.infinity. The text adapts to the container and can be moved to the middle of the screen with TextAlign.center.

like image 24
Vasco Avatar answered Oct 24 '22 08:10

Vasco