A Container widget helps you group and style parts of your app's screen. It can add space, color, borders, and size to its child widget.
Container widget in Flutter
Container( width: double?, height: double?, padding: EdgeInsets?, margin: EdgeInsets?, decoration: BoxDecoration?, child: Widget? )
width and height set the size of the container.
padding adds space inside the container around the child.
Container(
color: Colors.blue,
child: Text('Hello')
)Container( width: 100, height: 100, margin: EdgeInsets.all(10), decoration: BoxDecoration( color: Colors.red, borderRadius: BorderRadius.circular(8), ), child: Icon(Icons.star, color: Colors.white), )
This app shows a green square container with rounded corners and a black border. Inside, there is white text centered with padding around it. The container also has margin space outside it.
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Container Example')), body: Center( child: Container( width: 150, height: 150, padding: const EdgeInsets.all(20), margin: const EdgeInsets.all(30), decoration: BoxDecoration( color: Colors.green, borderRadius: BorderRadius.circular(15), border: Border.all(color: Colors.black, width: 3), ), child: const Center( child: Text( 'Hi Flutter!', style: TextStyle(color: Colors.white, fontSize: 18), textAlign: TextAlign.center, ), ), ), ), ), ); } }
If you only set color on Container, it applies directly. But if you use decoration, set color inside BoxDecoration.
Container tries to be as big as possible if no size is set. Use width and height to control size.
Use padding to add space inside the container, and margin to add space outside it.
Container is a box that holds and styles one child widget.
You can control size, color, padding, margin, and borders with Container.
It helps organize and decorate your app's layout easily.