我正在尝试将标题文字集中在一个既有前导也有尾随操作的应用栏中。
@override
Widget build(BuildContext context) {
final menuButton = new PopupMenuButton<int>(
onSelected: (int i) {},
itemBuilder: (BuildContext ctx) {},
child: new Icon(
Icons.dashboard,
),
);
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that
// was created by the App.build method, and use it to set
// our appbar title.
title: new Text(widget.title, textAlign: TextAlign.center),
leading: new IconButton(
icon: new Icon(Icons.accessibility),
onPressed: () {},
),
actions: [
menuButton,
],
),
body: new Center(
child: new Text(
'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
除了标题在左边对齐之外,这很有效,如下图所示:
当我尝试将标题包含在中心时,它似乎在左边太多了:
@override
Widget build(BuildContext context) {
final menuButton = new PopupMenuButton<int>(
onSelected: (int i) {},
itemBuilder: (BuildContext ctx) {},
child: new Icon(
Icons.dashboard,
),
);
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that
// was created by the App.build method, and use it to set
// our appbar title.
title: new Center(child: new Text(widget.title, textAlign: TextAlign.center)),
leading: new IconButton(
icon: new Icon(Icons.accessibility),
onPressed: () {},
),
actions: [
menuButton,
],
),
body: new Center(
child: new Text(
'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
我希望找到一个解决方案,让标题文字在2个图标之间完美居中。非常感谢,
答案 0 :(得分:73)
中心标题是iOS上的默认设置。在Android上,AppBar
的标题默认为左对齐,但您可以通过将centerTitle: true
作为参数传递给AppBar
构造函数来覆盖它。
答案 1 :(得分:5)
这是我在应用栏上设置centerTitle的方法:
` @override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
centerTitle: true ,
title: new Text("Login"),
),
body: new Container(
padding: EdgeInsets.all(18.0),
key: formkey,
child: ListView(
children: buildInputs() + buildSubmitButton(),
),
)
);
}`
答案 2 :(得分:2)
我遇到了同样的问题,当我添加
时,它终于可以工作了
mainAxisSize:MainAxisSize.min 到“我的行”窗口小部件。我希望这有帮助!
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that
// was created by the App.build method, and use it to set
// our appbar title.
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
widget.title,
),
],
),
leading: new IconButton(
icon: new Icon(Icons.accessibility),
onPressed: () {},
),
actions: [
menuButton,
],
),
);
}
答案 3 :(得分:1)
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
actions: <Widget> [
IconButton(icon: const Icon(Icons.file_upload), onPressed: _pressed),
],
leading: IconButton(icon: const Icon(Icons.list), onPressed: _pressed),
centerTitle: true,
)
body: Text("Content"),
);
}
答案 4 :(得分:1)
尝试了许多解决方案后,这对我lesson_views
有帮助
除了 @Collin Jackson 答案
示例
在const LessonView = use('App/Models/LessonView')
执行此操作
const lessonviewdata = await LessonView.findBy('user_id',1)
答案 5 :(得分:1)
就我而言,我希望徽标/图像居中而不是文本。在这种情况下,centerTitle
是不够的(不确定为什么,我有一个svg文件,也许就是这个原因...),例如:
appBar: AppBar(centerTitle: true, title: AppImages.logoSvg)
不会真正使图像居中(加上图像可能太大,等等)。对我来说很好的是这样的代码:
appBar: AppBar(centerTitle: true,
title: ConstrainedBox(
constraints: BoxConstraints(maxHeight: 35, maxWidth: 200),
child: AppImages.logoSvg)),
答案 6 :(得分:1)
appBar对于标题中心显示与否都有自己的布尔条件,
所以, 如果设置为真,
appBar: AppBar(
title: Text(
"header Text",
style: TextStyle(color: Colors.black),
),
centerTitle: true,
),
然后将其居中,然后将其默认左对齐(在android中) ios设置中心(默认)。
答案 7 :(得分:1)
appbar:AppBar( centerTitle:正确, 标题:文字(“ HELLO”) )
答案 8 :(得分:1)
您可以使用 centerTitle 参数使appBar的标题居中。
centerTitle 是布尔数据类型,默认值为False。
centerTitle : true
示例:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('App Title'),
backgroundColor: Colors.red,
centerTitle: true,
),
),
),
);
}
答案 9 :(得分:0)
您可以使用 appBar 部分中的 centerTitle 属性将标题居中
答案 10 :(得分:0)
是的,但在我的情况下,我使用了 centertitle 以及轴对齐,然后它使它居中,如果我只使用它,那么它并没有使它居中,这是我的做法:
import 'package:flutter/material.dart';
import 'package:infintywalls/widgets/widget.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
appName(),
],
),
elevation: 0.0,
centerTitle: true,
),
);
}
}
是的,顺便说一句,appName() 是我的自定义小部件,而不是默认的内置小部件。
home 这对你有帮助 谢谢
答案 11 :(得分:0)
在我的情况下,此代码有效:-
appBar: AppBar(
centerTitle: true,
elevation: 2,
title: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text(" TINKLE"),
)
],
),
),
),
希望这对您有所帮助。
答案 12 :(得分:0)
这有助于在Center中制作Appbar标题文本。 您可以选择使用“样式”添加所需的样式,或者在不需要时对其进行注释。
appBar: AppBar(
title: const Center(
child: Text(
"App Title",
style: TextStyle( color: Colors.white,fontSize: 20),
),
),
),
在应用程序显示上:
答案 13 :(得分:0)
使用Center
对象
appBar: AppBar(
title: Center(
child: const Text('Title Centered')
)
)
答案 14 :(得分:0)
如果要创建自定义应用栏标题,可以采用不同的方法。例如,您需要在应用栏中心添加图像和文本,然后添加 -
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.your_app_icon,
color: Colors.green[500],
),
Container(
padding: const EdgeInsets.all(8.0), child: Text('YourAppTitle'))
],
),
)
在这里,我们使用 MainAxisAlignment.center 创建了一个行,以使子项居中。然后我们添加了两个孩子 - 一个图标和一个带有文本的容器。我在Container中包装Text小部件以添加必要的填充。
希望这有帮助。
答案 15 :(得分:-1)
您可以将标题包装到Container中并将其对齐方式设置为居中
答案 16 :(得分:-4)
appBar: AppBar(
mainAxisAlignment: MainAxisAlignment.center,
)