Flutter inkwell ripple color

InkWell not showing ripple effect

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

I tried putting the InkWell inside the Container as well but in vain.

16 Answers 16

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

This code seems to work

just click the middle square.

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 that 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.

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

Источник

InkWell and FlatButton do not ripple when inside a container with opaque background #3782

Comments

drewwarren commented May 6, 2016 •

Steps to Reproduce

I have only tested this against an android emulator.

Note that the ripple does not appear on the FlatButton and InkWell for first and third rows (the ones that use opaque backgrounds). The ripple works fine on the raised button for all rows.

Flutter Doctor

Logs and Crash Reports

No logs. Requires visual inspection.

The text was updated successfully, but these errors were encountered:

Hixie commented May 6, 2016

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.

On 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.

Источник

Flutter — Using InkWell and InkResponse Examples

This tutorial shows you how to use InkWell and InkResponse in Flutter with some examples, available properties, and the differences between those two.

For this tutorial, we are going to create a simple layout consisting of the InkWell or InkResponse widget and a counter text that will be incremented every time the Ink widget pressed.

InkWell

InkWell is an area of Material widget that responds when being touched by showing splash. The shape is always rectangle and it clips the splash.

Using InkWell Examples

First, we create an InkWell with yellow splash color and blue highlight color. To increment the counter when the widget pressed, onTap is passed with a function that increments the _count state.

However, in the above output, the splash color is not shown clearly because it’s covered by the highlight color. To make it more visible, we can set opacity to the highlight color.

The highlight color is the color for highlighted area which is shown immediately when the user presses the widget. Whereas splash color is shown gradually from the point where the touch happens, creating splash animation effect. That means the splash doesn’t always start from the center.

Below are the properties you can pass to the constructor.

InkWell Properties

Name Type Description
key Key The widget key, used to control if it’s should be replaced.
child Widget A child widget that will be placed below this widget in tree.
onTap GestureTapCallback Callback that will be called when the user taps.
onDoubleTap GestureTapCallback Callback that will be called when the user double taps.
onLongPress GestureLongPressCallback Callback that will be called when the user long taps.
onTapDown GestureTapDownCallback Callback that will be called when the user taps down.
onTapCancel GestureTapCancelCallback Callback that will be called when the user cancels the tap.
onHighlightChanged ValueChanged Defaults to false .
onHover ValueChanged Called when a pointer enters or exits the ink response area.
focusColor Color The color of the ink response when the parent widget is focused. Defaults to ThemeData.focusColor .
hoverColor Color The color of the ink response when a pointer is hovering over it. Defaults to ThemeData.hoverColor .
highlightColor Color The color of the ink response when pressed. Defaults to ThemeData.highlightColor .
splashColor Color The splash color of the ink response. Defaults to ThemeData.splashColor
splashFactory InteractiveInkFeatureFactory The appearance of the splash. Defaults to ThemeData.splashColor .
radius double The ink splash’s radius.
borderRadius BorderRadius Clipping radius of the containing rectangle, only used if customBorder is null .
customBorder ShapeBorder Defines a custom clip border that overrides borderRadius .
enableFeedback bool Whether detected gestures should provide acoustic and/or haptic feedback.
excludeFromSemantics bool Whether to exclude the gestures introduced by this widget from the semantics tree.
focusNode FocusNode Focus node for the widget (optional).
canRequestFocus bool Whether the widget may request the primary focus
onFocusChange ValueChanged Handler called when the focus change
autofocus bool Whether the widget will be selected as the initial focus.

InkResponse

InkResponse is an area of Material widget that responds when being touched by showing splash. Unlike InkWell , the shape of InkResponse can be configured. You can also configure whether it should clip the splash.

Using InkResponse Examples

InkResponse Properties

The properties are similar to InkWell , but it also has containedInkWell and highlightShape .

Источник

InkWell class Null safety

A rectangular area of a Material that responds to touch.

For a variant of this widget that does not clip splashes, see InkResponse.

The following diagram shows how an InkWell looks when tapped, when using default values.

The InkWell widget must have a Material widget as an ancestor. The Material widget is where the ink reactions are actually painted. This matches the material design premise wherein the Material is what is actually reacting to touches by spreading ink.

If a Widget uses this class directly, it should include the following line at the top of its build function to call debugCheckHasMaterial:

Troubleshooting

The ink splashes aren’t visible!

If there is an opaque graphic, e.g. painted using a Container, Image, or DecoratedBox, between the Material widget and the InkWell widget, then the splash won’t be visible because it will be under the opaque graphic. This is because ink splashes draw on the underlying Material itself, as if the ink was spreading inside the material.

The Ink widget can be used as a replacement for Image, Container, or DecoratedBox to ensure that the image or decoration also paints in the Material itself, below the ink.

