I want to add a ripple on an item, it is working fine until I add a gradient on the item using BoxDecoration
.
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Material(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
elevation: 6.0,
shadowColor: Colors.grey[50],
child: InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: AlignmentDirectional.bottomStart,
end: AlignmentDirectional.topEnd,
colors: [
Colors.yellow[800],
Colors.yellow[700],
],
),
),
padding: EdgeInsets.all(16.0),
child: Text(
widget.title,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
),
);
}
Create a ripple effect using the following steps: Create a widget that supports tap. Wrap it in an InkWell widget to manage tap callbacks and ripple animations.
You can't disable inkwell in flutter, use an if condition to either display an inkwell or a container (without gesture handling properties) depending on the number of views the user has.
The main difference between InkWell and GestureDetector lies in Material widget. InkWell must have a Material Widget as an ancestor. However, there is no such compulsion for GestureDetector widget. Otherwise, in many ways, these two widgets share common features.
Update in 2019:
You should use Ink
widget inside Material
, instead of Container
.
It takes decoration
parameter as well:
Material(
child: Ink(
decoration: BoxDecoration(
// ...
),
child: InkWell(
onTap: () {},
child: child, // other widget
),
),
);
I found the solution:
I need one Material
for Inkwell
, and one Material
for elevation and rounded borders.
The inner Material
has a type of MaterialType.transparency
so that it doesn't draw anything over the box decoration of its parent and still preserve the ink effect. The shadow and borders are controlled by outer Material
.
Container(
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Material( // <----------------------------- Outer Material
shadowColor: Colors.grey[50],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
elevation: 6.0,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: AlignmentDirectional.bottomStart,
end: AlignmentDirectional.topEnd,
colors: [
AppColors.pinkDark,
AppColors.pink,
],
),
),
child: Material( // <------------------------- Inner Material
type: MaterialType.transparency,
elevation: 6.0,
color: Colors.transparent,
shadowColor: Colors.grey[50],
child: InkWell( //<------------------------- InkWell
splashColor: Colors.white30,
onTap: () {},
child: Container(
padding: EdgeInsets.all(16.0),
child: Row(
children: <Widget>[
Icon(
Icons.work,
size: 40.0,
color: Colors.white,
),
SizedBox(
width: 20.0,
),
Column(
children: <Widget>[
Text(
widget.title,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
],
)
],
),
),
),
),
),
),
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With