How to align text in flutter - flutter

I am trying to align the name to the left side but it is not working. Always displays in the center suppose I use text-align. So, How to resolve this issue?
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Name",
maxLines: 1,
overflow: TextOverflow.ellipsis,
softWrap: false,
textAlign: TextAlign.left,
style: const TextStyle(fontSize: 16,color: Colors.black,fontWeight: FontWeight.bold,),
),
const SizedBox( width: 160,
child:Text(
"mynameee#gmail.com",
maxLines: 3,
overflow: TextOverflow.ellipsis,
softWrap: true,
style: const TextStyle(
fontSize: 12,
color: Colors.black,
),
),),
],
),

In column widget add this line :
crossAxisAlignment: CrossAxisAlignment.start
and remove :
mainAxisAlignment: MainAxisAlignment.center,

Add this to column:
crossAxisAlignment: CrossAxisAlignment.start,

Warp your column with Container and then inside the container enter below line:
alignment:Alignment.center

add Container for the text and give it alignment
Like this:
Container(
alignment: Alignment.centerLeft,
child:Text('text here')
)
And you want display the name and email together use Row
Like this:
Row(
mainAxisAlignment: MainAxisAlignment.start,
children:[
Text("name:"),
Text("Example#gmail.com"),
]
)
And the output well be like this:
Name: Example#gmail.com

Container(
alignment: Alignment.centerLeft,
child:Text('text here',textalign:center)
)

Related

Align a text in Flutter to make a restaurant menu

I'm trying to make a menu for a reastaurant like this one : -
I'm using Flutter and this is my code:
return Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
title: Row(
children: [
Expanded(child: Text( text, style: style, )),
const SizedBox( width: 15 ),
Text( '\$ $price', style: style,),
],
),
),
Divider( color: Colors.white.withOpacity( 0.5 ),),
],
);
But this is the result that I'm getting : -
Please, any help will be welcome.
And thanks in advance.
The result that you want to achieve can be done just by using the row widget, you don't have to wrap it up with ListTile. I am writing the code snippet of Row widget for you. You can copy paste it and it will work. Additionally I see that you are generating it in the column widget. If you have a dynamic array then I suggest that you use ListView Divider instead. It will have a divider to add between your rows as well. You can find more about it on flutter official documentation.
Row(
children:[
//You may add some padding to Text for better looking UI.
Text("Chicken Masala"),
Expanded(child:Divider()),
Text("180")
])
Just replace your column's children with this row and you will be able to see it.
I solved your issue like this:
Output of your issue : -
body: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
title: Row(
children: [
const Text(
'Cheese Burger',
),
Expanded(child: Text('.' * 100, maxLines: 1)),
const Text(
'\$ 10',
),
],
),
),
const SizedBox(
height: 10,
),
ListTile(
title: Row(
children: [
const Text(
'Chicken Masala Soup',
),
Expanded(child: Text('.' * 100, maxLines: 1)),
const Text(
'\$ 10',
),
],
),
),
],
),
#Ramji, #Dhruvil Patel, I try your code and it work fine, but it was making an overflow when the text was long, so I made some changes in your example and I got what I was looking for.
Thanks a lot for you help.
Here is what I dit.
return SizedBox(
width: double.infinity,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Expanded( child: Text( text, style: style, maxLines: 2, overflow: TextOverflow.ellipsis, ) ),
],),
Row(
children: [
Expanded(
child: Text(
'.' * 150,
style: style,
maxLines: 1,
overflow: TextOverflow.fade,)),
SizedBox(
width: 80,
child: Text(
'\$ $price',
style: style,
textAlign: TextAlign.right,)),
],
),
// Divider( color: Colors.white.withOpacity( 0.5 ),),
],
),
);

I want to align the starting place of two texts