If this is not possible for some reason, e.g. because you are using an opaque CustomPaint widget, alternatively consider using a second Material above the opaque widget but below the InkWell (as an ancestor to the ink well). The MaterialType.transparency material kind can be used for this purpose.

The ink splashes don’t track the size of an animated container

If the size of an InkWell’s Material ancestor changes while the InkWell’s splashes are expanding, you may notice that the splashes aren’t clipped correctly. This can’t be avoided.

An example of this situation is as follows:

An InkWell’s splashes will not properly update to conform to changes if the size of its underlying Material, where the splashes are rendered, changes during animation. You should avoid using InkWells within Material widgets that are changing size.

  • GestureDetector, for listening for gestures without ink splashes.
  • ElevatedButton and TextButton, two kinds of buttons in material design.
  • InkResponse, a variant of InkWell that doesn’t force a rectangular shape on the ink reaction.

Inheritance

  • Object
  • DiagnosticableTree
  • Widget
  • StatelessWidget
  • InkResponse
  • InkWell

Источник

Adding InkWell Splash Ripple Effect To Custom Widgets In Flutter

By Sovit Poudel
Post date

In this post we will learn how to add InkWell ripple splash effect on custom widgets in a Flutter application.

Introduction

InkWell is Flutter’s implementation of Material Design concept for touch response. It helps to create interactivity in your mobile application by adding gesture feedback.

For example you might want to give ripple effect when user taps on certain area of the app like a button or a container.

Flutter framework already provides splash effect functionality to many of it’s widgets like RaisedButton, FlatButton, ListTile etc. by default. However, if you want to use this feature to your custom widgets, you will have to explicitly make use of InkWell widget.

Basic Setup

In this post we will create a custom Chip like container and add InkWell splash ripple effect on it. So, let’s begin with a basic setup of a Flutter Material App.

Create A Chip Like Container

So, far we have a container placed in the center of screen. Also, notice that we have set the color for container to be blue.

» data-medium-file=»https://stacksecrets.com/wp-content/uploads/2019/04/inkwell_1-150×300.png» data-large-file=»https://stacksecrets.com/wp-content/uploads/2019/04/inkwell_1.png» loading=»lazy» width=»240″ height=»480″ src=»http://stacksecrets.com/wp-content/uploads/2019/04/inkwell_1.png» alt=»Basic Setup For Custom Flutter Chip» data-lazy-srcset=»https://stacksecrets.com/wp-content/uploads/2019/04/inkwell_1.png 240w, https://stacksecrets.com/wp-content/uploads/2019/04/inkwell_1-150×300.png 150w» data-lazy-sizes=»(max-width: 240px) 100vw, 240px» data-lazy-src=»http://stacksecrets.com/wp-content/uploads/2019/04/inkwell_1.png?is-pending-load=1″ srcset=»data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″> Basic Setup For Custom Flutter Chip

If you currently tap on this container, we will not see any splash effects. So, now we will turn this container into a InkWell widget.

Adding InkWell To Add Touch Effect

We wrapped the Center and Text widgets by an InkWell widget. But if you run the app now, you would still not be able to see splash ripple effect.

This is because an InkWell widget must always have a Material widget as it’s parent widget.

Wrap InkWell By Material To Show Ripple Effect

After creating a top level Material widget, now if you run the app again, you can see that there is a splash effect.

» data-medium-file=»https://stacksecrets.com/wp-content/uploads/2019/04/inkwell_gif_1-152×300.gif» data-large-file=»https://stacksecrets.com/wp-content/uploads/2019/04/inkwell_gif_1.gif» loading=»lazy» width=»480″ height=»945″ src=»http://stacksecrets.com/wp-content/uploads/2019/04/inkwell_gif_1.gif» alt=»Splash effect on InkWell» data-lazy-src=»http://stacksecrets.com/wp-content/uploads/2019/04/inkwell_gif_1.gif?is-pending-load=1″ srcset=»data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″> Splash effect on InkWell

Great, we got a working inkwell splash effect in our Flutter app! But what happened to it’s color? The container color turned into white.

Setting Color Of InkWell Container

Any child widgets placed inside the Material widget, takes the color set in the Material widget. So, we should set the color from Material’s color property itself. You can not set it from any the parent Container widget.

Another important thing to note when creating InkWell with Material parent widget is that you should set the color from the Material’s property itself. You can not set it from the Container .

So finally, our code that fixes the InkWell effect and the container color looks like this:

Important Points To Note Before Adding InkWell Widget

  • An InkWell widget must always have a Material widget as it’s parent.
  • It will only work when you have implemented at least one of its click event handler like the onTap event handler.
  • To set the widget color, it should be done via the color property of the containing Material widget.

Conclusion

In this post we looked at how to use Flutter’s InkWell widget to add splash ripple effect. We also learnt that Material widget must be used as a parent and also the properties like color and shapes should be defined from the Material widget itself.

Источник

Оцените статью
Adblock
detector