logo

Education

Flutter BorderRadius: Crafting Modern UIs with Rounded Corners

Authore Name
Kesar Bhimani

Software Development Executive - I

Last updated on Oct 26, 2023

In Flutter, the BorderRadius widget is a fundamental tool for creating visually appealing UI elements. This widget, part of the BoxDecoration class, allows developers to set the border radius of container widgets, giving them rounded corners.

The BorderRadius class provides several methods to create and manipulate the border radius, including BorderRadius.all, BorderRadius.circular, BorderRadius.only, and BorderRadius.vertical. These methods offer flexibility in shaping the corners of a container, whether you want all corners to be uniformly rounded or prefer different radii for each corner.

Importance of BorderRadius Widget in Flutter

Role of BorderRadius Widget in UI Design

In the realm of Flutter, the BorderRadius widget plays a crucial role in UI design. It is a part of the BoxDecoration class, which is used to set the border radius of container widgets. The BorderRadius class provides us with various ways to create and manipulate the rounded corners of a container.

For instance, we can use the BorderRadius.circular method to create a circular border radius, which is quite common in modern UI design. The BorderRadius.only method allows us to set the border radius for specific corners, providing more flexibility in design.

1 Container( 2 width: 200.0, 3 height: 200.0, 4 decoration: BoxDecoration( 5 color: Colors.blue, 6 borderRadius: BorderRadius.only( 7 topLeft: Radius.circular(15.0), 8 bottomRight: Radius.circular(15.0), 9 ), 10 ), 11 ) 12

How BorderRadius Widget Enhances Aesthetics of an Application

The BorderRadius widget significantly enhances the aesthetics of a Flutter application. By setting the border radius of container widgets, we can create visually pleasing UI elements with rounded corners. This is particularly useful when we want to soften the look of a container border or when we want to create a specific shape, such as a circle or an oval.

Moreover, the BorderRadius property of the BoxDecoration class allows us to control the curvature of the container border, which can add a sophisticated touch to the overall design.

For example, the following code snippet demonstrates how to use the BorderRadius.all method to set the border radius for all corners of a container widget:

1 Container( 2 width: 200.0, 3 height: 200.0, 4 decoration: BoxDecoration( 5 color: Colors.blue, 6 borderRadius: BorderRadius.all(Radius.circular(15.0)), 7 ), 8 ) 9

In this example, the BorderRadius.all method is used with the Radius.circular method to create a consistent border radius for all corners of the container. The result is a container widget with beautifully rounded corners, enhancing the aesthetics of the Flutter app.

How to Use BorderRadius Widget in Flutter?

Step 1: Create a New Flutter Application

To start, we need to create a new Flutter application. In the main.dart file, we will define the 'void main' method which is the entry point of our Flutter app.

Step 2: Define the StatefulWidget

Next, we define the StatefulWidget. We will use the 'class MyStatefulWidget' and 'const MyStatefulWidget' keywords to define our StatefulWidget. The StatefulWidget class is a key part of the Flutter framework, and it allows us to create widgets that can change over time.

Step 3: Use the Build Method

In the 'class MyStatefulWidget', we use the build method, which takes a 'BuildContext context' as a parameter. This method is responsible for describing the part of the user interface represented by the widget.

Step 4: Create a Container Widget

Now, we will create a container widget. The container widget is a convenience widget that combines common painting, positioning, and sizing widgets. We will set the width and height of the container using the 'width' and 'height' properties.

Step 5: Set the BorderRadius

Next, we will set the border radius of the container. To set border radius, we use the 'borderRadius' property in the 'BoxDecoration' class. The 'BoxDecoration' class describes a box decoration. We can set the border color, shape, and other properties using this class.

1 Container( 2 width: 200.0, 3 height: 200.0, 4 decoration: BoxDecoration( 5 color: Colors.blue, 6 borderRadius: BorderRadius.circular(10.0), 7 border: Border.all( 8 color: Colors.black, 9 width: 3.0, 10 ), 11 ), 12 ) 13

In the code snippet above, we have set the border radius to 10.0 using the BorderRadius.circular() constructor. We have also set the border color to black and the width to 3.0 using the 'Border.all()' constructor.

Step 6: Add Padding and Child Widgets

Finally, we will add padding to the container using the 'const EdgeInsets' class. We will also add child widgets to the container. The child widget can be any widget, such as 'const Text', 'const SizedBox', or another container widget.

1 Container( 2 padding: const EdgeInsets.all(10.0), 3 width: 200.0, 4 height: 200.0, 5 decoration: BoxDecoration( 6 color: Colors.blue, 7 borderRadius: BorderRadius.circular(10.0), 8 border: Border.all( 9 color: Colors.black, 10 width: 3.0, 11 ), 12 ), 13 child: const Text( 14 'Hello, Flutter!', 15 style: TextStyle(fontSize: 24.0), 16 ), 17 ) 18

In the code snippet above, we have added padding to the container using the 'const EdgeInsets.all()' constructor. We have also added a 'const Text' widget as a child of the container.

Understanding the BorderRadius Widget in Detail

Explanation of BorderRadius Widget Properties

The BorderRadius widget in Flutter has several properties that allow us to control the shape and curvature of the container border. These properties include topLeft, topRight, bottomLeft, and bottomRight, each representing a corner of the container. By setting these properties, we can control the radius of each corner individually.

1 Container( 2 decoration: BoxDecoration( 3 borderRadius: BorderRadius.only( 4 topLeft: Radius.circular(10.0), 5 bottomRight: Radius.circular(20.0), 6 ), 7 ), 8 ) 9

Understanding BorderRadius.all, BorderRadius.circular, BorderRadius.only, and BorderRadius.vertical

The BorderRadius class provides several methods to set the border radius:

  • BorderRadius.all: This method sets the same radius for all corners of the container. It takes a Radius object as a parameter.
  • BorderRadius.circular: This method is a shortcut for BorderRadius.all(Radius.circular(radius)). It sets the same circular radius for all corners.
  • BorderRadius.only: This method allows us to set the radius for specific corners. We can specify the radius for topLeft, topRight, bottomLeft, and bottomRight.
  • BorderRadius.vertical: This method creates a vertically symmetric border radius where the top and bottom sides of the rectangle have the same radii.
1 Container( 2 decoration: BoxDecoration( 3 borderRadius: BorderRadius.vertical( 4 top: Radius.circular(10.0), 5 bottom: Radius.circular(20.0), 6 ), 7 ), 8 ) 9

Use of Radius in BorderRadius

The Radius class in Flutter is used in conjunction with the BorderRadius class to define the curvature of the container border. We can use Radius.circular to create a circular radius, or Radius.elliptical to create an elliptical radius. The Radius.circular method is commonly used with BorderRadius to create rounded corners for container widgets.

1 Container( 2 decoration: BoxDecoration( 3 borderRadius: BorderRadius.all(Radius.circular(15.0)), 4 ), 5 ) 6

In this code snippet, Radius.circular(15.0) creates a circular radius of 15.0 for all corners of the container. This results in a container with uniformly rounded corners.

Level up Your Application with Flutter BorderRadius Widget!

In conclusion, the BorderRadius widget is a powerful tool in the Flutter framework that allows developers to create visually appealing UI elements with rounded corners. Understanding and effectively using this widget can significantly enhance the aesthetics of your Flutter application.

Throughout this guide, we've explored the importance of the BorderRadius widget, delved into its properties, and provided examples of its implementation. We hope this has provided you with a solid understanding of how to use the BorderRadius widget in your Flutter projects.

Short on time? Speed things up with DhiWise!!

Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!

You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.

Sign up to DhiWise for free

Read More