Flutter Tricks
Published on

How to Disable Material Splash Effect in Flutter

When building Flutter applications, you may want to customize the visual effects of certain components. One such effect is the material splash effect, which is the ripple effect that occurs when a user taps on a material widget. While this effect can be visually appealing in many scenarios, there are cases where you may want to disable it.

In this article, we will explore how to disable the material splash effect in Flutter using the material theme. The solution is straightforward and does not require additional code implementation, as the NoSplash.splashFactory is already available in the Flutter library.

Disabling Material Splash Effect Using Material Theme

To disable the material splash effect, we can leverage the splashFactory property provided by the material theme. This property allows us to customize the splash effect for all material widgets in our application.

Here's how you can disable the material splash effect using the material theme:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        splashFactory: NoSplash.splashFactory,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Disable Material Splash Effect'),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            // Button action
          },
          child: Text('Disable Splash'),
        ),
      ),
    );
  }
}

In the above code snippet, we define a MyApp widget as the root of our application. Inside the build method of MyApp, we set the theme property of MaterialApp to a ThemeData object. We assign NoSplash.splashFactory to the splashFactory property of ThemeData. This effectively disables the material splash effect for all material widgets in our application.

Inside the MyHomePage widget, we create a simple RaisedButton that triggers an action when pressed. Since we have disabled the material splash effect globally, this button will no longer exhibit the ripple effect when tapped.

Summary

Disabling the material splash effect in Flutter is as simple as leveraging the splashFactory property provided by the material theme. By assigning NoSplash.splashFactory to this property, we can disable the splash effect for all material widgets in our application.

Remember that the material splash effect provides visual feedback to users, so it should be disabled sparingly and only in situations where it is appropriate for the user experience.