如何在Flutter中禁用默认的Widget飞溅效果?

xip*_*xip 9 widget flutter

如何在Widget上禁用默认的splash/ripple/ink效果?有时效果是不需要的,例如在以下TextField案例中:

小智 46

您可以将组件包装到Theme并设置属性splashColorhighlightColor透明ThemeData

Theme(
  data: ThemeData(
    splashColor: Colors.transparent,
    highlightColor: Colors.transparent,
  ),
  child: YourWidget(),
);
Run Code Online (Sandbox Code Playgroud)

  • 最佳答案在这里,不改变整个应用程序主题!您甚至可以使用 `Theme.of(context).copyWith` 而不是 `ThemeData`,因为它将保留已定义的主题数据,而不是 flutter 的默认主题数据:) (2认同)

Ale*_*x.F 29

使用NoSplash.splashFactory

设置主题

final yourTheme = ThemeData.light();
...
Theme(
  data: yourTheme.copyWith(
    splashFactory: NoSplash.splashFactory,
  ),
  ...
)
Run Code Online (Sandbox Code Playgroud)

设置为材质小部件

ElevatedButton(
  style: ElevatedButton.styleFrom(
    splashFactory: NoSplash.splashFactory,
  ),
  onPressed: () { },
  child: Text('No Splash'),
)
Run Code Online (Sandbox Code Playgroud)


Cra*_*rds 18

根据以上@hunter的建议,我发现通过在主题中同时设置highlightColorsplashColorColors.transparent消除波纹。

我确实担心设置highlightColor可能会产生连锁反应,但是我还没有注意到。


xip*_*xip 9

您可以将主题替换为splashFactory不绘制任何内容的主题:

class NoSplashFactory extends InteractiveInkFeatureFactory {
  const NoSplashFactory();

  @override
  InteractiveInkFeature create({
    @required MaterialInkController controller,
    @required RenderBox referenceBox,
    @required Offset position,
    @required Color color,
    bool containedInkWell: false,
    RectCallback rectCallback,
    BorderRadius borderRadius,
    double radius,
    VoidCallback onRemoved,
  }) {
    return new NoSplash(
      controller: controller,
      referenceBox: referenceBox,
    );
  }
}

class NoSplash extends InteractiveInkFeature {
  NoSplash({
    @required MaterialInkController controller,
    @required RenderBox referenceBox,
  })  : assert(controller != null),
        assert(referenceBox != null),
        super(
          controller: controller,
          referenceBox: referenceBox,
        );

  @override
  void paintFeature(Canvas canvas, Matrix4 transform) {}
}
Run Code Online (Sandbox Code Playgroud)

用它包装你的小部件:

child: new Theme(
  data: new ThemeData(splashFactory: const NoSplashFactory()),
  child: new TextField(...),
),
Run Code Online (Sandbox Code Playgroud)

最初由HansMuller在GitHub PR上回答.

  • 你也可以定义一个透明的splashColor; 即:`ThemeData(splashColor:Colors.transparent)` (5认同)
  • 现在它已合并到 flutter 中了!使用“splashFactory:NoSplash.splashFactory” (3认同)

Alr*_*hir 7

我已经尝试了上面的答案,但没有成功(splashColor: Colors.transparent, highlightColor: Colors.transparent,)。

我的解决方案是只设置hoverColor:Colors.transparent


iDe*_*ode 6

我将修改 Camilo 的方法,以确保我们不会覆盖父主题的其他属性。

var color = Colors.transparent;
Theme(
  data: Theme.of(context).copyWith(
    highlightColor: color,
    splashColor: color,
    hoverColor: color,
  ),
  child: YourWidget(),
)
Run Code Online (Sandbox Code Playgroud)


Dha*_*ara 6

highlightColor在您的材质应用程序主题中将、设置splashColor透明splashFactory设置为NoSplash ,如下所示。

MaterialApp(
   theme: ThemeData(
      highlightColor: Colors.transparent,
      splashColor: Colors.transparent,
      splashFactory: NoSplash.splashFactory,,
    )
 )
Run Code Online (Sandbox Code Playgroud)