0

Within the same body, I'm trying to align all my widgets at the center of the Screen for my loginpage.dart file. But the result I'm getting is where everything is at the top. I'm a beginner at flutter, and I don't know a lot of things. Can you please help me with the code so that all my widgets in the Listview are at the center?

My Code


 // ignore_for_file: prefer_const_constructors
    import 'package:flutter/material.dart';
    
    class LoginPage extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => _State();
    }
    
    class _State extends State<LoginPage> {
      TextEditingController nameController = TextEditingController();
      TextEditingController passwordController = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: Scaffold(
            body: Center(
              child: new ListView(
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 5.0),
                    child: Text(
                      'ABC',
                      style: TextStyle(
                          color: Colors.red,
                          fontWeight: FontWeight.w500,
                          fontSize: 30),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB(10, 0, 10, 10),
                    child: TextField(
                      controller: nameController,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'User Name',
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                    child: TextField(
                      obscureText: true,
                      controller: passwordController,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'Password',
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 8.0,
                    height: 8.0,
                    child: Card(child: Text('sheraspace')),
                  ),
                  Container(
                      height: 50,
                      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                      child: ElevatedButton(
                        child: Text('Submit'),
                        onPressed: () {
                          print(nameController.text);
                          print(passwordController.text);
                        },
                      )),
                ],
              ),
            ),
          ),
        );
      }
    }

1 Answer 1

2

To vertically center the elements inside the ListView Widget, we can use the shrinkWrap property:

ListView(
  shrinkWrap: true,
  children: [
    ...
  ],
),
3
  • Thank you very much. It worked! Oct 28, 2021 at 20:02
  • @MahdiHasan you're welcome! Don't forget to select the answer as correct Oct 28, 2021 at 20:37
  • Sure! Done that. Oct 28, 2021 at 21:44

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.