What is BorderRadius?
BorderRadius is a class in Flutter that defines the rounding of corners of a rectangle. It is used with BoxDecoration, ShapeDecoration, ClipRRect, RoundedRectangleBorder, and other widgets that accept a BorderRadiusGeometry. Understanding how to round corners is essential for creating modern, visually appealing UI designs, such as cards, buttons, images, and dialogs.
BorderRadius.circular – Uniform Rounding
The simplest way to create rounded corners is BorderRadius.circular(radius). It applies the same radius to all four corners.
BorderRadius.only – Custom Corner Rounding
Use BorderRadius.only when you need different radii for different corners. The topLeft, topRight, bottomLeft, bottomRight parameters take Radius objects.
BorderRadius.vertical and BorderRadius.horizontal
For rounding only the top or bottom corners, use BorderRadius.vertical. Similarly, BorderRadius.horizontal rounds the left or right corners.
Applying BorderRadius to Different Widgets
BorderRadius is not limited to Container. You can use it with:
Card: Theshapeproperty accepts aRoundedRectangleBorderwithborderRadius.
ClipRRect: Rounds the corners of any child widget (e.g., images).
Image: Wrap withClipRRectto round image corners.
Button: UseElevatedButton.styleFromwithshape(requires aRoundedRectangleBorder).
Dialog: Useshapeproperty ofAlertDialogorDialog.
ExpansionTile, etc.
Advanced: Using Radius with Circle
Radius can also be created with Radius.elliptical(x, y) for non‑uniform radii. This is useful for more complex shapes, but usually Radius.circular is sufficient.
Common Mistakes
- Using
BorderRadiusdirectly onClipRRect:ClipRRectexpects aBorderRadiusGeometry, whichBorderRadiusimplements. That's fine. But forgetting to setclipBehaviortoClip.antiAliascan result in jagged edges.
- Using
- Applying
BorderRadiusto aContainerwithoutBoxDecoration: TheborderRadiusproperty only exists insideBoxDecoration. You cannot setborderRadiusdirectly on aContainer.
- Applying
- Mixing
BorderRadius.circularwithRadius:BorderRadius.circularaccepts a double, whileBorderRadius.onlyrequiresRadiusobjects. They are different; don't mix them incorrectly.
- Mixing
- Using too large radii: A radius larger than half the width/height will produce a pill shape; be intentional about your design.
- Forgetting to use
ClipRRectfor images:Imagedoes not have a built‑in borderRadius; you must wrap it withClipRRector useContainerwith decoration.
- Forgetting to use
Best Practices
- For consistent design, define a constant for the corner radius (e.g.,
const kBorderRadius = 12.0).
- For consistent design, define a constant for the corner radius (e.g.,
- Use
ClipRRectfor clipping images; combine withImage.networkfor smooth corners.
- Use
- For interactive widgets (buttons, cards), ensure the radius is consistent with the Material Design guidelines (e.g., buttons often use 30dp radius).
- When creating custom shapes, prefer
RoundedRectangleBorderover directly applying aBorderRadiusto aClipPathunless you need irregular shapes.
- When creating custom shapes, prefer
Key Takeaways
BorderRadius.circularrounds all corners equally.
BorderRadius.onlylets you set different radii per corner.
BorderRadius.vertical/horizontalround top/bottom or left/right corners.
- Apply to
ContainerviaBoxDecoration, toCardviashape, to images viaClipRRect, and to buttons viaElevatedButton.styleFrom.
- Apply to
- Use
Radius.ellipticalfor non‑circular rounding.
- Use
- Always test on different screen sizes to avoid unexpected clipping.