White space in text - Flutter - flutter

When using Unicode text in flutter there seems to be a lot of whitespace in each character which makes it hard to align. In the code and picture below, I'm trying to get the 35m sit on the cross arrows with no space in between. I'm also trying to get the three stars at the start to center align vertically with the word starburst and then center align the (35m and arrows), starburst, and thee stars all to center align with each other vertically.
Scaffold(
body: SafeArea(
child: Column(
children: [
Row(
children: [
Text(
'\u{2605}' * 3,
style: TextStyle(
fontSize: 18.0,
),
),
Text(
'Starburst',
style: TextStyle(
fontSize: 28.0,
),
),
Column(
children: [
Text(
'35m',
style: TextStyle(
fontSize: 18.0,
),
),
Text(
'\u{2194}',
style: TextStyle(fontSize: 35.0),
),
],
)
],
),
],
),
),
);

Make Row crossAxisAlignment property to CrossAxisAlignment.center
Row(
crossAxisAlignment: CrossAxisAlignment.center
children: [....
And
Make Column mainAxisAlignment propert to
MainAxisAlignment.center
Column(
mainAxisAlignment: MainAxisAlignment.center
children: [...

You can use Stack instead of Column and align the Text as per your UI specifications:
Output Using Column:
Output Using Stack:
Full code:
return Scaffold(
body: SafeArea(
child: Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'\u{2605}' * 3,
style: TextStyle(
fontSize: 18.0,
),
),
Text(
'Starburst',
style: TextStyle(
fontSize: 28.0,
),
),
// SizedBox(width: 5.0),
Container(
width: 50.0,
child: Center(
child: Stack(
children: [
Positioned(
top: 4,
child: Text(
'35m',
style: TextStyle(
fontSize: 18.0,
),
),
),
Text(
'\u{2194}',
style: TextStyle(fontSize: 40.0),
),
],
),
),
),
],
),
],
),
),
);
You might have to tweak Stack widget's children a bit, but I hope this answer gives the idea.

Related

How to align three items, two on the sides and one on the center in Flutter?

Obviously, if two items only, MainAxisAlignment.space Between would be enough, but I have three items, so I have tried with Align:
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Align(
alignment: Alignment.topLeft,
child: Text('Left',
style: TextStyle(
fontSize: 8.0,
fontWeight: FontWeight.bold,
),),
),
Align(
alignment:Alignment.center,
child: Text('Center',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold),
),
),
Align(
alignment: Alignment.topRight,
child: Text('Right',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
)),
),
],
),
But contents are still aligned to center in general, I also tried using Spacer() before and after the centered element, the result was much like spaceEvenly but with spaces in between.
What I want is the centered element to be fixed in the center no matter what other elements contentes, the left element to be fixed on the left, the right element on the right.
EDIT
space Between is not working for sure, see doc:
MainAxisAlignment.spaceBetween:
Divides the extra space evenly between children.
This does not garantie that the centered element go to center.
Try below code, remove Align Widget refer Flutter Layout here
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Left',
style: TextStyle(
fontSize: 8.0,
fontWeight: FontWeight.bold,
),
),
Text(
'Center',
style: TextStyle(fontSize: 9.0, fontWeight: FontWeight.bold),
),
Text(
'Right',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
),
),
],
),
Result->
Answer Updated:
Wrap your Text widget inside Expanded or Flexible Widget
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
'Leftvsdvjwhgehdxuguhe duyxegfygwedue dw',
style: TextStyle(
fontSize: 8.0,
fontWeight: FontWeight.bold,
),
),
),
Expanded(
child: Text(
'Center',
style: TextStyle(fontSize: 9.0, fontWeight: FontWeight.bold),
),
),
Expanded(
child: Text(
'Right',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
),
),
),
],
),
Result Image->
Finally, to get rid of the equal space caused by Expanded, I removed Expanded from the centered element, then set the fit properly to loose for the left and right elements.
To align sides, I surrounded the left and right elements with 'Row', to set 'MainAxisAlignment' to start and end respectively, now it works:
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
fit: FlexFit.loose,//Added this
child: Row(
//Surround the Left element with Row aligned to start
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'Left',
style: TextStyle(
fontSize: 8.0,
fontWeight: FontWeight.bold,
),
),
]),
),
//No expanded needed to the centered child
Text( 'Center',style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
),),
Expanded(
fit: FlexFit.loose,//Added this too
child: Row( //Surround the right element with Row aligned to end
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
'Right',
style: TextStyle(
fontSize: 9.0,
fontWeight: FontWeight.bold,
),
),
]),
),
],
)
Like #Yunus Emre Çelik said, you dont need to add Align, spaceBetween is already doing that. But, if your problem still persists, maybe you have a Column as parent of your Row that need crossAxisAlignment: CrossAxisAlignment.stretch,. Or if you dont, check if your Row can expand all width space available.
Anyway you can set your Row like that:
Row(
children: [
Expanded(...),
Expanded(...),
Expanded(...),
])
This force to set equals space to each child
But if you dont want to set equals space for all, can do that:
Row(
mainAxisAlignment: MainAxisAlignment.center, // <- center items
children: [
Flexible( // <-- flexible space
child: Container(
alignment: Alignment.centerLeft,
color: Colors.red,
child: Text('left left left left left left left left left left left '),
),
),
Container( // <-- center
alignment: Alignment.center,
color: Colors.blue,
child: Text('center'),
),
Flexible( // <-- flexible space
child: Container(
alignment: Alignment.centerRight,
color: Colors.green,
child: Text('right'),
),
),
],
),

