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

如何为Flutter的InkResponse组件设置点击时的矩形高亮效果?

原因说明

InkResponse的高亮效果需要基于父级Material组件渲染,你当前外层用Container包裹,Container的背景装饰会直接挡住Ink系列的墨水效果,所以就算设置了highlightShape也不会生效。

修复方案

把外层的Container替换为Material组件,直接通过Material的属性配置尺寸、圆角、背景色等样式即可,修改后的代码如下:

Material(
  clipBehavior: Clip.antiAliasWithSaveLayer,
  height: 11.h,
  width: 10.h,
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.all(Radius.circular(1.w)),
  ),
  color: const Color(accentColor),
  child: InkResponse(
    highlightShape: BoxShape.rectangle,
    highlightColor: const Color(greyColor),
    onTap: () async {},
    child: BlocBuilder<HomeCubit, HomeState>(
      buildWhen: (previousState, currentState) =>
          previousState != currentState,
      builder: (context, state) {
        return Icon(
          state is OpenBottomSheet ? Icons.save : Icons.add,
          color: const Color(whiteColor),
          size: 9.h,
        );
      },
    ),
  ),
)

如果有场景必须保留外层Container,也可以在Container和InkResponse之间插入一层Material(color: Colors.transparent),同样可以让高亮效果正常渲染。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 16:57:04