【发布时间】:2021-08-07 22:17:12
【问题描述】:
我对 Flutter 很陌生,所以请多多包涵。我想知道我是否可以在build 方法中检索和使用小部件的宽度。下面的 RoundButton 小部件说明了具有宽度可能有用的两种情况:
- 将宽度显示为文本
- 要创建一个圆形容器,我可以使用
radius=width/2(注意:我知道这可以使用shape: BoxShape.circle来完成,所以这只是一个可以使用宽度的示例)
所以我有两个问题:
- 是否可以获取小部件的宽度
- 如果是这样,您能否概述一下如何做到这一点?
class RoundButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: Colors.white),
child: Container (
decoration: BoxDecoration(
border: Border.all(width: 6, color: Colors.green),
borderRadius: const BorderRadius.all(const Radius.circular(**width**/2)),
//shape: BoxShape.circle,
),
child: Center (
child: Text(
"w="+**width**.toString(),
),
)
));
}
}
【问题讨论】:
-
这能回答你的问题吗? How to get height of a Widget?