flutter - 如何根据 Flutter 中具有动态大小的小部件的大小设置角半径

标签 flutter flutter-layout cornerradius

如何在按钮上使用公式 radius = hight/2 确定小部件的角半径,该按钮在其子项之后调整大小?

本例中的子项是一个文本,其长度可以与传递的字符串一样长。

这就是我得到的。 我将高度和最大宽度设置为零以包裹文本大小,但我无法找到在应用角半径时测量宽度的方法。

ButtonTheme(
  height: 0,
  minWidth: 0,
  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(width/2)), //how can i calculate width?
  child: RaisedButton(
    onPressed: onPress,
    child: Text('Some String of variable length'),
  ),
);

我在网上搜索过,但没有任何结果。

最佳答案

正如 @pskink 提到的,您可以使用 StadiumBorder :

A border that fits a stadium-shaped border (a box with semicircles on the ends) within the rectangle of the widget it is applied to.

下面是一个示例,在 TextField 上写入多行即可查看结果:

Center(
  child: Container(
    decoration: ShapeDecoration(
      shape: StadiumBorder(
        side: BorderSide(width: 2.0),
      ),
    ),
    child: TextField(
      textAlign: TextAlign.center,
      minLines: 1,
      maxLines: 100,
    ),
  ),
)

关于flutter - 如何根据 Flutter 中具有动态大小的小部件的大小设置角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58412318/

相关文章:

flutter - 从外部小部件触发小部件动画

flutter - 长按显示对话框,手指向上弹出

flutter - MediaQuery 与 IntrinsicHeight

Flutter : ListView : Scroll parent ListView when child ListView reach bottom - ClampingScrollPhysics not working in sized container

c# - 设置内容边框的圆角半径

ios - 无法使角半径工作

flutter - 如何 BoxFit.cover 具有特定纵横比的全屏 VideoPlayer 小部件

flutter : Custom Radio Button

dart - 如何在多个项目之间分配空间

ios - 我怎样才能圆容器 TableView 的角落?