Flutter align IconButton content to parent boundaries

450 views Asked by At

Is it possible to get IconButton content (icon) aligned to parent widget boundary?

Got code like this

Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            IconButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                splashRadius: 20,
                constraints: BoxConstraints(),
                padding: EdgeInsets.zero,
                icon: SvgPicture.asset('assets/icons/arrow_left.svg', color: primaryColor)
            )
          ],
        )
    );

I want to keep Icon aligned to a Row widget left boundary and keep on-click splash animation around that positioning.

With this parameter it is possible to position Icon to the place where I need it, but on-click splash animation remains unaligned.

alignment: Alignment.centerLeft

So it does something like this (vertical markup line is parent widget boundary)

enter image description here

But I need this

enter image description here

2

There are 2 answers

1
Nagaraj Alagusundaram On

I believe this is something to be done with the resource than the code.

       Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    IconButton(
                      constraints: BoxConstraints(),
                      padding: EdgeInsets.zero,
                      icon: Icon(Icons.arrow_back, size: 20),
                      splashRadius: 20,
                      onPressed: () => {},
                    ),
                  ],
                )
            )

I used your code, added size to it, and used the inbuilt resource.

icon: Icon(Icons.arrow_back, size: 20),

The output is

enter image description here

1
Salim Murshed On

You can use it in simple way like below.

     Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                  color: Colors.black12,
                  borderRadius: BorderRadius.circular(50)),
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: IconButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    constraints: BoxConstraints(),
                    padding: EdgeInsets.zero,
                    icon: SvgPicture.asset('assets/icons/arrow_left.svg',
                        color: primaryColor)),
              ),
            )
          ],
        ))