how to auto expand when text is too long in Row in flutter

Now I am using this code to show text in flutter, this is my code:
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8.0),
child: Text(
item.subName,
softWrap: true,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
],
),
sometimes the text item.subName is too long, how to make it auto expand or render in new line when the text overflow? I have tried to add this but still not work:
softWrap: true,
This is the error:
Try this
new Container(
child: Row(
children: <Widget>[
Flexible(
child: new Text("sample text"))
],
));
Row(
children: <Widget>[
Expanded(
child: new Text("your text"))
],
)
Widget build(BuildContext context){
final size = MediaQuery.of(context).size;
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: size.width * 0.4, // Choose a number that works for you
child: Text(
item.subName,
softWrap: true,
overflow: TextOverflow.clip,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
],
),

How to fix top position in column in flutter?

If I add widgets in column, the top of column is going up.(picture 2)
Is there any way to fixed the top of column?(picture 3)
picture1
Column(
crossAxisAlignment: CrossAxisAlignment.center
mainAxisAlignment: MainAxisAlignment.center
children: <Widget>[
Text("Test1")
]
)
picture2
Column(
crossAxisAlignment: CrossAxisAlignment.center
mainAxisAlignment: MainAxisAlignment.center
children: <Widget>[
Text("Test1"),
Text("Test2")
]
)
Recreated what you want from the picture attached above:
Make two Expanded widgets and let the 2nd Expanded widget take a Column where you will put your Texts widgets:
return Scaffold(
body: Column(
children: [
Expanded(
child: SizedBox(),
),
Expanded(
child: SizedBox.expand(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Test 1',
style: TextStyle(
fontSize: 20,
color: Colors.red,
),
),
Text(
'Test 2',
style: TextStyle(
fontSize: 20,
color: Colors.red,
),
),
Text(
'Test 3',
style: TextStyle(
fontSize: 20,
color: Colors.red,
),
),
],
),
),
),
],
),
);
RESULT:
Use mainAxisAlignment: MainAxisAlignment.start

Flutter different alignment in Column

I have a column with three rows:
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
print('card of course clicked');
},
child: Card(
shape: RoundedRectangleBorder(borderRadius:
BorderRadius.circular(8.0)),
child: Container(
height: 144,
child: Row(children: [
Padding(
padding: EdgeInsets.all(16.0),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
child: Image.network(_model._schoolThumb))),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 16),
Align(
alignment: Alignment.center,
child: Text(_model._schoolName,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18)),
),
SizedBox(height: 12),
Row(
children: <Widget>[
Icon(Icons.location_on, color: Colors.grey[700]),
Container(width: 8),
Text(_model._guides,
style: TextStyle(
fontSize: 14,
color: Colors.black,
fontWeight: FontWeight.w400)),
],
),
SizedBox(height: 12),
Row(
children: <Widget>[
Icon(Icons.attach_money, color: Colors.grey[700]),
Container(width: 8),
Text(_model._priceFrom,
style: TextStyle(
fontSize: 14,
color: Colors.black,
fontWeight: FontWeight.w400))
],
)
],
)
]),
),
),
);
}
As a result, I have three rows aligned to start.
How can I align the first Text() in the center? Giving properties to the text and wrapping in Expanded, Container, Stack didn't help, or I did it in the wrong way. Thanks in advance!
EDIT
Whole build() method attached
Problem:
By default, the width of the Column is set to the width of the widest child widget of Column.
Solution:
You need to wrap your Column with either Expanded or Flexible.
Like below.
Expanded(
child: Column(
children: <Widget>[
...
],
),
)
or
Flexible(
child: Column(
children: <Widget>[
...
],
),
)
And then need to use the Align or Center widget to set the alignment to your child widget in your case the Text widget.
Like below:
Align(
alignment: Alignment.center,
child: Text(
"Test",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18,
),
),
),
or
Center(
child: Text(
"Test",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18,
),
),
),
UPDATE
You haven't given a width to the column so it does not know the remaining width that it can take. So wrap your column in a flexible which will give it the width remaining to expand.
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 16),
Row(

FLUTTER | Right Align not working

Trying to right align a container with text view child, it has a row parent which is inside another column, tried number of solutions from Stack Overflow, didn't work.
Code
///Transactions Heading
new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Container(
alignment: Alignment.centerLeft,
margin: new EdgeInsets.only(top: 20.0, left: 10.0),
child: new Text(
"Transactions",
style: new TextStyle(
color: primaryTextColor,
fontSize: 25.0,
fontWeight: FontWeight.w700,
letterSpacing: 0.1,
),
),
),
new Container(
margin: new EdgeInsets.only(top: 25.0),
child: new Text(
currentGoalTransactions.length.toString() + " items",
style: new TextStyle(
color: darkHeadingsTextColor, fontSize: 15.0),
),
),
],
),
]);
Want to align the 2 items text to the right
Screenshot
Use the mainAxisAlignment property in Row widget.
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
...
)
Use Spacer which will get remaining space and will divide the widget equally
Row(
children: <Widget>[
Text("Text 1", style: TextStyle(fontSize: 20),),
Spacer(),
Text("Text 2", style: TextStyle(fontSize: 20),),
Spacer(),
Text("Text 3", style: TextStyle(fontSize: 20),),
],
),
Output:
Screenshot:
You can also try Wrap like this:
Wrap(
spacing: 50, // spacing between each of the widgets below
children: <Widget>[
Text("One"),
Text("Two"),
Text("Three"),
],
)