I'm trying to build a simple login UI in Flutter, like this:
Currently, my design looks like this:
Here's my code:
class LogInEnterEmailPassword extends StatefulWidget {
const LogInEnterEmailPassword({Key? key}) : super(key: key);
@override
State<LogInEnterEmailPassword> createState() => _LogInEnterEmailPasswordState();
}
class _LogInEnterEmailPasswordState extends State<LogInEnterEmailPassword> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xffF6F6F6),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text("Enter Email",
style: TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
color: Color(0xff313640),
),),
Text("______________________________",
style: TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
color: Color(0xff313640),
),),
Text("Enter Password",
style: TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
color: Color(0xff313640),
),),
Text("______________________________",
style: TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
color: Color(0xff313640),
),),
],
)
)
);
}
}
I've tried using SizedBox
, but it leaves too much space. How can I achieve a more compact layout without sacrificing the overall structure?
Could you try to change MainAxisAlignment from spaceEvenly to center? You could see the difference between them.
Actually, I fully use SizeBox to separate them with specific spaces.
mainAxisAlignment: MainAxisAlignment.spaceEvenly
mainAxisAlignment: MainAxisAlignment.center
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Flexible(
child: Hero(
tag: 'logo',
child: Container(
height: 200.0,
child: Image.asset('images/logo.png'),
),
),
),
SizedBox(
height: 48.0,
),
TextField(
keyboardType: TextInputType.emailAddress,
textAlign: TextAlign.center,
onChanged: (value) {
email = value;
//Do something with the user input.
}, decoration: kTextFieldDecoration.copyWith(hintText: 'Eenter your email')),
SizedBox(
height: 8.0,
),
TextField(
obscureText: true,
textAlign: TextAlign.center,
onChanged: (value) {
password = value;
//Do something with the user input.
},
decoration: kTextFieldDecoration.copyWith(hintText: 'Eenter your password')),
SizedBox(
height: 24.0,
),
RoundButton(
title: 'Register',
colour: Colors.blueAccent,
onPressed: () async{
print(email);
print(password);
print('Register');
//Go to login screen.
// Navigator.pushNamed(context, RegistrationScreen.id);
try{
final newUser = await _auth.createUserWithEmailAndPassword(email: email, password: password);
if (newUser != null) {
Navigator.pushNamed(context, ChatScreen.id);
}
}
catch(e) {
print(e);
}
},
),
],
),
),
);