InkWell not showing ripple effect

后端 未结 13 733
陌清茗
陌清茗 2020-11-30 01:39

Tapping the container triggers the onTap() handler but does not show any ink splash effect.

class _MyHomePageState extends State

        
相关标签:
13条回答
  • 2020-11-30 02:05
    1. The InkWell widget must have a Material widget as an ancestor otherwise it can't show effects. E.g.:

      Material(
        child : InkWell(
          child : .....
      
    2. you have to add onTap method to see the actual effects as like

       Buttons {RaisedButton,FlatButton etc}.
       e.g -> Material(
                   child : InkWell(
                           onTap : (){}
                           child : .....
      

    Let's come to the main points see some examples below and try to understand the actual concepts of 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,
                ),
              ),
            ),
          ),
        );
       }
      
    0 讨论(0)
  • 2020-11-30 02:07

    I have found this solution for me. I think it can help you:

    Material(
          color: Theme.of(context).primaryColor,
          child: InkWell(
            splashColor: Theme.of(context).primaryColorLight,
            child: Container(
              height: 100,
            ),
            onTap: () {},
          ),
        )
    

    Color is given to Material widget. It is the default color of your widget. You can adjust color of ripple effect using splashColor property of Inkwell.

    0 讨论(0)
  • 2020-11-30 02:08

    I think adding color to the container is covering over the ink effect

    https://docs.flutter.io/flutter/material/InkWell/InkWell.html

    This code seems to work

      body: new Center(
        child: new Container(
          child: new Material(
            child: new InkWell(
              onTap: (){print("tapped");},
              child: new Container(
                width: 100.0,
                height: 100.0,
              ),
            ),
            color: Colors.transparent,
          ),
          color: Colors.orange,
        ),
      ),
    

    just click the middle square.

    Edit: I found the bug report. https://github.com/flutter/flutter/issues/3782

    This is actually as expected, though we should update the docs to make it clearer.

    What's going on is that the Material spec says that the splashes are actually ink on the Material. So when we splash, what we do is we literally have the Material widget do the splash. If you have something on top of the Material, we splash under it, and you can't see it.

    I have wanted to add a "MaterialImage" widget which conceptually prints its image into the Material as well so that then the splashes would be over the image. We could have a MaterialDecoration which does something similar for a Decoration. Or we could have Material itself take a decoration. Right now it takes a color, but we could extend that to taking a whole decoration. It's not clear whether it's really material-spec-compatible to have a material with a gradient, though, so I'm not sure whether we should do that.

    In the short run, if you just need a workaround, you can put a Material on top of the container, with the material set to use the "transparency" type, and then put the ink well inside that.

    --hixie

    Update: Hixie merged a new Ink solution last year. The Ink provides a convenient way to splash over images.

      testWidgets('Does the Ink widget render anything', (WidgetTester tester) async {
        await tester.pumpWidget(
          new Material(
            child: new Center(
              child: new Ink(
                color: Colors.blue,
                width: 200.0,
                height: 200.0,
                child: new InkWell(
                  splashColor: Colors.green,
                  onTap: () { },
                ),
              ),
            ),
          ),
        );
    
    
    Material(
      color: Colors.grey[800],
      child: Center(
        child: Ink.image(
          image: AssetImage('cat.jpeg'),
          fit: BoxFit.cover,
          width: 300.0,
          height: 200.0,
          child: InkWell(
            onTap: () { /* ... */ },
            child: Align(
              alignment: Alignment.topLeft,
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: Text('KITTEN', style: TextStyle(fontWeight: FontWeight.w900, color: Colors.white)),
              ),
            )
          ),
        ),
      ),
    )
    

    Please Note: I did not test the new Ink Widget. I coped the code from ink_paint_test.dart and the Ink class docs

    https://github.com/Hixie/flutter/blob/1f6531984984f52328e66c0cd500a8d517964564/packages/flutter/test/material/ink_paint_test.dart

    https://github.com/flutter/flutter/pull/13900

    https://api.flutter.dev/flutter/material/Ink-class.html

    0 讨论(0)
  • 2020-11-30 02:10

    The solution to circular widgets, do like below:

    Material(
        color: Colors.transparent,
        child: Container(
          alignment: Alignment.center,
          height: 100,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              IconButton(
                  enableFeedback: true,
                  iconSize: 40,
                  icon: Icon(
                    Icons.skip_previous,
                    color: Colors.white,
                    size: 40,
                  ),
                  onPressed: () {}),
              IconButton(
                iconSize: 100,
                enableFeedback: true,
                splashColor: Colors.grey,
                icon: Icon(Icons.play_circle_filled, color: Colors.white, size: 100),
                padding: EdgeInsets.all(0),
                onPressed: () {},
              ),
              IconButton(
                  enableFeedback: true,
                  iconSize: 40,
                  icon: Icon(
                    Icons.skip_next,
                    color: Colors.white,
                    size: 40,
                  ),
                  onPressed: () {}),
            ],
          ),
        ),
      )
    
    0 讨论(0)
  • 2020-11-30 02:17

    This is working for me:

    Material(
        color: Colors.white.withOpacity(0.0),
        child: InkWell(
          splashColor: Colors.orange,
          child: Text('Hello'), // actually here it's a Container wrapping an image
          onTap: () {
            print('Click');
          },
        ));
    

    After trying many answers here, it was a combination of:

    1. Setting splashColor
    2. Wrapping InkWell in Material(color: Colors.white.withOpacity(0.0), ..)

    Thanks to the answers here that make those 2 points

    0 讨论(0)
  • 2020-11-30 02:19

    For me it was another problem. InkWell was not showing ripple effect when i had onTap function as parameter of my widget defined as below.

    Function(Result) onTapItem;
    ...
    onTap: onTapItem(result),
    

    I don't know what's the difference, but next code is working well.

    onTap: (){ onTapItem(result); },
    
    0 讨论(0)
提交回复
热议问题