Flutter - Container Styling
Last Updated :
09 May, 2021
In this article we will look at the most basic and simple widget in flutter Container. We will look that how can we style our container in different ways according to our need , so that we can effectively use this widget in our app .
First of all we have to write some starting code for our project . Our code for the structure of our app will go like :-
Dart
import 'package:flutter/material.dart';
void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(),
);
}
}
We had created a stateless widget called HomePage in our home property of Material App . So when we run our app we do not see anything created on our screen .
This is because a container itself is an empty body until some constraints are provided . By constraints here we mean some height or width should be given to our container . Also assign some color to it so that it can be reflected in our app.
Dart
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height: 200.0,
width: 200.0,
color: Colors.blueAccent
)
)
);
}
}
NOTE :- We have to assign color property inside BoxDecoration constructor only else it will create an error .
Using decoration property of Container :
Decoration property takes an argument of type Box Decoration . Now we can see that multiple properties can be applied to our container .
1. Border : - We will assign some border to our container using Border.all constructor. Also provide it some width ,color and style properties .
Dart
border: Border.all(
color: Colors.black,
width: 2.0,
),
2. Border Radius :- This is used when we have to structure our container other than a default rectangular one . We can provide some radius to our container so that it can have a rounded border . This property is used many times to provide some uniqueness to our UI .
Dart
borderRadius: BorderRadius.circular(10.0)
3. Image : - As the name suggests if we want to add some image to our container we can use it with Decoration Image constructor . Image can be added in 2 ways . One by adding into our assets and another by fetching from internet .
4. Box Shadow :- To provide some shadow to our container this can be used . Actually it accepts a list of object of type Box Shadow . We can provide some color and blur radius to it .
Dart
boxShadow: [
BoxShadow(
color: Colors.grey ,
blurRadius: 2.0,
offset: Offset(2.0,2.0)
)
]
5. Shape : - If for some reasons we do not want to hard code the value of radius inside Border Radius property we can use this property by assigning it circle value . (Note : - Both can not be used simultaneously.)
Dart
6. Gradient :- If we wish to provide some multiple or combination of colors to our container we can use this property . A gradient of type Linear Gradient is assigned to it and colors are provided in form of list .
Dart
gradient: LinearGradient(
colors: [
Colors.indigo,
Colors.blueAccent
]
),
Complete code for our app :
Dart
import 'package:flutter/material.dart';
void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height: 200.0,
width: 200.0,
decoration: BoxDecoration(
color: Colors.blueAccent,
border: Border.all(
color: Colors.black,
width: 2.0,
),
borderRadius: BorderRadius.circular(10.0),
gradient: LinearGradient(
colors: [
Colors.indigo,
Colors.blueAccent
]
),
boxShadow: [
BoxShadow(
color: Colors.grey ,
blurRadius: 2.0,
offset: Offset(2.0,2.0)
)
]
),
),
),
);
}
}
Output:
So in this article we saw that how we can style our container in different and best ways to make some better UIs .
Similar Reads
Container class in Flutter
Container class in flutter is a convenience widget that combines common painting, positioning, and sizing of widgets. A Container class can be used to store one or more widgets and position them on the screen according to our convenience. Basically, a container is like a box to store contents. A bas
8 min read
Shimmer Container in Flutter
A Shimmer Container is the fade-in and fade-out effect, We can show it instead using CircularProgressIndicator or Linear progress indicator that looks decent. While fetching the data from the API or from the database we need to wait as it is an asynchronous process, we need to do something on the us
3 min read
Flutter - AnimatedContainer Widget
In Flutter a container is a simple widget with well-defined properties like height, width, and color, etc. The AnimatedContainer widget is a simple container widget with animations. These types of widgets can be animated by altering the values of their properties which are the same as the Container
3 min read
Flutter - ConstrainedBox Widget
ConstrainedBox is a built-in widget in flutter SDK. Its function is to add size constraints to its child widgets. It comes quite handy if we want a container or image to not exceed a certain height and width. It is also good to keep text in a wrapped layout by making the Text widget a child on Const
2 min read
Center widget in Flutter
Center widget comes built-in with flutter, it aligns its child widget to the center of the available space on the screen. The size of this widget will be as big as possible if the widthFactor and heightFactor properties are set to null and the dimensions are constrained. And in case the dimensions a
2 min read
Flutter - BoxConstraints Widget
BoxConstraints is a built-in widget in flutter SDK. Its functionality is to add sized constraints on its child widget. It is usually taken as the object of constraints property in ConstrainedBox widget. It comes packed with many properties for customization. Below we will see all its properties with
5 min read
Flutter - Banner Widget
Banner widget comes built-in with flutter API. It is somewhat similar to the debug banner that we are used to seeing on the top-right corner on a flutter app in debug mode. It enables us to show a message or text on top of any other widget. Below we will see its implementation with the help of an ex
3 min read
Flutter - Card Widget
Card is a built-in widget in Flutter which derives its design from Google's Material Design Library. The functionality of this widget on screen is, that it is a bland space or panel with round corners and a slight elevation on the lower side. It comes with many properties like color, shape, shadow c
4 min read
Clipping in Flutter
In computer graphics, the act of restricting the rendering to a particular area is called Clipping. As developers, we use clipping to create awesome-looking, custom user interfaces with special effects. We have two types of Clipping that are ClipOval and ClipRRect. ClipOval A widget that clips its c
2 min read
Flutter - Spin Bottle Animation
In Flutter, Spin Bottle Animation is a fun and attractive animation, here we can achieve this by using the Flutter RotationTransition and AnimatedBuilder widget, In this animation, a bottle is rotated when we press a button. In this article, we are going to implement the Spin Bottle Animation using
5 min read