Search code examples
flutterstylesalignmentrowiconbutton

Flutter : Make IconButtons with different Icon sizes and Text elements in Row align to the center


I have this Row widget in Flutter App with some IconButtons

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    IconButton(
      icon: Icon(Icons.skip_previous,
        color: Colors.amber, size: 35),
      onPressed: () {
        setState(() {
          pageIndex = 1;
        });
      }),
    IconButton(
      icon: Icon(Icons.arrow_left,
        color: Colors.amber, size: 45),
      onPressed: decIndex),
    Text('Page $pageIndex',
      textAlign: TextAlign.center,
      style: TextStyle(
        color: Colors.amber,
        fontSize: 20,
        fontWeight: FontWeight.bold)),
    IconButton(
      icon: Icon(Icons.arrow_right,
        color: Colors.amber, size: 45),
      onPressed: () {
        incIndex(pageNumbers);
      }),
    IconButton(
      icon: Icon(Icons.skip_next,
        color: Colors.amber, size: 35),
      onPressed: () {
        setState(() {
          pageIndex = pageNumbers;
        });
      }),
    IconButton(
      icon: Icon(Icons.location_searching,
        color: Colors.amber, size: 35),
      onPressed: () {
        setState(() {
          pageIndex = userPage;
        });
      }),
  ],
),

they display as shown in this image:

the red line is just to be clear the difference between elevations

I want make all items aligned on the same line through their center. How can I do that?

enter image description here


Solution

  • Using size parameter on the Icon is not a very good approach for IconButton widgets. You icon are becoming big and the IconButtons are not adapting to that expanded size, which is causing the icon to overflow.

    Instead, use the iconSize parameter on the IconButton and give the same value you were giving to the Icon and remove it from the Icon.

    Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        IconButton(
          iconSize: 35,
          icon: Icon(Icons.skip_previous, color: Colors.amber),
          onPressed: () {
            setState(() {
              pageIndex = 1;
            });
          }
        ),
        IconButton(
          iconSize: 45,
          icon: Icon(Icons.arrow_left, color: Colors.amber),   
          onPressed: decIndex
        ),
        Text('Page $pageIndex',
          textAlign: TextAlign.center,
          style: TextStyle(
            color: Colors.amber,
            fontSize: 20,
            fontWeight: FontWeight.bold)),
        IconButton(
          iconSize: 45,
          icon: Icon(Icons.arrow_right, color: Colors.amber),
          onPressed: () {
            incIndex(pageNumbers);
          }),
        IconButton(
          iconSize: 35,
          icon: Icon(Icons.skip_next, color: Colors.amber),
          onPressed: () {
            setState(() {
              pageIndex = pageNumbers;
            });
          }),
        IconButton(
          iconSize: 35,
          icon: Icon(Icons.location_searching, color: Colors.amber),
          onPressed: () {
            setState(() {
              pageIndex = userPage;
            });
          }
        )
      ],
    ),
    

    enter image description here