如何在颤振中垂直居中容器子项

bih*_*ris 5 flutter flutter-appbar

我正在尝试将容器 appBar 的子项(即文本)垂直居中,我是 flutter 新手,所以我在这里缺少什么。它只是水平居中

小部件

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);
}
Run Code Online (Sandbox Code Playgroud)

我在哪里称呼它

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

        );
Run Code Online (Sandbox Code Playgroud)

Dee*_*yan 11

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}'),
          ]))
Run Code Online (Sandbox Code Playgroud)


fee*_*nda 5

实际上,您自定义的 AppBar 中的文本是垂直居中的。

在此输入图像描述

我认为你想要达到的效果是排除状态栏。

在此输入图像描述

如果这是你想要的,只需用 SafeArea 包裹 Center Widget 即可。

文档在这里:SafeArea

SafeArea(child: Center(child: title))
Run Code Online (Sandbox Code Playgroud)


Bil*_*san -1

使用AppBar带有参数的小部件centerTitle: true,

AppBar(
  centerTitle: true, // this is all you need
  ...
)
Run Code Online (Sandbox Code Playgroud)