How to center vertically a container child in flutter
Asked Answered
T

4

7

I am trying to center vertically a child of a container appBar which is text, I am new to flutter so what am I missing here. It is only being centered horizontally

The widget

import 'package:flutter/material.dart';

class MyAppbar extends StatelessWidget implements PreferredSizeWidget {
  final Widget title;

  const MyAppbar({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      elevation: 0.0,
      color: Colors.white,
      child: Container(
        padding: const EdgeInsets.all(0.0),
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: Colors.red,
          border: Border(
            bottom: BorderSide(
              color: Color(0xffd9d9d9),
              width: .8,
              style: BorderStyle.solid,
            ),
          ),
        ),

        child: Center(child: title),
      ),
    );
  }

  final Size preferredSize = const Size.fromHeight(kToolbarHeight);
}

Where I am calling it

Scaffold(
          appBar: MyAppbar(title: Text('Welcome to Ikaze', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w400, color: Colors.black))),
          body: Center(),

        );
Turnspit answered 4/6, 2020 at 9:13 Comment(4)
Can you upload the full code?Croy
Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[])Dispersant
That was the fullCode, I just uploaded where I was calling it @BilaalAbdelHassanTurnspit
@bihireboris Use the AppBar widget to customize your appbar. It has an argument which centers the text. See my answer below.Croy
D
13
Scaffold(
      appBar: AppBar(
        title: Text('Welcome to Ikaze',
            style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w400,
                color: Colors.black)),
      ),
      body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
                'location: ${userLocation.latitude}, ${userLocation.longitude}'),
          ]))
Dispersant answered 4/6, 2020 at 9:21 Comment(1)
I am trying to center the appBar Text not the bodyTurnspit
C
5

actually the text in your customized AppBar is vertically centered.

enter image description here

I think the effect you wanna achieve is to exclude status bar.

enter image description here

if this is what you want, just wrap Center Widget with SafeArea.

documentation here: SafeArea

SafeArea(child: Center(child: title))
Caligula answered 4/6, 2020 at 11:2 Comment(1)
Thanks a lot for the try, I had found that issue check the answer aboveTurnspit
T
1

so found the issue It was actually centered however since it wasn't in the safeArea widget the containing widget contained that system status bar.

fix

return Material(
      elevation: 0.0,
      color: Colors.white,
      child: Container(
        padding: const EdgeInsets.all(0.0),
        alignment: Alignment.center,
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Color(0xffd9d9d9),
              width: .8,
              style: BorderStyle.solid,
            ),
          ),
        ),

        child: SafeArea(
          child: Container(
            alignment: Alignment.center,
            child: title,
          )),
      ),
    );
Turnspit answered 4/6, 2020 at 10:2 Comment(1)
:) grate, ohh That was the issue.Dispersant
C
-1

Use the AppBar widget with the argument centerTitle: true,

AppBar(
  centerTitle: true, // this is all you need
  ...
)
Croy answered 4/6, 2020 at 9:21 Comment(1)
I wanted to customize that appbar heavy afterward that is why I tried creating a new widgetTurnspit

© 2022 - 2024 — McMap. All rights reserved.