Create a rounded button / button with border-radius in Flutter
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Flutter is a powerful open-source framework for building natively compiled applications for mobile, web, and desktop from a single codebase. One common UI element in Flutter applications is the rounded button, which can be created using various built-in widgets and properties. This article delves into how to create a rounded button in Flutter and explores the technical aspects of using border-radius.
Creating a Rounded Button in Flutter
In Flutter, the most straightforward way to create a rounded button is by using the ElevatedButton widget, which is a material design button. You can modify its appearance by specifying the style parameter.
Example
Below is a basic example demonstrating how to create an ElevatedButton with rounded corners:
Explanation
ElevatedButton: A material design elevated button. It provides the appearance of a button that is lifted above most elements on the screen, giving a 3D effect.onPressed: The callback that is called when the button is tapped.style: Defines the button's appearance, including its shape, color, and more.RoundedRectangleBorder: A shape with rounded corners defined byBorderRadius.BorderRadius.circular(30.0): Creates a circular border with a radius. This value determines the amount of roundness. A higher value results in more rounded corners.
Detailed Technical Explanation
BorderRadius in Flutter
BorderRadius is a property that defines the rounding of corners in a Container, BoxDecoration, or, as shown above, a ButtonStyle. The widget BorderRadius provides multiple constructors:
BorderRadius.circular(radius): Creates a border radius where all the corners have the same radius.BorderRadius.horizontal(left, right): Allows specifying different radii for the top and bottom corners on the left and right sides.BorderRadius.vertical(top, bottom): Allows specifying different radii for the left and right corners at the top and bottom.BorderRadius.only(topLeft, topRight, bottomLeft, bottomRight): Allows you to set different radius values for all four corners separately.
Applying BorderRadius in Different Widgets
While this article focuses on buttons, BorderRadius can be used in any widget that supports the decoration or shape property like Container, Card, and DecoratedBox.
Other Button Widgets
TextButton: A button that displays text with a click action. You can similarly applyBorderRadiususingstyle.OutlinedButton: Similar toTextButton, but with an outline border. UseOutlinedButton.styleFromand applyBorderRadius.IconButton: For icon-based buttons. This can be customized for circular or round shapes using other layout widgets likeClipRRect.
Comparisons and Features Summary
The table below summarizes the features of different button types and their customization options using BorderRadius:
| Button Type | Default Style | Customization Option | Use Case |
| ElevatedButton | Material design, raised button | style: ElevatedButton.styleFrom | Prominent actions |
| TextButton | Flat button, usually for text actions | style: TextButton.styleFrom | Less prominent text actions |
| OutlinedButton | Border only button, no fill | style: OutlinedButton.styleFrom | Actions requiring separation |
| IconButton | Tapable icon | Use with ClipRRect for rounding | Icon-based interactions |
Additional Applications
Using rounding can greatly enhance the aesthetics of a user interface by aligning with design trends like soft UI or neumorphism, where shadow and lighting play to create subtle but appealing 3D effects.
Conclusion
Creating rounded buttons in Flutter is accessible through modification of button styles with BorderRadius. This customization allows developers to adhere to their desired design patterns easily, whether they are building for mobile, web, or desktop. The use of BorderRadius extends beyond buttons to any widgets requiring customized corner implications, providing flexible UI design capabilities in Flutter applications.

