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

Flutter:SearchBar设置透明后仍残留底色,如何自定义颜色?

解决Flutter SearchBar背景色叠加淡蓝色的问题

这个问题是因为SearchBar基于Material3设计,默认会自带一层surface tint(表面色调),就算你设置了backgroundColor,这个tint也会和自定义颜色叠加,导致出现残留的淡蓝色。

直接加个参数就能解决:

  • 在SearchBar的参数里添加surfaceTintColor: const MaterialStatePropertyAll(Colors.transparent),关闭默认的表面色调,这样自定义的背景色就能完全生效。

修改后的完整代码:

SearchBar(
  hintText: "Search",
  side: const MaterialStatePropertyAll(
      const BorderSide(color: Colors.black45)),
  shadowColor: const MaterialStatePropertyAll(Colors.transparent),
  overlayColor: const MaterialStatePropertyAll(Colors.transparent),
  backgroundColor: const MaterialStatePropertyAll(Colors.transparent),
  // 新增这行关闭surface tint
  surfaceTintColor: const MaterialStatePropertyAll(Colors.transparent),
  padding: const MaterialStatePropertyAll<EdgeInsets>(
      EdgeInsets.symmetric(horizontal: 16.0)),
  shape: const MaterialStatePropertyAll<RoundedRectangleBorder>(
      RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(10.0)))),
  onTap: () {
    // controller.openView();
  },
  onChanged: (e) {
    print(e);
    setState(() {
      _productList = searchProducts(e);
    });
  },
  leading: const Icon(Icons.search),
),

添加后,SearchBar的背景色就会和你设置的完全一致,不会再出现淡蓝色叠加的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 02:13:21