Flutter
UI Design
Rounded Button
Border Radius
Mobile Development

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.

Introduction

Creating aesthetically pleasing user interfaces is a critical component of mobile app development. Flutter, Google's UI toolkit for crafting natively compiled applications for mobile, web, and desktop, offers extensive customization options. Among these, designing buttons with rounded corners using the borderRadius property is essential for creating modern interfaces. This article will explore how to create these rounded buttons in Flutter, enriched with detailed technical explanations and examples.

The Basics of Button Styling in Flutter

Flutter provides several built-in widgets for creating buttons, such as ElevatedButton, OutlinedButton, and TextButton. Each offers different styling options, but they all share a common property called style, which can be used to customize their appearance:

  • ElevatedButton: A material design button typically used for prominently showing primary actions.
  • OutlinedButton: A button with an outline border that becomes opaque when pressed.
  • TextButton: A button without any border that becomes opaque when pressed.

All these buttons can be styled using the parameter ButtonStyle, which lets you define textStyle, backgroundColor, foregroundColor, overlayColor, shadowColor, elevation, padding, minimumSize, side, shape, and more.

Creating a Rounded Button

To create a rounded button, you need to make use of the shape property within ButtonStyle. The shape attribute utilizes RoundedRectangleBorder allowing you to define the borderRadius.

Here's an example of how to create a rounded button using the ElevatedButton widget in Flutter:

dart
1import 'package:flutter/material.dart';
2
3void main() {
4  runApp(MyApp());
5}
6
7class MyApp extends StatelessWidget {
8  
9  Widget build(BuildContext context) {
10    return MaterialApp(
11      home: Scaffold(
12        appBar: AppBar(
13          title: Text('Rounded Button Example'),
14        ),
15        body: Center(
16          child: ElevatedButton(
17            onPressed: () {},
18            style: ElevatedButton.styleFrom(
19              shape: RoundedRectangleBorder(
20                borderRadius: BorderRadius.circular(30.0), // Radius of the corners
21              ),
22            ),
23            child: Text('Rounded Button'),
24          ),
25        ),
26      ),
27    );
28  }
29}

Detailed Explanation

Widgets and Properties

  • ElevatedButton: A button made prominent through elevation and background color.
  • RoundedRectangleBorder: Used to apply rounded corners to the button.
    • borderRadius: Defines the radius of the corners. In this example, BorderRadius.circular(30.0) is used, indicating that each corner should be 30 pixels in radius.

ButtonStyle Customization

Using the ButtonStyle widget provides more control over button customization. Here's an example:

dart
1ElevatedButton(
2  onPressed: () {},
3  style: ButtonStyle(
4    shape: MaterialStateProperty.all<RoundedRectangleBorder>(
5      RoundedRectangleBorder(
6        borderRadius: BorderRadius.circular(18.0),
7      ),
8    ),
9    backgroundColor: MaterialStateProperty.all<Color>(Colors.blue),
10    foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
11  ),
12  child: Text('Customized Rounded Button'),
13)

Enhancing User Interface

Customizing buttons can significantly enhance user interaction. Consider the following tactics:

  • Color Schemes: Align button colors with your application's theme.
  • Elevation and Shadows: Use elevation to give depth to buttons, making them appear more interactive.
  • Padding and Sizing: Ensure appropriate padding and size to enhance usability.

Summary Table

ParameterProperty/ClassDescription
Button TypeElevatedButton, OutlinedButton, TextButtonWidgets to create buttons with different styles
Style PropertyButtonStyleAllows customization of button appearance
Shape for RoundedRoundedRectangleBorderClass to round the corners of a button
BorderRadiusBorderRadius.circular(value)Sets the corner radius, where value is in pixels
Color CustomizationbackgroundColor foregroundColorDefines colors for button and text

Conclusion

Creating a button with a border-radius in Flutter is straightforward, leveraging the flexible styling capabilities through its ButtonStyle and shape properties. By understanding and utilizing these properties, developers can create highly customized and visually appealing buttons that enhance user experience in their applications. Customize further with colors, sizes, and shadows for even more personalized designs.


Course illustration
Course illustration

All Rights Reserved.