flutter container rounded rectangle

Flutter: Creating Rounded Containers with Ease

By Christopher Lopez
AD

At the heart of Flutter's design system lies the Container widget, a versatile tool for applying decorations, padding, and layout configurations to its child widget. One of the most popular uses of this widget is to create rounded rectangles, adding a touch of elegance and visual hierarchy to your app's user interface.

By using the decoration property of the Container widget, we can apply a BoxDecoration, which in turn gives us access to the borderRadius property. This is where the magic happens. By setting borderRadius, we can easily transform a rectangular Container into a rounded rectangle, enhancing the visual appeal of our app.

BorderRadius Property in BoxDecoration

The borderRadius property in BoxDecoration expects a BorderRadiusGeometry object. This object can be instantiated with various ways to define the radius. Let's explore two common methods: BorderRadius.all() and BorderRadius.only().

AD
Package Vectors - Download Free High-Quality Vectors | Magnific (formerly Freepik)
Package Vectors - Download Free High-Quality Vectors | Magnific (formerly Freepik)
Better Homes & Gardens Canister - 11.1 Cup Flip-Tite® Rectangular Food Storage Container - Walmart.com
Better Homes & Gardens Canister - 11.1 Cup Flip-Tite® Rectangular Food Storage Container - Walmart.com
three trays with different shapes and sizes
three trays with different shapes and sizes

BorderRadius.all(Radius()) applies the same radius to all corners of the rectangle. BorderRadius.only(), on the other hand, offers fine-grained control over each corner, accepting a map of Corner values (top-left, top-right, bottom-right, bottom-left).

BorderRadius.all(Radius())

The BorderRadius.all() method is used when you want to apply the same radius to all corners. To use it, import the Radius class and set the value to a positive number. Here's an example:

37 oz. Rectangular Hinged Bakery Container - #SLP18D
37 oz. Rectangular Hinged Bakery Container - #SLP18D
Décor | Encore Events Rentals
Décor | Encore Events Rentals
20-Piece Flowers Rectangle Food Storage Set
20-Piece Flowers Rectangle Food Storage Set
```dart Container( decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(15)), ), ) ```

BorderRadius.only()

For more complex arrangements, use BorderRadius.only(). It takes a map of Corners and their respective Radius values. Here's an example that creates a rectangle with rounded top-left and bottom-right corners:

```dart Container( decoration: BoxDecoration( borderRadius: BorderRadius.only( topLeft: Radius.circular(10), bottomRight: Radius.circular(15), ), ), ) ```

Adding a Border with BorderRadius

To add a border around your rounded rectangle, use the border property of BoxDecoration. This property expects a Border object with its side property set. Inside the BorderSide object, you can define the color and width of your border.

AD

The borderSide property accepts a BorderSide object with various properties. Set the color and width as per your design needs.

Personalized Empty Gift Box - Velvet Box for Preserved Roses and Flowers - Round, Oval, Heart and Rectangle Shapes - Engraved
Personalized Empty Gift Box - Velvet Box for Preserved Roses and Flowers - Round, Oval, Heart and Rectangle Shapes - Engraved
AMERICAN OUTLET en Instagram: "Set de envases de v
AMERICAN OUTLET en Instagram: "Set de envases de v
Tinted Rectangular Container Set - Purple
Tinted Rectangular Container Set - Purple
Glass Lunch Box Containers, Glass Bento Box Containers, Leakproof Glass Food Storage, Stacking Tiffin Box, Glass Lunch Box, Heat Resistant Glass Storage Container, Lunchbox Glass Food Container, Bentgo Glass Snack Container, Transparent Glass Lunch Container
Glass Lunch Box Containers, Glass Bento Box Containers, Leakproof Glass Food Storage, Stacking Tiffin Box, Glass Lunch Box, Heat Resistant Glass Storage Container, Lunchbox Glass Food Container, Bentgo Glass Snack Container, Transparent Glass Lunch Container
Flatpack Rectangle Low Planter by Green Theory
Flatpack Rectangle Low Planter by Green Theory
COOK WITH COLOR Premium 32-Pc. Borosilicate Glass Food Container Set with Dividers - 4 Rectangles, 8 Rounds, 4 Squares - Leakproof Lids - Meal Prep, Storage, Pink
COOK WITH COLOR Premium 32-Pc. Borosilicate Glass Food Container Set with Dividers - 4 Rectangles, 8 Rounds, 4 Squares - Leakproof Lids - Meal Prep, Storage, Pink

BorderSide Property

To add a border to your rounded rectangle, use the borderSide property. Here's an example:

```dart Container( decoration: BoxDecoration( border: Border.all( color: Colors.black, width: 2, ), borderRadius: BorderRadius.all(Radius.circular(10)), ), ) ```

Applying Gradient with Bordered Rounded Corner

Flutter's BoxDecoration also allows applying gradients to your rounded rectangle. Use the gradient property and pass in a LinearGradient or RadialGradient object.

AD

Here's an example of applying a linear gradient to a rounded rectangle with a border:

an overhead view of a white and black object
an overhead view of a white and black object
Set of 4 Girls' Snack Container - Pillowfort™
Set of 4 Girls' Snack Container - Pillowfort™
Floral Column Storage Jar Silicone Mold - Medium
Floral Column Storage Jar Silicone Mold - Medium
a large amount of containers stacked on top of each other
a large amount of containers stacked on top of each other
Butterfly Meadow Large Round Container
Butterfly Meadow Large Round Container
a person is holding a plastic container with food in it on a table next to some glasses
a person is holding a plastic container with food in it on a table next to some glasses
there are many containers stacked on top of each other in the shape of a rainbow arch
there are many containers stacked on top of each other in the shape of a rainbow arch
Horplkj Rectangular Wavy Edge Flowerpot, Large Plastic Planter with Ruffled Rim, for Balcony & Garden White 30.2*13.2*13.7cm, Size:30.2*13.2*13.7cm
Horplkj Rectangular Wavy Edge Flowerpot, Large Plastic Planter with Ruffled Rim, for Balcony & Garden White 30.2*13.2*13.7cm, Size:30.2*13.2*13.7cm
```dart Container( decoration: BoxDecoration( border: Border.all( color: Colors.black, width: 2, ), borderRadius: BorderRadius.all(Radius.circular(10)), gradient: LinearGradient( colors: [Colors.blue, Colors.red], begin: Alignment.topLeft, end: Alignment.bottomRight, ), ), ) ```

In Flutter, the Container widget with its BoxDecoration, borderRadius, and border properties provides a robust and flexible way to create rounded rectangles with borders and gradients. This flexibility is key to creating visually appealing and customizable user interfaces in your Flutter apps.

AD