Flutter Widget Guide: DecoratedBox Widget | Flutter Agency (2024)

What is DecoratedBox Widget?

DecoratedBox Widget paints a decoration onto another box like a container widget that is a child of DecoratedBox. DecoratedBox which defines the decoration you want.

Container Widget inserts its child by the widths of the borders; this widget does not. DecoratedBox commonly used with BoxDecoration.

DecoratedBox Widget combines with BoxDecoration is a handy and flexible way to add more styling to any Container Widget elements as your needs require.

Default Constructor of DecoratedBox will look like below :

 DecoratedBox({ Key key, @required Decoration decoration, DecorationPosition position: DecorationPosition.background, Widget child, });

The decoration and position arguments must not be null. By default the decoration paints behind the child.

Decorations can be painted in the foreground or background via DecorationPosition (i.e., in front of or behind the child, respectively

Code Snippet will look like below :

 DecoratedBox( child :widget(),)

Properties :

decoration: Represents the decoration to be drawn, its type is Decoration. Decoration. It is an abstract class that defines an interface createBoxPainter(). The main responsibility of the subclass is to create a brush by implementing it. The brush is used to draw decorations.

position: This attribute determines where to draw Decoration, DecorationPositionthe type of enumeration it receives, and the enumeration class has two values:

  • background: Draw after the sub-component, that is, background decoration.
  • foreground: Draw on the sub-component, namely the foreground.

child: The widget below this widget in the tree. child widget can have only single children. To use multiple children users can make use of Row Widget, Column Widget or Stack Widget

A simple example to will Look like below :

import 'package:flutter/material.dart';class DecoratedBoxWidget extends StatelessWidget { DecoratedBoxWidget({ Key key, @required Decoration decoration, DecorationPosition position: DecorationPosition.background, Widget child, }); @override Widget build(BuildContext context) { return Center( child: DecoratedBox( position: DecorationPosition.background, decoration: BoxDecoration( color: const Color(0xFFFFFFFF), border: Border.all( color: const Color(0xFF000000), style: BorderStyle.solid, width: 4.0, ), borderRadius: BorderRadius.zero, shape: BoxShape.rectangle, boxShadow: const <BoxShadow>[ BoxShadow( color: Color(0x66000000), blurRadius: 10.0, spreadRadius: 4.0, ) ], ), child: Container( child: Container( width: 200, height: 200, padding: EdgeInsets.all(20), child: FlutterLogo(), ), ), ), ); }}

We will get output like below. In the above code, we have wrapped a DecoratedBoxWidget in a Center Widget.

Flutter Widget Guide: DecoratedBox Widget | Flutter Agency (1)

Decorated Box Widget

General Queries on Decorated Box

  • How to set the background color of a Flutter OutlineButton?

Consider a code snippet below :

Container( color: Colors.green, child: OutlineButton( onPressed: () {}, color: Colors.orange, highlightColor: Colors.pink, child: Container( color: Colors.yellow, child: Text("A"), ), shape: CircleBorder(), ), ),

We will get output like below :

Flutter Widget Guide: DecoratedBox Widget | Flutter Agency (2)

Button Widget

To modify the backgroundColor of an OutlineButton you can use a DecoratedBox and a Theme widget.

Wrap your OutlinedButton inside a DecoratedBox. Set the shape of your DecoratedBox to the same shape your OutlinedButton. Now you can use the color attribute of your DecoratedBox to change the color. The result will still have small padding around the OutlinedButton. To remove this you can wrap the DecoratedBox inside a Theme in which you adjust the ButtonTheme. Inside the ButtonTheme you want to set materialTapTargetSize: MaterialTapTargetSize.shrinkWrap

Consider a solution using a DecoratedBox with below code snippet.

DecoratedBox( decoration: ShapeDecoration( shape: CircleBorder(), color: Colors.pinkAccent, ), child: Theme( data: Theme.of(context).copyWith( buttonTheme: ButtonTheme.of(context).copyWith( materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, ), ), child: OutlineButton( shape: CircleBorder(), child: Text('A'), onPressed: () => {}, ), ), ),

We will get our output as expected as below :

Flutter Widget Guide: DecoratedBox Widget | Flutter Agency (3)

DecoratedBox Widget

  • Stack paints Container Widget, but not DecoratedBox?

Consider a code snippet like below :

Widget build(BuildContext context) { return Stack( children: <Widget>[ DecoratedBox( 
// doesn't work decoration: BoxDecoration( color: Colors.red, ), ), ], );

When you look at the above code we have a DecoratedBox wrapped with Stack Widget. In this Stack doesn’t render DecoratedBox because DecoratedBox doesn’t have a default size while Container will have a parent size by default.

A Container widget is not just a DecoratedBox. It also uses other widgets, including SizedBox and Alignment.

We can say that

Container( color: Colors.red,)

is not to

DecoratedBox( decoration: BoxDecoration(color: Colors.red),)

but instead to

SizedBox.expand( child: DecoratedBox( decoration: BoxDecoration(color: Colors.red), ),)

Thanks for reading !!!

Do let us know your suggestion/feedback.

FlutterAgency.comis our portal Platform dedicated toFlutter TechnologyandFlutter Developers. The portal is full of cool resources from Flutter likeFlutter WidgetGuide,Flutter Projects,Code libsand etc.

Flutter Widget Guide: DecoratedBox Widget | Flutter Agency (4)

Written by Nirali Patel

Nirali Patel is a dedicated Flutter developer with over two years of experience, specializing in creating seamless mobile applications using Dart. With a passion for crafting user-centric solutions, Nirali combines technical proficiency with innovative thinking to push the boundaries of mobile app development.

Flutter Widget Guide: DecoratedBox Widget | Flutter Agency (2024)
Top Articles
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6178

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.