Dart 如何在appbar的标题下对齐潜台词?
我在Flitter中有下面的应用程序栏,并试图将标题“enter”下的潜台词“Your desired business name”对齐。我尝试了不同的方法,但仍然无法对齐。它应该在顶部有“回车”,然后在中间对齐子文本 这是我目前的代码Dart 如何在appbar的标题下对齐潜台词?,dart,flutter,Dart,Flutter,我在Flitter中有下面的应用程序栏,并试图将标题“enter”下的潜台词“Your desired business name”对齐。我尝试了不同的方法,但仍然无法对齐。它应该在顶部有“回车”,然后在中间对齐子文本 这是我目前的代码 Widget build(BuildContext context) { return Scaffold( appBar: PreferredSize( preferredSize: Size.fromHeight(75
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(75.0),
child: AppBar(
centerTitle: true,
title: Text('enter'),
actions: <Widget>[
new Container(),
new Center(
child: Text(
'Your desired business name',
textAlign: TextAlign.center,
style: new TextStyle(
fontSize: 14.0,
color: Colors.white,
),
)),
],
)),
);
}
小部件构建(构建上下文){
返回脚手架(
appBar:首选大小(
首选尺寸:从高度开始的尺寸(75.0),
孩子:AppBar(
标题:对,
标题:文本('enter'),
行动:[
新容器(),
新中心(
子:文本(
“您想要的企业名称”,
textAlign:textAlign.center,
样式:新文本样式(
字体大小:14.0,
颜色:颜色,白色,
),
)),
],
)),
);
}
菜单栏操作小部件在右侧对齐,据我所知,无法获得您设计的布局
是否应考虑具有标题和字幕的基于列的小部件:
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
centerTitle: true,
title: Column(children: [
Text(
"Title",
),
GestureDetector(
child: Text('subtitle'),
onTap: () {
print("tapped subtitle");
},
)
]),
...
在本例中,有一个
手势检测器
,用于为字幕提供交互性,因为这似乎是您正在尝试做的。要对齐AppBar
标题下的文本,您可以使用AppBar的bottom
属性,该属性将PreferredSize
小部件作为参数,还可以帮助您根据需要调整AppBar的高度
这是密码
AppBar(
title: Text('Title 1'),
centerTitle: true,
bottom: PreferredSize(
child: Text("Title 2"),
preferredSize: null),
)
您应该添加列视图
title: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Title",
style: TextStyle(color: Colors.white, fontSize: 16.0),
),
Text(
"Sub Title",
style: TextStyle(color: Colors.white, fontSize: 14.0),
),
]
),
我使用此代码添加字幕和显示用户图标 //
name、status和userPic是变量的
@override
Widget build(BuildContext context)
{
return new Scaffold(
appBar: new AppBar(
title: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
style: TextStyle(color: Colors.white, fontSize: 16.0),
),
Text(
status,
style: TextStyle(color: Colors.white, fontSize: 14.0),
)
],
),
leading: new Padding(
padding: const EdgeInsets.all(5.0),
child: new CircleAvatar(
backgroundImage: new NetworkImage(userPicUrl),
),
),
actions: <Widget>[new Icon(Icons.more_vert)],
),
);
}
@覆盖
小部件构建(构建上下文)
{
归还新脚手架(
appBar:新的appBar(
标题:专栏(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
正文(
名称
样式:TextStyle(颜色:Colors.white,fontSize:16.0),
),
正文(
地位
样式:TextStyle(颜色:Colors.white,fontSize:14.0),
)
],
),
领先:新的填充(
填充:常数边集全部(5.0),
孩子:新CircleAvatar(
背景图片:新的网络图片(userPicUrl),
),
),
操作:[新图标(图标。更多内容)],
),
);
}
输出
答案有效,但在Dart中使用空安全着陆时,不允许将首选大小
设置为空
因此,解决方案是将preferredSize
设置为零或任何其他有效数字
appBar:appBar(
标题:文本(“标题1”),
标题:对,
底部:首选尺寸(
子:文本(“标题2”),
preferredSize:Size.fromHeight(0),
),
)
在这些情况下,我更喜欢使用ListTile:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:
AppBar(
title:
ListTile(
title:
Text("Enter", style: TextStyle(color:Colors.white)),
subtitle:
Text("Your desired business name", style: TextStyle(color:Colors.white)),
)
),
);
}
如果您需要居中的文本小部件,只需将其包装在一个居中小部件中。这是最有效的方法,但我删除了该手势检测器,因为我不需要它。ThanksIt离底部太近了。感谢您提供此代码段,它可能会提供一些有限的、即时的帮助。通过展示为什么这是一个很好的问题解决方案,A将极大地提高它的长期价值,并将使它对未来有其他类似问题的读者更有用。请在您的答案中添加一些解释,包括您所做的假设。希望它能解决问题,但请添加对您的代码的解释,以便用户能够完全理解他/她真正想要的内容。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:
AppBar(
title:
ListTile(
title:
Text("Enter", style: TextStyle(color:Colors.white)),
subtitle:
Text("Your desired business name", style: TextStyle(color:Colors.white)),
)
),
);
}