Adding a circle in Flutter is typically achieved using the Container
widget with a specific decoration.
Creating a Circular Container
The most straightforward way to create a circular shape in Flutter is by utilizing the Container
widget and its decoration
property. Creating a circular container in Flutter is a straightforward process.
The key is to use the decoration property of the container widget and set the shape to BoxShape.circle.
Here's a breakdown of the essential steps:
- Use a
Container
Widget: This widget allows you to define areas with dimensions, padding, margins, and decorations. - Set the
decoration
Property: Assign aBoxDecoration
object to thedecoration
property of theContainer
. - Specify the
shape
: Inside theBoxDecoration
, set theshape
property toBoxShape.circle
. This tells Flutter to draw the container as a circle (or ellipse if dimensions are unequal). - Define Size: Provide equal
width
andheight
to theContainer
to ensure it's a perfect circle rather than an ellipse. If only one dimension is provided, the other will often default or match, but explicitly setting both is clearer. - Add Color (Optional but common): Specify a
color
within theBoxDecoration
to fill the circle. Note: Do not set thecolor
property directly on theContainer
if you are usingdecoration
, as this will cause an error. - Add Content (Optional): The child property can be used to add content to the container.
Example Code
Here's a simple example of how to create a small blue circle:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Circle Example')),
body: Center(
child: Container(
width: 100.0,
height: 100.0,
decoration: BoxDecoration(
shape: BoxShape.circle, // This makes it a circle
color: Colors.blue, // Fill color
),
),
),
),
);
}
}
In this code:
- We create a
Container
withwidth
andheight
of 100.0. - We use the
decoration
property withBoxDecoration
. - Inside
BoxDecoration
, we setshape: BoxShape.circle
to define the circular form andcolor: Colors.blue
to give it a blue fill.
Key Properties for Circular Containers
Understanding the properties involved helps in customizing your circle:
Property | Applies To | Description | Usage |
---|---|---|---|
width |
Container |
Sets the width of the container. Equal to height for a perfect circle. |
width: 100.0 |
height |
Container |
Sets the height of the container. Equal to width for a perfect circle. |
height: 100.0 |
decoration |
Container |
Paints decoration behind the child. Requires a BoxDecoration object. |
decoration: BoxDecoration(...) |
shape |
BoxDecoration |
Defines the shape of the box. Set to BoxShape.circle for a circle. |
shape: BoxShape.circle |
color |
BoxDecoration |
The color to fill the box shape with. | color: Colors.red |
borderRadius |
BoxDecoration |
Defines rounded corners. Cannot be used when shape is BoxShape.circle . This property is relevant for creating rounded rectangles, and the borderRadius property can be used to create rounded corners, but not for perfect circles made with BoxShape.circle . |
borderRadius: BorderRadius.circular(10) (for rounded corners) |
child |
Container |
The widget below this one in the tree. Can be used to add content. | child: Icon(Icons.star) |
Using BoxShape.circle
is the most direct way to render a circular shape with Container
.