I have one "row" and two "text" widgets inside. I want to align the starting place of two texts. ie "m. name" and "m105" should line up.
Container(
child: Row(
children: [
Container(
child: Align(
alignment: Alignment.topLeft,
child: Text(
"M. Adı: ",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
),
// SizedBox(
// width: 20,
// ),
Expanded(
child: Text(
"$productName",
overflow: TextOverflow.ellipsis,
maxLines: 3,
softWrap: true,
),
),
],
),
),
Try
Row() widget attribute
crossAxisAlignment: CrossAxisAlignment.start
Text() widget attribute
textAlign: TextAlign.start,
/// Try something like that
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"M. Adı: ",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
SizedBox(width: 5),
Flexible(
child: Text(
"$productName",
textAlign: TextAlign.start,
),
),
],
),
Add crossAxisAlignment: CrossAxisAlignment.center, to your Row widget it should be aligned as you desired or still if it doesnt works just try changing center with other values check which one works for you...
and even after adding above property to your Row widget then you would have to remove alignment of topleft in your text of m.name

Flutter: Text overflow for a Text inside a Column not working

Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(showing.title, style: browseTitle, overflow: TextOverflow.ellipsis,),
Text(showing.dates, style: browseInfo),
SizedBox(
height: 20,
),
Text('Posted by: ' + showing.email, style: browseInfo),
],
),
This code produces this:
But the problem is that the textoverflow I put inside the text doesn't seem to be applying and the text keeps overflowing. How do I solve this so that the text doesn't overflow and it turns into an ellipsis?
CheckOut this work perfect
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: MediaQuery.of(context).size.width,
child: Text(
showing.title,
style: browseTitle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
Text(showing.dates, style: browseInfo),
SizedBox(height: 20),
Text('Posted by: ' + showing.email, style: browseInfo),
],
),

Center Text in a Flutter Column

I am looking for help in centering the "mytitle" text in a column.
I tried centerTitle: true, between 'mytitle' and 'overflow', but it did not work:
Card(
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
child:
Text(
mytitle,
overflow: TextOverflow.ellipsis,
style: CustomTheme.bodyText1.override(
fontFamily: 'Noto Sans',
fontSize: 24,
color: txtcolor),
)
)
]
)
)
)
try to use crossAxisAlignment as follow:
Container(
alignment: Alignment.center,
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
child:
Text(
mytitle,
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: CustomTheme.bodyText1.override(
fontFamily: 'Noto Sans',
fontSize: 24,
color: txtcolor),
)
)
]
)
)
for more information see this. In summary, you can set the layout of Column in two directions. The first one is the vertical axis (MainAxis) and the second one is the horizontal (crossAxis) direction. Row has the same feature but its mainAixs and crossAxis is opposite.

How to align text with 2 lines with a text of one line in flutter

So, I want the row with a string of 2 lines ("Some long string with 2 lines heres") to be properly aligned with the string of 1 line: ("Name:"), but I want to do this WITHOUT checking the string length.
This is my code for the row:
Row(
children: <Widget>[
Text(
title,
style: Theme.of(context).textTheme.headline6.copyWith(height: 1.24),
),
Spacer(),
Container(
width: 242.0,
alignment: Alignment.bottomRight,
child: Text(
text,
textAlign: TextAlign.end,
maxLines: 2,
style: Theme.of(context).textTheme.bodyText1.apply(
color: kGrey,
),
),
),
],
);
As for the result I want, here's an image example:
Use crossAxisAlignment Row parameter to align the text to start crossAxisAlignment: CrossAxisAlignment.start
Solution-
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
title,
style: Theme.of(context)
.textTheme
.headline6
.copyWith(height: 1.24),
),
Spacer(),
Container(
width: 242.0,
alignment: Alignment.bottomRight,
child: Text(
text,
textAlign: TextAlign.end,
maxLines: 2,
style: Theme.of(context).textTheme.bodyText1.apply(
color: kGrey,
),
),
),
],
),
Hope this will help you If you have any doubts ask in comment.