我想将Container
的大小设置为动态,我的意思是wrap_content
抖动如何做?
在下面的代码Container
中,默认情况下采用全宽,但是我想将Container
中的宽度设置为Text()
,只要
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Container(
margin: EdgeInsets.all(10),
alignment: Alignment.center,
color: Colors.orange[800],
child: Text("Hello How are you"),
),
Container(
margin: EdgeInsets.all(10),
alignment: Alignment.center,
color: Colors.yellow[800],
child: Text("Welcome"),
)
],
);
我明白了:
但是我想要这个输出:
答案 0 :(得分:3)
用行
包裹您的容器 Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.all(10),
alignment: Alignment.center,
color: Colors.orange[800],
child: Text("Hello How are you"),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.all(10),
alignment: Alignment.center,
color: Colors.yellow[800],
child: Text("Welcome"),
),
],
)
],
),
答案 1 :(得分:2)
最简单的解决方案是使用Align
。
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Align(
child: Container(
margin: EdgeInsets.all(10),
color: Colors.orange[800],
child: Text("Hello How are you"),
),
),
Align(
child: Container(
margin: EdgeInsets.all(10),
color: Colors.yellow[800],
child: Text("Welcome"),
),
)
],
)
答案 2 :(得分:0)
最短的解决方案是,从alignment: Alignment.center,
移除Container
并将Column
包裹在Center
内,像这样,
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.all(10),
color: Colors.orange[800],
child: Text("Hello How are you"),
),
Container(
margin: EdgeInsets.all(10),
color: Colors.yellow[800],
child: Text("Welcome"),
)
],
),
)
输出
答案 3 :(得分:0)
包装容器内容的最佳方式不是为该小部件设置任何高度。