I was learning Flutter and wanted to learn to how structure layout. Thus, I decided to use column widget and wanted to ask how to center horizontally column widget having this code: import
'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Column(
children: <Widget>[
CircleAvatar(
radius: 50.0,
backgroundImage: AssetImage('images/avatar.jpeg'),
),
Text('SAM',
style: TextStyle(
fontFamily: 'Pacifico',
fontSize: 50.0,
color: Colors.white,
fontWeight: FontWeight.bold)),
Text('Flutter developer',
style: TextStyle(
// fontFamily: 'Pacifico',
fontSize: 30.0,
color: Colors.white,
fontWeight: FontWeight.bold))
],
))),
),
);
}
}
You can do this by setting the Column crossAxisAlignment property to CrossAxisAlignment.center
Try wrapping your Column in a Container widget and give it a width property.
Check the code below:
It works perfectly:
'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
Container(
// set the height property to take the screen width
width: MediaQuery.of(context).size.width,
child: Column(
// set the crossAxisAlignment property to center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
CircleAvatar(
radius: 50.0,
backgroundImage: AssetImage('images/avatar.jpeg'),
),
Text('SAM',
style: TextStyle(
fontFamily: 'Pacifico',
fontSize: 50.0,
color: Colors.white,
fontWeight: FontWeight.bold)),
Text('Flutter developer',
style: TextStyle(
// fontFamily: 'Pacifico',
fontSize: 30.0,
color: Colors.white,
fontWeight: FontWeight.bold))
],
))),
),
),
);
}
}
I hope this helps.
The problem is that your screen is not tacking whole width, so it is centre widget, whatever sized is used.
To used whole width you can provide container size to max size, which will force widget to use full width and it will centre widget.
MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Container(
width: MediaQuery.of(context).size.width, // added
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
CircleAvatar(
radius: 50.0,
backgroundImage: AssetImage('assets/images/crown 1.png'),
),
Text('SAM',
style: TextStyle(
fontFamily: 'Pacifico',
fontSize: 50.0,
color: Colors.white,
fontWeight: FontWeight.bold)),
Text('Flutter developer',
style: TextStyle(
// fontFamily: 'Pacifico',
fontSize: 30.0,
color: Colors.white,
fontWeight: FontWeight.bold))
],
),
))),
);
Case 1: If you want Column to be center aligned:
//First Way: Use Column Widget inside Center Widget
Center(
child: Column(
children: <Widget>[]
)
//Second Way: Use Container Widget and set alignment to center
Container(
alignment: Alignment.center,
child: Column(
children: <Widget>[]
)
)
Case 2: If you want Column children to be center aligned
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[]
)
if you want to align widgets in center (vertically and horizontally), you can use
Center(
child: Column(
children: <Widget>[]
)
if you want to align horizontally but at the top of the screen then
Column(
crossAxisAlignment: CrossAxisAlignment.stretch, // to cover all the area
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [Text("test data")],
)
],
)
worked for me.
And if you want to center vertically and at the beginning of the screen then
Row(
crossAxisAlignment: CrossAxisAlignment.stretch, // to cover all the area
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [Text("test data")],
)
],
works as well.
Related
Hi All I am learning flutter and I want the text to be align-center inside the column but the property crossAlignmentCenter is not working can anyone look and told what i am doing wrong here
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("hello"),
),
body: Padding(
padding: const EdgeInsets.only(left: 32,right: 32,top: 32),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
child: Text("Log In", style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w400,
color: Colors.black,
),),
)
],
),
),
backgroundColor: Colors.teal,
);
}
}```
You need to wrap the Column widget with a SizedBox or a Container with the width of double.infinity. This allows Column to extend as much as the parent allows.
Replace Padding widget with this,
Container(
width: double.infinity,
padding: const EdgeInsets.only(left: 32, right: 32, top: 32),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Log In",
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w400,
color: Colors.black,
),
)
],
),
),
The reason is that by default columns do not take all available space on the cross axis. So in your example, the column is only as thick as the child it holds. Therefore centering it does not change anything. If you are looking for a solution go to Why CrossAxisAligment not Working in Flex,Row and Column?
If you are expecting to make it the vertical center of the column add mainAxisAlignment for your column widget.
mainAxisAlignment: MainAxisAlignment.center,
If want to have it horizontal center replace your column widget to row and make sure to set mainAxisAlignment and crossAxisAlignnment to center
you can wrap your Text widget inside an Align widget to do this. this is your fixed code:
import 'package:flutter/cupertino.dart';
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("hello"),
),
body: Padding(
padding: const EdgeInsets.only(left: 32,right: 32,top: 32),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
child: Align(
alignment: Alignment.center,
child:Text("Log In", style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w400,
color: Colors.black,
),)
) ,
)
],
),
),
backgroundColor: Colors.teal,
);
}
}
For some reason I can't get these text widgets to center. I've tried using Center and axisalignment but nothing seems to move the text. Surprisingly to me, the image centers itself. What do I need to change to make the text widgets align to center?
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
backgroundColor: Colors.black,
body: Center(
child: Column(
children: <Widget>[
showImage(),
showDate(),
showName()
],
),
),
);
}
Widget showName() => Center(child: Container(child: showTitle(model!.name!)));
Widget showDate() => Center(child: Container(child: showTitle(model!.dateTime!)));
Widget showImage() => Center(child: Container(height: MediaQuery.of(context).size.height * .8,
child: Image.network(model!.pathImage!)),
);
}
Widget showTitle(String string) {
return Row(
children: <Widget>[
Center(
child: Container(
child: Text(
string,
style: TextStyle(
fontSize: 40.0,
fontWeight: FontWeight.bold,
color: Color.fromARGB(0xff, 0x00, 0x2f, 0x6c),
),
),
),
),
],
);
}```
Add a mainAxisAlignment to the Row inside your showTitle
Row(
mainAxisAlignment: MainAxisAlignment.center, //Here define the position "center"
children: <Widget>[
Center(
child: Container(
child: Text(
string,
style: TextStyle(
fontSize: 40.0,
fontWeight: FontWeight.bold,
color: Color.fromARGB(0xff, 0x00, 0x2f, 0x6c),
),
),
),
),
],
);
Add this to your column
crossAxisAlignment: CrossAxisAlignment.stretch,
I think the problem is that your Container has not height and width. Try add width and height to your Container.
child: Container(
width: 30.0,
height: 30.0,
child: Text(...),
),
I'm trying to fit text to the container with FittedBox, but it doesn't work. On device text goes to the right and it does not break to the next line. (On device I have right overflowed warning).
Do someone know what's wrong with this code? I think there is some issue with Row and Column combination so then FittedBox doesn't affect text, but I'm not sure.
Thanks.
class HomePage extends StatelessWidget {
final Greetings greetings = new Greetings();
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
SafeArea(
child:
SizedBox(height: MediaQuery.of(context).size.height * 0.025)),
Center(child: greetings),
/*ListView(
children: [greetings],
)*/
],
),
);
}
}
class Greetings extends StatelessWidget {
#override
Widget build(BuildContext context) {
var bought = true;
var color;
if (bought) {
color = Colors.blue[700];
} else {
color = Colors.red;
}
return Container(
decoration: BoxDecoration(
color: color, borderRadius: BorderRadius.all(Radius.circular(10))),
height: MediaQuery.of(context).size.height * 0.40,
width: MediaQuery.of(context).size.width * 0.85,
child: Padding(
padding: new EdgeInsets.all(20),
child: Row(
//mainAxisAlignment: MainAxisAlignment.start, --> NOT NECESSARY
children: [
Column(
//mainAxisAlignment: MainAxisAlignment.start, --> NOT NECESSARY
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'hi',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
),
SizedBox(height: 10),
FittedBox(
fit: BoxFit.contain,
child: Text(
'kodsajhnidoasdoisahioasdohiasdhioúsadiophas',
style: TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.normal),
)),
],
)
],
)));
}
}
At first FittedBox does not break the text to the new line, it just decrease or increases the fontSize on it. secondly you are now providing any constraints for the FittedBox wrap it with a container and set its width.
Just use Flexible Widget to make it parent for Column
Flexible(
child: Column(
//...
));
Full Example:
Container(
decoration: BoxDecoration(
color: color, borderRadius: BorderRadius.all(Radius.circular(10))),
height: MediaQuery.of(context).size.height * 0.40,
width: MediaQuery.of(context).size.width * 0.85,
child: Padding(
padding: new EdgeInsets.all(20),
child: Row(
//mainAxisAlignment: MainAxisAlignment.start, --> NOT NECESSARY
children: [
Flexible(
child: Column(
//mainAxisAlignment: MainAxisAlignment.start, --> NOT NECESSARY
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'hi',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
),
SizedBox(height: 10),
FittedBox(
fit: BoxFit.contain,
child: Text(
'kodsajhnidoasdoisahioasdohiasdhioúsadiophas',
style: TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.normal),
)),
],
),
)
],
),
),
);
Row Widget causing the issue as it is taking full space horizontally, by adding Flexible it can shrink/grow according to text.
I have this two items that once needs to appear in the middle but the other other needs to move to the bottom.
import 'package:flutter/material.dart';
class WelcomeScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'K',
style: TextStyle(
fontSize: 200,
color: Colors.white,
decoration: TextDecoration.none,
fontStyle: FontStyle.italic,
),
),
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [Text('hello')],
)
],
),
);
}
}
This is how it looks.
I want to move the Hello Text to the bottom. How can I do that?
You need to use two things for that, to make it work more efficiently. These are:
Expanded Class -> This will provide space, that is, takes up the remaining space
Align class -> This will help you to align the item
Please Note: Align(), itself, cannot aligns, the item, unless space is given. If you just use the Align(), you will not see any change, cos Text('Hello') looks for space, but no space is there.
Code
Container(
width: double.infinity, // <-- Takes up total width of the device
height: double.infinity, // <-- Takes up the total height of the device
color: Colors.blue,
child: Column(
children: [
Expanded(
flex: 2, // <-- Flex takes care of the remaining space ratio
child: Center(
child: Text(
'K',
style: TextStyle(
fontSize: 200,
color: Colors.white,
decoration: TextDecoration.none,
fontStyle: FontStyle.italic,
),
)
)
),
Align(
alignment: Alignment.bottomCenter,
child: Text('Hello', style: TextStyle(color: Colors.white, fontSize: 17.0))
)
]
)
)
Result
If you don't want your K text to move on top, you would have to use Stack. This way, your widget inside can position themselves anywhere they want.
Stack(
alignment:Alignment.center,
children:[
Text("K"),
Align(
alignment:Alignment.bottomCenter,
child:Text("hello"),
),
]
),
Take a look at the stack widget here: https://www.youtube.com/watch?v=liEGSeD3Zt8
return Container(
color: Colors.blue,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Spacer(),
Text(
'K',
style: TextStyle(
fontSize: 200,
color: Colors.white,
decoration: TextDecoration.none,
fontStyle: FontStyle.italic,
),
),
Spacer(),
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [Text('hello')],
)
],
),
);
how about this way? add two Spacer
In the code below, textAlign property doesn't work. If you remove DefaultTextStyle wrapper which is several levels above, textAlign starts to work.
Why and how to ensure it is always working?
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new DefaultTextStyle(style: new TextStyle(fontSize: 10.0), child: new Column(children: <Widget>[
new Text("Should be left", textAlign: TextAlign.left,),
new Text("Should be right", textAlign: TextAlign.right,)
],))
);
}
}
Both approaches, suggested by Remi apparently don't work "in the wild". Here is an example I nested both inside rows and columns. First approach doesn't do align and second approach makes application just crash:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return new Directionality(textDirection: TextDirection.ltr, child: new DefaultTextStyle(
style: new TextStyle(fontSize: 10.0, color: Colors.white),
child: new Column(children: <Widget>[
new Row(children: <Widget>[
new Container(color: Colors.grey, child: new Column(children: <Widget>[
new Align(alignment: Alignment.centerLeft, child: new Text("left")),
new Align(alignment: Alignment.centerRight, child: new Text("right")),
],)),
new Container(color: Colors.grey, child: new Column(children: <Widget>[
new Align(alignment: Alignment.centerLeft, child: new Text("left")),
new Align(alignment: Alignment.centerRight, child: new Text("right")),
],)),
],),
/*new Row(children: <Widget>[
new Container(color: Colors.grey, child: new Column(children: <Widget>[
new SizedBox(width: double.infinity, child: new Text("left", textAlign: TextAlign.left,)),
new SizedBox(width: double.infinity, child: new Text("right", textAlign: TextAlign.right)),
],)),
new Container(color: Colors.grey, child: new Column(children: <Widget>[
new SizedBox(width: double.infinity, child: new Text("left", textAlign: TextAlign.left)),
new SizedBox(width: double.infinity, child: new Text("right", textAlign: TextAlign.right)),
],)),
],)*/]
)));
}
}
What I get from code is
i.e. text is centered, ignoring alignment of Align element.
DefaultTextStyle is unrelated to the problem. Removing it simply uses the default style, which is far bigger than the one you used so it hides the problem.
textAlign aligns the text in the space occupied by Text when that occupied space is bigger than the actual content.
The thing is, inside a Column, your Text takes the bare minimum space. It is then the Column that aligns its children using crossAxisAlignment which defaults to center.
An easy way to catch such behavior is by wrapping your texts like this :
Container(
color: Colors.red,
child: Text(...)
)
Which using the code you provided, render the following :
The problem suddenly becomes obvious: Text don't take the whole Column width.
You now have a few solutions.
You can wrap your Text into an Align to mimic textAlign behavior
Column(
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Container(
color: Colors.red,
child: Text(
"Should be left",
),
),
),
],
)
Which will render the following :
or you can force your Text to fill the Column width.
Either by specifying crossAxisAlignment: CrossAxisAlignment.stretch on Column, or by using SizedBox with an infinite width.
Column(
children: <Widget>[
SizedBox(
width: double.infinity,
child: Container(
color: Colors.red,
child: Text(
"Should be left",
textAlign: TextAlign.left,
),
),
),
],
),
which renders the following:
In that example, it is TextAlign that placed the text to the left.
Specify crossAxisAlignment: CrossAxisAlignment.start in your column
In Colum widget Text alignment will be centred automatically, so use crossAxisAlignment: CrossAxisAlignment.start to align start.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(""),
Text(""),
]);
textAlign property only works when there is a more space left for the Text's content. Below are 2 examples which shows when textAlign has impact and when not.
No impact
For instance, in this example, it won't have any impact because there is no extra space for the content of the Text.
Text(
"Hello",
textAlign: TextAlign.end, // no impact
),
Has impact
If you wrap it in a Container and provide extra width such that it has more extra space.
Container(
width: 200,
color: Colors.orange,
child: Text(
"Hello",
textAlign: TextAlign.end, // has impact
),
)
Set alignment: Alignment.centerRight in Container:
Container(
alignment: Alignment.centerRight,
child:Text(
"Hello",
),
)
You can use the container, It will help you to set the alignment.
Widget _buildListWidget({Map reminder}) {
return Container(
color: Colors.amber,
alignment: Alignment.centerLeft,
padding: EdgeInsets.all(20),
height: 80,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
child: Text(
reminder['title'],
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 16,
color: Colors.black,
backgroundColor: Colors.blue,
fontWeight: FontWeight.normal,
),
),
),
Container(
alignment: Alignment.centerRight,
child: Text(
reminder['Date'],
textAlign: TextAlign.right,
style: TextStyle(
fontSize: 12,
color: Colors.grey,
backgroundColor: Colors.blue,
fontWeight: FontWeight.normal,
),
),
),
],
),
);
}
For maximum flexibility, I usually prefer working with SizedBox like this:
Row(
children: <Widget>[
SizedBox(
width: 235,
child: Text('Hey, ')),
SizedBox(
width: 110,
child: Text('how are'),
SizedBox(
width: 10,
child: Text('you?'))
],
)
I've experienced problems with text alignment when using alignment in the past, whereas sizedbox always does the work.
You can align text anywhere in the scaffold or container except center:-
Its works for me anywhere in my application:-
new Text(
"Nextperience",
//i have setted in center.
textAlign: TextAlign.center,
//when i want it left.
//textAlign: TextAlign.left,
//when i want it right.
//textAlign: TextAlign.right,
style: TextStyle(
fontSize: 16,
color: Colors.blue[900],
fontWeight: FontWeight.w500),
),
Text(' Use textAlign: TextAlign.center',
style: TextStyle(fontWeight: FontWeight.w900,fontSize: 20,),
textAlign: TextAlign.center,)
Always use textAlign When trying to direct you text