· Flutter Deep Dives · 3 min read
Creating water ripple effects with SkiaShadersMask in Flutter
One of the powerful features of Flutter is the ability to create custom visual effects using shaders. In this tutorial, we’re going to explore how to create a water ripple effect using SkiaShadersMask in Flutter. This effect can be used to enhance the visual appeal of your app or add a dynamic touch to your user interface.
Prerequisites
Before we begin, make sure you have Flutter installed and set up on your machine. You should also have a basic understanding of Flutter and Dart programming language.
Setting up the project
- Create a new Flutter project using the command
flutter create water_ripple_effects. - Open the project in your preferred code editor.
Adding dependencies
In your project’s pubspec.yaml file, add the following dependency:
dependencies:
skia: ^0.4.0
Then, run flutter pub get to download and add the dependency to your project.
Implementing the water ripple effect
- Create a new Dart file called
water_ripple_effects.dartin thelibdirectory. - Add the following code to import the necessary packages and define a stateful widget:
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:skia/skia.dart';
class WaterRippleEffects extends StatefulWidget {
@override
_WaterRippleEffectsState createState() => _WaterRippleEffectsState();
}
class _WaterRippleEffectsState extends State<WaterRippleEffects> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Water Ripple Effects'),
),
body: Center(
child: CustomPaint(
painter: WaterRipplePainter(),
),
),
);
}
}
- Create a new Dart file called
water_ripple_painter.dartin the samelibdirectory. - Add the following code to define a
WaterRipplePainterclass, which extendsCustomPainter:
class WaterRipplePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
// TODO: Implement the water ripple effect logic
}
@override
bool shouldRepaint(WaterRipplePainter oldDelegate) => true;
}
Implementing the water ripple effect logic
- Update the
paintmethod in theWaterRipplePainterclass with the following code:
void paint(Canvas canvas, Size size) {
final paint = Paint()
..maskFilter = MaskFilter.blur(BlurStyle.normal, 10.0);
final center = Offset(size.width / 2, size.height / 2);
final radius = size.width / 3;
final shader = SkiaShadersMask.createRadial(
center,
radius,
[Colors.blue, Colors.transparent],
TileMode.mirror,
);
final rect = Rect.fromCircle(center: center, radius: radius);
canvas.saveLayer(
rect,
paint..shader = shader,
);
canvas.drawRect(
Rect.fromLTWH(0, 0, size.width, size.height),
Paint()
..shader = Gradient.linear(
Offset(0, size.height / 2),
Offset(size.width, size.height / 2),
[Colors.blue, Colors.green],
),
);
canvas.restore();
}
- Replace the
TODOcomment with the logic to create the water ripple effect. In this example, we use theMaskFilter.blurmethod to add a blur effect to the painting, and theSkiaShadersMask.createRadialmethod to create a radial gradient that gives the visual appearance of a ripple effect.
Testing the water ripple effect
- In the
main.dartfile, update thevoid mainmethod to use theWaterRippleEffectswidget:
void main() {
runApp(MaterialApp(
home: WaterRippleEffects(),
));
}
- Run the app using
flutter runcommand in your terminal or from your IDE.
Congratulations! You have successfully created a water ripple effect using SkiaShadersMask in Flutter. Feel free to experiment with different settings and properties to achieve the desired visual effect.