gpt4 book ai didi

flutter - flutter 文本的宽度和高度与父项匹配

转载 作者:行者123 更新时间:2023-12-03 04:19:44 32 4
gpt4 key购买 nike

我想实现设置文本的宽度和高度的父大小。这是我尝试过的但无法正常工作:

Container(
height: double.infinity,
child: Padding(
padding: const EdgeInsets.only(left: 30, right: 30),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
GestureDetector(
onTap: () {
_onPagerTabItemClicked(0);
},
child: Container(
width: double.infinity,
height: double.infinity,
child: Text("Günlük",
style: TextStyle(color: getTextColor(0))),
)),
GestureDetector(
onTap: () {
_onPagerTabItemClicked(0);
},
child: Container(
width: double.infinity,
height: double.infinity,
child: Text("Günlük",
style: TextStyle(color: getTextColor(0))),
)),
GestureDetector(
onTap: () {
_onPagerTabItemClicked(0);
},
child: Container(
width: double.infinity,
height: double.infinity,
child: Text("Günlük",
style: TextStyle(color: getTextColor(0))),
)),
],
),
),
),
在我使用上面的代码之后,文本的宽度和高度仍然是wrap_content。(我通过给文本赋予backgroundcolor进行了调查)。如何实现呢?

ps :in the code block that i share the container width and height isinfinite so if i try to surround it with SizedBox.expand its not working.

最佳答案

您可以通过将Text小部件包装在FittedBox中并用fit: BoxFit.fill来实现,以使其占用所有可用空间。

Container(
height: double.infinity,
child: Padding(
padding: const EdgeInsets.only(left: 30, right: 30),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
buildItem(Colors.red),
buildItem(Colors.green),
buildItem(Colors.blue),
],
),
),
);

Widget buildItem(Color color) {
return Expanded(
child: Container(
height: double.infinity,
color: color,
child: FittedBox(
fit: BoxFit.fill,
alignment: Alignment.center,
child: GestureDetector(
onTap: (){},
child: Text(
"Günlük",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
),
),
),
),
),
);
Screenshot

关于flutter - flutter 文本的宽度和高度与父项匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63403113/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com