ccc
ccc

Reputation: 2385

Aligning ListTile items when some of them don't have a leading object

Here's what I have on the screen:

https://i.sstatic.net/brN5q.png

How can I align Last name so it's under First name ? I don't want to put an icon next to Last name, just First name.

Here's the code I have. Thanks.

            body: Center(
            child: Form(
                key: _formKey,
                child: Column(
                    children: <Widget>[
                        ListTile(
                            leading: const Icon(Icons.person),
                            title: new TextField(
                                decoration: new InputDecoration(
                                    hintText: "First name",
                                ),
                            ),
                        ),
                        ListTile(
                            title: new TextField(
                                decoration: new InputDecoration(
                                    hintText: "Last name",
                                ),
                            ),
                        ),
                    ],
                ),
            )
        )

Upvotes: 1

Views: 284

Answers (4)

Elihu Del Valle
Elihu Del Valle

Reputation: 406

What I've done is giving it an Icon with size 0, the ListTile will save the space even if there is nothing visible

Upvotes: 0

anmol.majhail
anmol.majhail

Reputation: 51286

Simple & Elegant Way would be to use Padding Widget.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: Form(
        //  key: _formKey,
        child: Column(
          children: <Widget>[
            ListTile(
              leading: const Icon(Icons.person),
              title: new TextField(
                decoration: new InputDecoration(
                  hintText: "First name",
                ),
              ),
            ),
            ListTile(
              leading: Padding(padding: EdgeInsets.all(16.0)),
              title: new TextField(
                decoration: new InputDecoration(
                  hintText: "Last name",
                ),
              ),
            ),
          ],
        ),
      )),
    );
  }

Upvotes: 2

Xuzan
Xuzan

Reputation: 335

There's a quick hack by changing icon color to colors.transparent. I don't know if it's proper way though.

     body: Center(
        child: Form(
            key: _formKey,
            child: Column(
                children: <Widget>[
                    ListTile(
                        leading: const Icon(Icons.person),
                        title: new TextField(
                            decoration: new InputDecoration(
                                hintText: "First name",
                            ),
                        ),
                    ),
                    ListTile(
                        leading: const Icon(Icons.person,color: Colors.transparent),
                        title: new TextField(
                            decoration: new InputDecoration(
                                hintText: "Last name",
                            ),
                        ),
                    ),
                ],
            ),
        )
    )

Upvotes: 1

bytesizedwizard
bytesizedwizard

Reputation: 6033

You can add an empty Container with fixed width as leading to the Last Name TextField, or you can wrap your Last Name TextField inside Padding and provide a some padding from left.

Example:

ListTile(
  leading: Container(
    width: 200.0, //You can adjust the width as required
  ),
  title: new TextField(
    decoration: new InputDecoration(
      hintText: "Last name",
    ),
  ),
),

OR

ListTile(
  title: Padding(
    padding: padding: EdgeInsets.only(left: 200.0) //Adjust as required
    child: new TextField(
      decoration: new InputDecoration(
        hintText: "Last name",
      ),
    ),
  ),
),

Upvotes: 1

Related Questions