Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Centering a single character in Container using flutter

I am trying to create an add-button, where the icon inside is just a simple ‘+’-character, but for some reason it just sits at the bottom of the container instead of in the center

Troubling text in a container

image

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the structure if needed, where I have my Text widget inside a Center widget, and also the textAlign set to TextAlign.center.

Underneath I have provided all the code leading up to the Text widget

Column(
children: [
  ...,
  Expanded(
    child: Row(
      children: [
        Container(
          width: 80,
          alignment: Alignment.topCenter,
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
                child: Container(
                  width: 60,
                  height: 60,
                  decoration: const BoxDecoration(
                    color: Colors.amber,
                    borderRadius: BorderRadius.all(Radius.circular(24))
                  ),
                  child: const Center(
                    child: Text("+",
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 70,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        ...,
      ],
    ),
  )
],);

Note this is my first time using flutter.

>Solution :

So your font size is too big, and your box is too small to show that the "+" is in the centre of the box.

But it’s in the centre, if you use this values for your second container you’ll see it is in the center.

Column(
children: [
  ...,
  Expanded(
    child: Row(
      children: [
        Container(
          width: 80,
          alignment: Alignment.topCenter,
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
                child: Container(
                  width: 80,
                  height: 80,
                  decoration: const BoxDecoration(
                    color: Colors.amber,
                    borderRadius: BorderRadius.all(Radius.circular(24))
                  ),
                  child: const Center(
                    child: Text("+",
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 70,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        ...,
      ],
    ),
  )
],);

So to do this use an icon and you’ll have a better presentation you can see example here

Row(
        children: [
          Container(
            width: 80,
            alignment: Alignment.topCenter,
            child: Column(
              children: [
                Padding(
                  padding:
                      const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
                  child: Container(
                    width: 60,
                    height: 60,
                    decoration: const BoxDecoration(
                        color: Colors.amber,
                        borderRadius: BorderRadius.all(Radius.circular(24))),
                    child: const Center(
                      child: Icon(Icons.add, size: 35,)
                    ),
                  ),
                ),
              ],
            ),
          )
        ],
      ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading