Flutter

How can I add shadow to the widget in flutter

25 September 2026 · 4 min read

How can I add shadow to the widget in flutter

Adding shadows to widgets in Flutter is a crucial aspect of UI design, giving depth and dimension to your app’s interface. It’s a simple yet effective way to visually separate elements, create a sense of hierarchy, and enhance the overall user experience. Whether you’re designing buttons, cards, or containers, mastering shadow implementation can significantly elevate your Flutter app’s aesthetics and professionalism. This guide will provide a comprehensive understanding of how to implement shadows in Flutter, from basic techniques to advanced customization.

Understanding Box Shadows in Flutter

In Flutter, shadows are created using the BoxShadow class. This class offers a rich set of properties that let you control the shadow’s appearance. Think of it as a virtual light source casting a shadow behind your widget. The key properties to understand are color, blurRadius, spreadRadius, and offset.

The color property, as you might expect, determines the shadow’s hue. blurRadius controls the softness of the shadow’s edges – a higher value results in a more diffused, blurry shadow. spreadRadius dictates the shadow’s size: positive values expand the shadow beyond the widget’s bounds, while negative values shrink it inwards. Finally, the offset property, an Offset object, controls the shadow’s position relative to the widget, defining its horizontal and vertical displacement.

A common mistake is confusing blurRadius and spreadRadius. Remember, blur affects the edge softness, while spread affects the shadow’s size.

Implementing Basic Shadows

Adding a basic shadow is straightforward. Wrap your widget with a Container and apply the boxShadow property. Here’s a simple example:

dart Container( decoration: BoxDecoration( boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 5, offset: Offset(0, 3), // changes position of shadow ), ], ), child: YourWidget(), ) This code snippet creates a subtle, grey shadow beneath the YourWidget. You can adjust the BoxShadow properties to achieve the desired effect. Experiment with different values for blurRadius, spreadRadius, and offset to see how they influence the shadow’s appearance.

For instance, to create a sharp, intense shadow, use a small blurRadius and a negative spreadRadius. Conversely, a large blurRadius and a positive spreadRadius create a diffused, expansive shadow.

Advanced Shadow Customization

Flutter allows for creating complex shadow effects using multiple BoxShadow instances. By supplying a list of BoxShadow objects, you can layer shadows, creating rich and visually interesting results. This is particularly useful for simulating inner shadows or creating a “glow” effect.

dart Container( decoration: BoxDecoration( boxShadow: [ BoxShadow( color: Colors.blue.withOpacity(0.3), spreadRadius: 5, blurRadius: 7, offset: Offset(0, 3), ), BoxShadow( color: Colors.red.withOpacity(0.2), spreadRadius: -2, blurRadius: 3, offset: Offset(0, -1), ), ], ), child: YourWidget(), ) This example demonstrates the use of two BoxShadow instances: a blue outer shadow and a subtle red inner shadow. By combining multiple shadows with varying properties, you can achieve intricate and visually appealing effects.

Remember, overusing shadows can clutter your UI. Use them strategically to enhance visual hierarchy and separate elements, not to overwhelm the user.

Optimizing Shadow Performance

While shadows enhance visual appeal, excessive use can impact performance, particularly on lower-end devices. Flutter offers ways to mitigate this. One effective technique is to cache the shadow using a ShaderMask. This offloads the shadow rendering to the GPU, improving performance.

  • Use ShaderMask for complex or frequently animated shadows.
  • Minimize the number of BoxShadow instances used.

Another optimization strategy involves using the RepaintBoundary widget. Wrapping a widget with a RepaintBoundary isolates its rendering layer, preventing unnecessary repaints of parent widgets when the shadow changes. This is especially useful when animating shadows.

  1. Identify widgets with dynamic shadows.
  2. Wrap these widgets with RepaintBoundary.

By implementing these optimization strategies, you can ensure that your Flutter app maintains smooth performance even with complex shadow effects.

Infographic Placeholder: Illustrating the different BoxShadow properties and their impact on the visual output.

Learn More about Flutter UI DesignFrequently Asked Questions

Q: How do I create a drop shadow in Flutter?

A: A drop shadow is simply a box shadow with a positive vertical offset. Use the offset property in the BoxShadow class to position the shadow below the widget.

Mastering shadows in Flutter empowers you to craft visually appealing and engaging user interfaces. By understanding the BoxShadow properties and applying the optimization techniques discussed, you can enhance your app’s aesthetics without compromising performance. Remember to use shadows judiciously, focusing on enhancing visual hierarchy and improving user experience. Explore the provided resources for further learning and elevate your Flutter UI design skills. Check out these additional resources for further learning: Flutter BoxDecoration Documentation, BoxShadow Class, and Flutter Performance Best Practices. Consider exploring related topics like Material Design principles and advanced Flutter animation techniques to further refine your UI development skills.

Question & Answer :
How can I add shadow to the widget like in the picture below?

This is my current widget code.

Image with shadow

Check out BoxShadow and BoxDecoration

A Container can take a BoxDecoration (going off of the code you had originally posted) which takes a boxShadow

return Container( margin: EdgeInsets.only(left: 30, top: 100, right: 30, bottom: 50), height: double.infinity, width: double.infinity, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(10)), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 5, blurRadius: 7, offset: Offset(0, 3), // changes position of shadow ), ], ), )