Creating visually appealing applications is a critical aspect of user experience design. In Flutter, one way to enhance your app's aesthetic appeal is by adding depth and sophistication through shadows.
In this blog, we will delve into the technicalities of implementing box shadows in your Flutter app, ensuring that your UI elements stand out with realism.
Before we dive into the code, let's understand what a BoxShadow widget is. In Flutter, the BoxShadow widget allows you to cast a shadow from a box. The shadow can be adjusted in color, position, blur, and spread to fit your design needs.
The BoxShadow class is a part of Flutter's painting library. It casts a box shadow on a Container or any other widget that supports decoration. The class provides several properties to customize the shadow's appearance.
To begin with, you need to have a Flutter environment set up. If you haven't done that yet, visit the official Flutter documentation to get started. Once you have your Flutter app up and running, you can start adding shadows to your widgets.
1void main() => runApp(MyApp()); 2 3class MyApp extends StatelessWidget { 4 5 Widget build(BuildContext context) { 6 return MaterialApp( 7 title: 'Flutter Box Shadow Demo', 8 theme: ThemeData( 9 primarySwatch: Colors.blue, 10 ), 11 home: ShadowExample(), 12 ); 13 } 14} 15
Let's create a simple Container widget to which we will apply a shadow.
1class ShadowExample extends StatelessWidget { 2 3 Widget build(BuildContext context) { 4 return Scaffold( 5 appBar: AppBar( 6 title: Text('Box Shadow Example'), 7 ), 8 body: Center( 9 child: Container( 10 width: 200, 11 height: 200, 12 decoration: BoxDecoration( 13 color: Colors.white, 14 boxShadow: [ 15 BoxShadow( 16 color: Colors.grey.withOpacity(0.5), 17 spreadRadius: 5, 18 blurRadius: 7, 19 offset: Offset(0, 3), 20 ), 21 ], 22 ), 23 child: Center(child: Text('Shadow')), 24 ), 25 ), 26 ); 27 } 28} 29
In the above example, we have a Container with a BoxShadow applied to it. The BoxShadow is defined within the decoration property of the Container.
When you want to add shadow to your Flutter app, the BoxShadow widget offers several properties to customize its appearance.
Now, let's apply shadow to our Container and see how to manipulate its properties to achieve the desired effect.
1Container( 2 width: 100, 3 height: 100, 4 decoration: BoxDecoration( 5 color: Colors.white, 6 boxShadow: [ 7 BoxShadow( 8 color: Colors.black.withOpacity(0.15), 9 spreadRadius: 0, 10 blurRadius: 10, 11 offset: Offset(0, 4), 12 ), 13 ], 14 ), 15) 16
In this example, we've added a simple shadow with a blurRadius of 10 and an offset of (0, 4), which positions the shadow 4 logical pixels below the Container.
To adjust the blur effect, you can modify the blurRadius property. Here's how you can increase the blurRadius to create a softer shadow.
1BoxShadow( 2 color: Colors.black.withOpacity(0.15), 3 spreadRadius: 0, 4 blurRadius: 20, // Increased blur radius 5 offset: Offset(0, 4), 6) 7
The offset property is crucial when you want to position the shadow in a specific direction relative to the container. By changing the values of the offset, you can simulate light sources coming from different angles, which affects where the shadow cast appears.
Example: Changing Shadow Position
1BoxShadow( 2 color: Colors.black.withOpacity(0.25), 3 spreadRadius: 0, 4 blurRadius: 15, 5 offset: Offset(5, 5), // Shadow moved to the right and bottom 6) 7
In this code snippet, the offset has been set to (5, 5), which moves the shadow 5 logical pixels to the right and 5 logical pixels down, creating the impression that the light source is coming from the top-left corner.
Sometimes, a single shadow is not enough to achieve the depth you're looking for. Flutter allows you to stack multiple boxshadow instances to create more complex and nuanced shadows.
Example: Layering Multiple Shadows
1Container( 2 width: 100, 3 height: 100, 4 decoration: BoxDecoration( 5 color: Colors.white, 6 boxShadow: [ 7 BoxShadow( 8 color: Colors.black.withOpacity(0.1), 9 spreadRadius: 0, 10 blurRadius: 6, 11 offset: Offset(0, 2), 12 ), 13 BoxShadow( 14 color: Colors.black.withOpacity(0.2), 15 spreadRadius: 0, 16 blurRadius: 10, 17 offset: Offset(0, 6), 18 ), 19 ], 20 ), 21) 22
In this example, two shadows with different blurRadius and offset values are layered on top of each other to create a more complex shadow effect.
The spreadRadius property can be used to control how much the shadow spreads out from the container. This can be useful when you need to increase the size of the shadow without altering its blur.
Example: Adjusting Spread Radius
1BoxShadow( 2 color: Colors.black.withOpacity(0.25), 3 spreadRadius: 5, // Increased spread radius 4 blurRadius: 15, 5 offset: Offset(0, 5), 6) 7
In this code, the spreadRadius is set to 5, which makes the shadow wider than the container itself.
The color parameter of the BoxShadow class allows you to customize the shadow's color. You can use the const Color constructor to specify the color and its opacity.
Example: Changing Shadow Color
1BoxShadow( 2 color: const Color(0xFF000000).withOpacity(0.5), // Semi-transparent black 3 spreadRadius: 0, 4 blurRadius: 15, 5 offset: Offset(0, 5), 6) 7
Here, the shadow color is set to a semi-transparent black, a common choice for shadows as it provides a natural look.
Sometimes, you might want to apply shadow to only one side of a container. This can be done by carefully adjusting the offset and spreadRadius properties.
Example: Shadow on One Side
1Container( 2 width: 100, 3 height: 100, 4 decoration: BoxDecoration( 5 color: Colors.white, 6 boxShadow: [ 7 BoxShadow( 8 color: Colors.black.withOpacity(0.25), 9 spreadRadius: -5, 10 blurRadius: 10, 11 offset: Offset(10, 0), // Shadow only on the right side 12 ), 13 ], 14 ), 15) 16
In this example, the spreadRadius is set to -5, which contracts the shadow, and the offset is set to (10, 0), casting the shadow only on the right side of the container.
Implementing shadows in your Flutter app can significantly enhance the visual appeal of your UI elements. By understanding and utilizing the properties of the BoxShadow class, such as color, offset, blurRadius, and spreadRadius, you can create realistic and attractive shadows that add depth to your application. Remember to experiment with different combinations of these properties to achieve the look that best fits your design vision.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.