简体   繁体   English

颤振| 边界半径不适用于Android

[英]Flutter | border radius not apply on Android

I want to find out why border radius wouldn't apply top half of the widget on Android.我想知道为什么边框半径不会在 Android 上应用小部件的上半部分。
Here is the image on Android这是Android上的图像

安卓

However on the web, it's working like image below.但是在网络上,它的工作方式如下图所示。

在此处输入图片说明

Does anyone know why?有谁知道为什么?

Code代码

    Center(
      child: Card(
        elevation: 1.5,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0))),
        child: SizedBox(
          width: 250.0,
          height: 150.0,
          child: Column(
            children: <Widget>[
              Container(
                width: double.infinity,
                height: 60.0,
                color: Colors.pink[200].withOpacity(0.95),
                child: Center(
                  child: Text('Felicity Jones'),
                ),
              ),
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 8.0),
                  child: Center(
                    child: Text('9:15'),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    ),

If you observe carefully by changing the opacity of the color for time being to:如果您仔细观察,暂时将颜色的opacity更改为:

color: Colors.pink[200].withOpacity(0.2),

You'll see that the top left and top right corners are cut-off and not filled by the color:你会看到左上角和右上角被截断,没有被颜色填充:

在此处输入图片说明

In order to avoid this, use Card widget's clipBehavior: Clip.antiAlias, property that covers all corners of the card with the given color.为了避免这种情况,请使用Card小部件的clipBehavior: Clip.antiAlias,属性,该属性用给定的颜色覆盖卡片的所有角落。 Here's the updated result:这是更新的结果:

在此处输入图片说明

Hope this answers your question.希望这能回答你的问题。

You can try the following:您可以尝试以下操作:

Center(
    child:
      Card(
          elevation: 1.5,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0))),
          child: SizedBox(
            width: 250.0,
            height: 150.0,
            child: Column(
              children: <Widget>[
                Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.vertical(
                        top: new Radius.circular(16.0)
                        ),
                    color: Colors.pink[200].withOpacity(0.95),
                  ),
                  width: double.infinity,
                  height: 60.0,
                  child: Center(
                    child: Text('Felicity Jones'),
                  ),
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 8.0),
                    child: Center(
                      child: Text('9:15'),
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
      ),

Note that the color was moved inside the BoxDecoration as it will fail to compile otherwise.请注意,颜色已在 BoxDecoration 内移动,否则将无法编译。 I kept the shape attribute so that the lower part of the card will be rounded as well.我保留了 shape 属性,以便卡片的下部也将变圆。 You could tinker with that and remove it, if necessary.如有必要,您可以修改并删除它。

截屏

Edit: In your case like @Darshan mentioned you can just set clipBehavior: Clip.antiAlias in Card widget.编辑:在你提到的@Darshan 的情况下,你可以在Card小部件中设置clipBehavior: Clip.antiAlias

You can use also use ClipRRect to force the child to have the given border radius, if you don't have clipBehavior .如果您没有clipBehavior ,您还可以使用ClipRRect强制子级具有给定的边框半径。

Center(
  child: Card(
    elevation: 1.5,
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(16.0),
    ),
    child: ClipRRect(
      borderRadius: BorderRadius.circular(16.0),
      child: SizedBox(
        width: 250.0,
        height: 150.0,
        child: Column(
          children: <Widget>[
            Container(
              width: double.infinity,
              height: 60.0,
              color: Colors.pink[200].withOpacity(0.95),
              child: Center(
                child: Text('Felicity Jones'),
              ),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 8.0),
                child: Center(
                  child: Text('9:15'),
                ),
              ),
            )
          ],
        ),
      ),
    ),
  ),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM