Home > front end >  How do I align all the widgets within list view at the center of the screen in Flutter?
How do I align all the widgets within list view at the center of the screen in Flutter?

Time:10-29

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);
                        },
                      )),
                ],
              ),
            ),
          ),
        );
      }
    }

CodePudding user response:

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

ListView(
  shrinkWrap: true,
  children: [
    ...
  ],
),
  • Related