flutterflutter-appbar

How to center a column in the appbar


how do I Center a column in the Appbar? I've searched online and even after adding a row with mainAxisAlignment and mainAxisSize, the Column is still not centered. TIA for all inputs. Additionally, the page is a second page, hence, there is a leading icon.

enter image description here

return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Container(
          color: Colors.deepPurple,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: Text(widget.id),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(vertical: 8.0),
                    child: Text(
                      _caption,
                      style: const TextStyle(fontSize: 14),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

Solution

  • You code works as intended:

    Look how your widget appears: it correctly centers the column inside the AppBar.

    enter image description here

    And here's a little example that centers a column within app bar, it's all about setting centerTitle: true,

    appBar: AppBar(
      backgroundColor: Colors.blue,
      centerTitle: true,
      title: Column(
        children: [
          Text('First Text'),
          Text('Second Text'),
          
        ],
      ),
    

    How it looks:

    enter image description here

    If you are expecting a different output, just let me know.


    Updates

    Now, The leading widget is in the center calculations, we must ignore it from calculation, which can be done by setting a similar widget that allocate the same width as leading in actions of the appbar:

      appBar: AppBar(
        title: Container(
          color: Colors.amber,
          child: Column(
            children: [
              Text('Centered Centered'),Text('Centered'),
            ],
          ),
        ),
        centerTitle: true,
        leadingWidth: 60,
        leading: GestureDetector(
          child: Icon(Icons.arrow_back),
        ) ,
        actions: [
          SizedBox(width: 60,),
        ],
      ),
    

    which appears like this, it's totally centered:

    enter image description here

    Hope it helps you.