点击容器触发onTap()处理程序,但不会显示任何墨水飞溅效果。

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new InkWell(
          onTap: (){print("tapped");},
          child: new Container(
            width: 100.0,
            height: 100.0,
            color: Colors.orange,
          ),
        ),
      ),
    );
  }
}

我试着把InkWell放在容器里,但徒劳。


当前回答

InkWell小部件必须有一个材质小部件作为祖先,否则它不能显示效果。例如: 材料( 孩子:InkWell( 孩子:..... 你必须添加onTap方法来查看实际效果 按钮{RaisedButton,FlatButton等}。 例如->材料( 孩子:InkWell( onTap:(){} 孩子:.....

让我们来看看要点,看看下面的例子,试着理解 InkWell的实际概念。

In Below example Material is parent of InkWell with onTap but it still not working. Please try to understand the concept of it. You should provide some margin to container or other widget to show the effects. Actually below code working fine but we can't see because we did not provide any margin or align so it has no space to show the effects. Widget build(BuildContext context) { return Center( child: Material( child: new InkWell( onTap: () { print("tapped"); }, child: new Container( width: 100.0, height: 100.0, color: Colors.orange, ), ), ), ); } Below example show InkWell effects only to upwards because we provide space {margin}. Widget build(BuildContext context) { return Center( child: Material( child: new InkWell( onTap: () { print("tapped"); }, child: new Container( margin: EdgeInsets.only(top: 100.0), width: 100.0, height: 100.0, color: Colors.orange, ), ), ), ); } Below exp. show the effects in all the page because center create margin from all the side. Centre align it's child widget from top, left, right and bottom. Widget build(BuildContext context) { return Center( child: Material( child: new InkWell( onTap: () { print("tapped"); }, child: Center( child: new Container( width: 100.0, height: 100.0, color: Colors.orange, ), ), ), ), ); }

其他回答

InkWell小部件必须有一个材质小部件作为祖先,否则它不能显示效果。例如: 材料( 孩子:InkWell( 孩子:..... 你必须添加onTap方法来查看实际效果 按钮{RaisedButton,FlatButton等}。 例如->材料( 孩子:InkWell( onTap:(){} 孩子:.....

让我们来看看要点,看看下面的例子,试着理解 InkWell的实际概念。

In Below example Material is parent of InkWell with onTap but it still not working. Please try to understand the concept of it. You should provide some margin to container or other widget to show the effects. Actually below code working fine but we can't see because we did not provide any margin or align so it has no space to show the effects. Widget build(BuildContext context) { return Center( child: Material( child: new InkWell( onTap: () { print("tapped"); }, child: new Container( width: 100.0, height: 100.0, color: Colors.orange, ), ), ), ); } Below example show InkWell effects only to upwards because we provide space {margin}. Widget build(BuildContext context) { return Center( child: Material( child: new InkWell( onTap: () { print("tapped"); }, child: new Container( margin: EdgeInsets.only(top: 100.0), width: 100.0, height: 100.0, color: Colors.orange, ), ), ), ); } Below exp. show the effects in all the page because center create margin from all the side. Centre align it's child widget from top, left, right and bottom. Widget build(BuildContext context) { return Center( child: Material( child: new InkWell( onTap: () { print("tapped"); }, child: Center( child: new Container( width: 100.0, height: 100.0, color: Colors.orange, ), ), ), ), ); }

添加onTap:(){}监听器后,涟漪效应应该工作良好。如果你在InkWell()小部件中使用BoxShadow(),它就不工作了。

用材料包裹InkWell,然后使用你需要的颜色:

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: Material(
          color: Colors.orange,
          child: new InkWell(
            onTap: (){ print("tapped"); },
            child: new Container(
              width: 100.0,
              height: 100.0,
            ),
          ),
        ),
      ),
    );
  }
}

用简单的话快速解决:

此解决方案适用于小部件树的任何位置。 只需在InkWell之前添加额外的材质,如下所示:

return Container(
  .......code.......
  ),
  child: Material(
    type: MaterialType.transparency,
    child: InkWell(
      onTap: () {},
      child: Container(
        .......code.......
      ),
    ),
  ),
);

参考: https://api.flutter.dev/flutter/material/InkWell-class.html “墨水飞溅不可见!”

为了处理连锁反应,你应该使用以下规则:

onTap不应该为空(或空)。 InkWell必须在任何材质小部件中