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(...),
),
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,
);
}
}
The content inside the column didnt go center,i dont want to use center widget all i want is to make the widget list go center
class Body extends StatelessWidget {
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
// == size provide height and width from screen
return Background(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Welcome to my App",
style: TextStyle(fontWeight: FontWeight.bold, color: kPrimaryColor,),
),
SvgPicture.asset(
"assets/icons/chat.svg",
height: size.height * 0.45,
),
FlatButton(
onPressed: (){},
child: Text("LOGIN", style: TextStyle(color: Colors.white),),
color: kPrimaryColor,
padding: EdgeInsets.symmetric(vertical: 20,horizontal: 40),
)
],
),
);
}
}
This will help you.
Column(
mainAxisAlignment:MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('text'),],
)
Or try with a list view
Scaffold(
appBar: new AppBar(),
body: Center(
child: new ListView(
shrinkWrap: true,
padding: const EdgeInsets.all(20.0),
children: [
Center(child: new Text('your content'))
]
),
),
);
Or you can use the Align widget
return Scaffold(
body: ListView( // your ListView
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: _button("Left"),
),
Align(
alignment: Alignment.center,
child: _button("Middle"),
),
Align(
alignment: Alignment.centerRight,
child: _button("Right"),
),
],
),
);
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.
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: CircleAvatar(
radius: 40.0,
child: Image.asset('images/pic.jpg'),
),
),
Text(
"michael dub",
),
Text("FLUTTER DEVELOPER"),
Container(
width: 350.0,
height: 70.0,
child: Card(
child: Row(
children: <Widget>[
Icon(
Icons.phone,
),
Text(
"+12-345-678-910",
textAlign: TextAlign.center,
),
],
)))
],
),
),
);
}
}
in your column, try crossAxisAlignment: CrossAxisAlignment.stretch
EDIT:
if you're trying to do what I think you're trying to do, try adding
mainAxisAlignment: MainAxisAlignment.center
to your row
you need to wrap your text widget with Expanded widget. Default Row Widget will not take full width and it is creating issue.
Check out following code.
Expanded(
child: Text(
"+12-345-678-910",
textAlign: TextAlign.center,
),
),
I'm getting the problem when I add Scrolling functionality in Scaffold, here I have tried SingleChildScrollView, CustomerScrollView, and ListView but it freezes the UI but when I replaced ti Scaffold to LayoutBuilder it works but all the background my UI got black.
I just want to add scrolling functionally to Scaffold.
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
...Some Code...
},
child: Scaffold(
body: Column(
children: <Widget>[
Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.all(15.0),
alignment: Alignment.bottomLeft,
child: Text(
mydata[0][i.toString()],
style: TextStyle(fontSize: 16.0, fontFamily: "Quando"),
),
)),
Expanded(
flex: 6,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
choiceButton('a'),
choiceButton('b'),
choiceButton('c'),
choiceButton('d'),
],
),
)),
Expanded(
flex: 1,
child: Container(
alignment: Alignment.topCenter,
child: Text(showtimer,
style: TextStyle(
fontSize: 35.0,
fontFamily: "Times New Roman",
fontWeight: FontWeight.w700)),
))
],
),
) // Scaffold
);// WillPopScope
}
Try This
return Scaffold(
resizeToAvoidBottomInset: false,
extendBodyBehindAppBar: false,
backgroundColor: Colors.white,
body: Column(
children: <Widget>[
Expanded(
child: SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
],
),
),
),
),
]
Don't use Expanded inside any scrolling container unless you wrap it in a fixed bound on its main axis.
Like in your case main axis is vertical and you are using expanded in it, since expanded needs a bound to limit its expansion, you should've remove that expanded thing or just wrap the container in a SizedBox for a specific height.
Than you can wrap the sized box in any scroll container which suits you.
Hope it helps!
In your code, change Column to ListView, and remove the expanded widget. you can use fixed height or MediaQuery.of(context).size.height * (any ratio) to the containers, or just don't specify any container height so it'll take its children's height.
here's the code will look like:
body: ListView(
shrinkWrap: true,
children: <Widget>[
Container(
// height: MediaQuery.of(context).size.height * (any ratio) / fixed height
child: Text(
mydata[0][i.toString()],
style: TextStyle(fontSize: 16.0, fontFamily: "Quando"),
),
),
Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
choiceButton('a'),
choiceButton('b'),
choiceButton('c'),
choiceButton('d'),
],
),
),
Container(
alignment: Alignment.topCenter,
child: Text(showtimer,
style: TextStyle(
fontSize: 35.0,
fontFamily: "Times New Roman",
fontWeight: FontWeight.w700)),
)
],
),