Flutter: Creating Rounded Containers with Ease
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().
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:
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.
The borderSide property accepts a BorderSide object with various properties. Set the color and width as per your design needs.
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.
Here's an example of applying a linear gradient to a rounded rectangle with a border:
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.