Dart 如何将颤振文本与行中的其他小部件居中
我用BackButton和TextWidget创建了一行。 我想把文字放在屏幕中间。实际上,flatter将文本居中到容器宽度,但容器宽度与屏幕宽度不同,因为存在backbutton。我该如何解决这个问题Dart 如何将颤振文本与行中的其他小部件居中,dart,flutter,Dart,Flutter,我用BackButton和TextWidget创建了一行。 我想把文字放在屏幕中间。实际上,flatter将文本居中到容器宽度,但容器宽度与屏幕宽度不同,因为存在backbutton。我该如何解决这个问题 Expanded getTitle() { return new Expanded( child: new Text("Einloggen", style: new TextStyle(fontSize: 18.0), textAlign: TextAlign.ce
Expanded getTitle() {
return new Expanded(
child: new Text("Einloggen", style: new TextStyle(fontSize: 18.0), textAlign: TextAlign.center)
);
}
BackButton getBackButton() {
return new BackButton(
);
}
Row getHeader() {
return new Row(
children: <Widget>[
getBackButton(),
getTitle()
],
);
}
@override
Widget build(BuildContext context) {
final double statusBarHeight = MediaQuery.of(context).padding.top;
return new Material(
child: new Container(
padding: new EdgeInsets.fromLTRB(0.0, statusBarHeight, 0.0, 0.0),
child: new Column(
children: <Widget>[
getHeader()
],
),
),
);
}
Expanded getTitle(){
返回新的扩展(
子项:新文本(“Einloggen”,样式:新文本样式(fontSize:18.0),textAlign:textAlign.center)
);
}
BackButton getBackButton(){
返回新的BackButton(
);
}
行getHeader(){
返回新行(
儿童:[
getBackButton(),
getTitle()
],
);
}
@凌驾
小部件构建(构建上下文){
final double statusBarHeight=MediaQuery.of(context).padding.top;
退回新材料(
子容器:新容器(
填充:来自LTRB(0.0,statusBarHeight,0.0,0.0)的新边插入,
子:新列(
儿童:[
getHeader()
],
),
),
);
}
您可以通过使用 要使标题居中,请执行以下操作: 标题:正确
您可以使用
行
的mainAxisAlignment
参数将行的子行居中对齐
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//children Widgets
]
);
行(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
//子部件
]
);
类似地,
mainAxisAligment
参数也可用于对齐列的子项。有关更多信息,我不知道它是否仍然有用,但多亏了widegtes,我找到了一个解决方案:容器、堆栈和对齐
Widget getTitle() {
return new Text("Einloggen", style: new TextStyle(fontSize: 18.0));
}
Widget getBackButton() {
return Container(
height: MediaQuery.of(context).size.height,
child: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: moveToLogin,
));
}
Widget getHeader() {
return Container(
height: 50.0,
// you must set a size to the Conteiener to make sure that the internal Align
// widens as much as possible.
child: new Stack(
// Stack places the objects in the upper left corner
children: <Widget>[
getBackButton(),
Align(alignment: Alignment.center, child: getTitle()),
],
),
);
}
final double statusBarHeight = MediaQuery.of(context).padding.top;
return new Container(
padding: new EdgeInsets.fromLTRB(0.0, statusBarHeight, 0.0, 0.0),
child: new Column(
children: <Widget>[getHeader()],
),
);
Widget getTitle(){
返回新文本(“Einloggen”,样式:newTextStyle(fontSize:18.0));
}
小部件getBackButton(){
返回容器(
高度:MediaQuery.of(context).size.height,
孩子:我的钮扣(
图标:图标(图标。箭头返回),
onPressed:moveToLogin,
));
}
小部件getHeader(){
返回容器(
身高:50.0,
//您必须为上下文设置大小,以确保内部对齐
//越宽越好。
子:新堆栈(
//堆栈将对象放置在左上角
儿童:[
getBackButton(),
对齐(对齐:alignment.center,子项:getTitle()),
],
),
);
}
final double statusBarHeight=MediaQuery.of(context).padding.top;
退回新货柜(
填充:来自LTRB(0.0,statusBarHeight,0.0,0.0)的新边插入,
子:新列(
子项:[getHeader()],
),
);
这就是结果
基于您的代码
Widget getTitle() {
return const Text('Einloggen',
style: TextStyle(fontSize: 18.0), textAlign: TextAlign.center);
}
BackButton getBackButton() {
return const BackButton();
}
Row getHeader() {
return Row(
children: <Widget>[
Expanded(
child: getBackButton(),
),
const Spacer(),
getTitle(),
const Spacer(flex: 2)
],
);
}
@override
Widget build(BuildContext context) {
final double statusBarHeight = MediaQuery.of(context).padding.top;
return Material(
child: Container(
padding: EdgeInsets.fromLTRB(0.0, statusBarHeight, 0.0, 0.0),
child: Column(
children: <Widget>[getHeader()],
),
),
);
}
Widget getTitle(){
返回常量文本('Einloggen',
样式:TextStyle(fontSize:18.0),textAlign:textAlign.center);
}
BackButton getBackButton(){
return const BackButton();
}
行getHeader(){
返回行(
儿童:[
扩大(
子项:getBackButton(),
),
常量间隔符(),
getTitle(),
常数间隔棒(挠性:2)
],
);
}
@凌驾
小部件构建(构建上下文){
final double statusBarHeight=MediaQuery.of(context).padding.top;
退货(
子:容器(
填充:来自LTRB(0.0,statusBarHeight,0.0,0.0)的EdgeInsets,
子:列(
子项:[getHeader()],
),
),
);
}
你想从中得到什么?我想要一个BackButton和一行居中的文本,但文本没有居中。不确定,我看不到,我看到的图像如示例所示
Widget getTitle() {
return const Text('Einloggen',
style: TextStyle(fontSize: 18.0), textAlign: TextAlign.center);
}
BackButton getBackButton() {
return const BackButton();
}
Row getHeader() {
return Row(
children: <Widget>[
Expanded(
child: getBackButton(),
),
const Spacer(),
getTitle(),
const Spacer(flex: 2)
],
);
}
@override
Widget build(BuildContext context) {
final double statusBarHeight = MediaQuery.of(context).padding.top;
return Material(
child: Container(
padding: EdgeInsets.fromLTRB(0.0, statusBarHeight, 0.0, 0.0),
child: Column(
children: <Widget>[getHeader()],
),
),
);
}