如何在小部件上禁用默认的溅泼/波纹/墨水效果?有时候这个效果是不需要的,比如下面的TextField案例:
highlightColor
和splashColor
都设置为Colors.transparent
可以去除涟漪效果。highlightColor
可能会有一些连锁反应,但我还没有注意到任何影响。splashFactory
。例如,下面的代码显示直接通过样式设置它,或者您也可以在主题中设置它:ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
splashFactory: NoSplash.splashFactory,
),
child: child,
);
Theme
中,并在 ThemeData
上将属性 splashColor
和 highlightColor
设置为透明。Theme(
data: ThemeData(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
),
child: YourWidget(),
);
Theme.of(context).copyWith
而不是 ThemeData
,因为它将保留已定义的主题数据,而不是使用 Flutter 的默认数据 :) - Hugo H设置为主题
final yourTheme = ThemeData.light();
...
Theme(
data: yourTheme.copyWith(
splashFactory: NoSplash.splashFactory,
),
...
)
设置为材料小部件
ElevatedButton(
style: ElevatedButton.styleFrom(
splashFactory: NoSplash.splashFactory,
),
onPressed: () { },
child: Text('No Splash'),
)
splashFactory
替换主题的原始 splashFactory
。class NoSplashFactory extends InteractiveInkFeatureFactory {
const NoSplashFactory();
@override
InteractiveInkFeature create({
MaterialInkController controller,
RenderBox referenceBox,
Offset position,
Color color,
TextDirection textDirection,
bool containedInkWell = false,
Rect Function() rectCallback,
BorderRadius borderRadius,
ShapeBorder customBorder,
double radius,
VoidCallback onRemoved,
}) {
return 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) {}
}
将您的小部件用它包装起来:
child: new Theme(
data: new ThemeData(splashFactory: const NoSplashFactory()),
child: new TextField(...),
),
最初由HansMuller在GitHub PR上回答。
ThemeData(splashColor: Colors.transparent)
。 - hunterInteractiveInkFeatureFactory
是一个抽象类,因此应该删除@override
注释。 - EvansplashFactory: NoSplash.splashFactory
。 - Pavel我会修改Camilo的方法,以确保我们不会覆盖父主题的其他属性。
var color = Colors.transparent;
Theme(
data: Theme.of(context).copyWith(
highlightColor: color,
splashColor: color,
hoverColor: color,
),
child: YourWidget(),
)
我尝试了上述回答,但没有成功(splashColor: Colors.transparent, highlightColor: Colors.transparent,
)。
我的解决方案是只设置hoverColor:Colors.transparent
在您的Material AppTheme中,如下所示,将highlightColor
、splashColor
设置为透明,并将splashFactory
设置为NoSplash。
MaterialApp(
theme: ThemeData(
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
splashFactory: NoSplash.splashFactory,,
)
)
在寻找解决ElevatedButton
闪屏问题的方法时,我发现了这个问题。尽管这里提出的所有解决方案都无法解决我的问题,但Theme(data: ThemeData(... NoSplash..))
却可以工作,但由于某些原因它没有起作用。我在ButtonStyle()
中将overlayColor:..
设置为透明,如下所示:overlayColor: MaterialStateProperty.all(Colors.transparent),
并且它起作用了。希望这能帮助到某些人。
当我在寻找一种方法来移除列表overscroll中的斜杠时,与ThemeData相关的解决方案都不适用于我。我认为这个问题和我遇到的问题是一样的,因此对于任何面临同样误解的用户,以下解决方案被证明是有效的,并且当放入无状态小部件中时非常整洁,如下所示:
class NoSplash extends StatelessWidget {
NoSplash({this.child});
final Widget child;
@override
Widget build(BuildContext context) {
return NotificationListener<OverscrollIndicatorNotification>(
onNotification: (OverscrollIndicatorNotification overscroll) {
overscroll.disallowGlow();
return true;
},
child: child);
}
}
用于ElevatedButton。(Flutter 2.20+)
class NoSplashState extends MaterialStatesController{
@override
void update(MaterialState state, bool add) {
}
}
ElevatedButton(
statesController: NoSplashState(),
...
)
highlightColor
设置为透明会使得使用Theme.of(context).highlightColor
毫无意义。 - Andrey GordeevTextButton.styleFrom(splashFactory: NoSplash.splashFactory);
我不知道为什么。 - undefined