I am trying to make all my widgets in a column widget to vertically align at the center of the page but after applying MainAxisAlignment and CrossAxisAlignment it still align top. What could be the reason for this. I expected the mainaxis alignment to fix this.
Here is my code:
body: ListView(
children: [
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
// Welcome back
const SizedBox(height: 10),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0),
child: Text(
'Welcome back, you've been missed!',
style: TextStyle(
fontSize: 20,
),
),
),
const SizedBox(height: 30),
//email textfield
MyTextField(
controller: emailController,
hintText: 'Email',
obscureText: false,
),
const SizedBox(height: 10),
//Password textfield
MyTextField(
controller: passwordController,
hintText: 'Password',
obscureText: true,
),
const SizedBox(height: 10),
//signin button
MyButton(
onTap: loginUser,
butlabel: 'Log In',
),
],),
),
],
),
2
Answers
You just need to wrap your Column with an intrinsic height widget that has the same height of the screen:
Note: after displaying your
TextField
s, if it’s too height a bottom overflow may happen. You need to know that ‘alignment doesn’t work within the scroll views’.So, i would suggest to use
SizedBox
s to align your Design within the column rather than alignment that’s if an overflow occurred.