You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何禁用BoxDecoration的抗锯齿效果

解决方法

BoxDecoration 默认开启了抗锯齿效果,你只需要显式将其 antiAlias 属性设置为 false 即可关闭该特性,修改后的代码如下:

class MyButton extends StatefulWidget {
    const MyButton({Key? key}) : super(key: key);

    @override
    _MyButtonState createState() => _MyButtonState();
}

class _MyButtonState extends State<MyButton> {
    @override
    Widget build(BuildContext context) {    
        return Container(
            padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 2.0),
            decoration: const BoxDecoration(
                // 新增此行关闭抗锯齿
                antiAlias: false,
                border: Border(
                    top: BorderSide(width: 5, color: Colors.grey),
                    left: BorderSide(width: 5, color: Colors.grey),
                    bottom: BorderSide(width: 5, color: Colors.grey),
                    right: BorderSide(width: 5, color: Colors.grey)
                ),
                color: Colors.blue,
            ),
            child: const Text('OK', textAlign: TextAlign.center, style: TextStyle(color: Colors.black)),
        );
    }
}

如果修改后仍存在轻微边缘模糊,可给每个 BorderSide 添加 strokeAlign: StrokeAlign.inside 配置,让边框完全绘制在容器内部,避免边缘出现半透明过渡效果。

内容的提问来源于stack exchange,提问作者Nukiloco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 15:18:03