While designing an app that should follow the Material Design Guidelines, we’llwant to add the ripple animation to Widgets when tapped.

Flutter provides the InkWellWidget to achieve this effect.

Directions

  • Create a Widget we want to tap
  • Wrap it in an InkWell Widget to manage tap callbacks and ripple animations
  1. // The InkWell Wraps our custom flat button Widget
  2. InkWell(
  3. // When the user taps the button, show a snackbar
  4. onTap: () {
  5. Scaffold.of(context).showSnackBar(SnackBar(
  6. content: Text('Tap'),
  7. ));
  8. },
  9. child: Container(
  10. padding: EdgeInsets.all(12.0),
  11. child: Text('Flat Button'),
  12. ),
  13. );

Complete example

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. final title = 'InkWell Demo';
  7. return MaterialApp(
  8. title: title,
  9. home: MyHomePage(title: title),
  10. );
  11. }
  12. }
  13. class MyHomePage extends StatelessWidget {
  14. final String title;
  15. MyHomePage({Key key, this.title}) : super(key: key);
  16. @override
  17. Widget build(BuildContext context) {
  18. return Scaffold(
  19. appBar: AppBar(
  20. title: Text(title),
  21. ),
  22. body: Center(child: MyButton()),
  23. );
  24. }
  25. }
  26. class MyButton extends StatelessWidget {
  27. @override
  28. Widget build(BuildContext context) {
  29. // The InkWell Wraps our custom flat button Widget
  30. return InkWell(
  31. // When the user taps the button, show a snackbar
  32. onTap: () {
  33. Scaffold.of(context).showSnackBar(SnackBar(
  34. content: Text('Tap'),
  35. ));
  36. },
  37. child: Container(
  38. padding: EdgeInsets.all(12.0),
  39. child: Text('Flat Button'),
  40. ),
  41. );
  42. }
  43. }

Ripples Demo