How Flutter really draws your screen · 4 h
Make every word look right · 4 h
Cards, gradients, shadows and shapes · 5 h
Place everything exactly where you want · 5 h
A small description of a piece of screen: Text, Padding, Row, your own ProfileCard.
The parent tells the child how big it may be. The child picks a size inside that.
A name tag that helps Flutter match old widgets with new ones when a list changes.
In Flutter a screen is built by putting small widgets inside bigger widgets. The result is a tree: one root at the top and branches below it. A Text inside a Padding inside a Center inside a Scaffold is a four-level tree.
void main() {
runApp(const MaterialApp(
home: Scaffold( // level 1
body: Center( // level 2
child: Padding( // level 3
padding: EdgeInsets.all(24),
child: Text('Namaste Ranchi', // level 4
style: TextStyle(fontSize: 32)),
),
),
),
));
}When a piece of screen repeats, put it in its own class. A StatelessWidget receives values through its constructor and returns a widget tree from build().
class ShopCard extends StatelessWidget {
final String name;
final int price;
const ShopCard(this.name, this.price, {super.key});
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
leading: const Icon(Icons.store),
title: Text(name),
trailing: Text('Rs $price'),
),
);
}
}
void main() {
runApp(const MaterialApp(home: Scaffold(
body: Column(children: [
ShopCard('Dhuska plate', 40),
ShopCard('Litti chokha', 60),
]),
)));
}This one rule explains almost every layout surprise.
void main() {
runApp(MaterialApp(home: Scaffold(
body: Column(children: [
// no Center: Scaffold body is tight, Container fills it
Container(color: Colors.red, height: 60),
// inside Center the constraints become loose
Center(child: Container(color: Colors.green, width: 120, height: 60)),
// ConstrainedBox limits the maximum width
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 200),
child: Container(color: Colors.blue, height: 60),
),
]),
)));
}context is the address of your widget inside the tree. Flutter uses it to look upwards: the nearest Theme, the screen size, the nearest Navigator.
class Info extends StatelessWidget {
const Info({super.key});
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
final color = Theme.of(context).colorScheme.primary;
return Center(
child: Text('Screen ${size.width.toInt()} x ${size.height.toInt()}',
style: TextStyle(fontSize: 24, color: color)),
);
}
}
void main() {
runApp(const MaterialApp(home: Scaffold(body: Info())));
}When a list is reordered, Flutter compares old and new widgets by type and position. Stateful widgets can then keep the wrong state. A ValueKey tells Flutter which widget is which.
Use a unique value such as an id or name.
A new key every build. Forces a fresh widget.
Reach a widget from far away (Forms use it).
For a static list that never reorders.
void main() {
runApp(const MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Ranchi Weather')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.wb_sunny, size: 64, color: Colors.orange),
Text('28 C, sunny', style: TextStyle(fontSize: 28)),
],
),
),
),
));
}class ShopCard extends StatelessWidget {
final String name;
final int price;
const ShopCard(this.name, this.price, {super.key});
@override
Widget build(BuildContext context) => Card(
margin: const EdgeInsets.all(12),
child: ListTile(
leading: const Icon(Icons.store, color: Colors.teal),
title: Text(name),
subtitle: const Text('Main Road, Ranchi'),
trailing: Text('Rs $price', style: const TextStyle(fontWeight: FontWeight.bold)),
),
);
}
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: ShopCard('Dhuska plate', 40)))));class ShopCard extends StatelessWidget {
final String name, item;
final int price;
const ShopCard(this.name, this.item, this.price, {super.key});
@override
Widget build(BuildContext context) => Card(
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
child: ListTile(
title: Text(name),
subtitle: Text(item),
trailing: Text('Rs $price'),
),
);
}
void main() => runApp(const MaterialApp(home: Scaffold(
body: Column(children: [
SizedBox(height: 24),
ShopCard('Albert Ekka Chowk Sweets', 'Rasgulla 1 kg', 320),
ShopCard('Kanke Road Tea Stall', 'Kulhad chai', 15),
ShopCard('Firayalal Chowk Snacks', 'Samosa plate', 30),
]),
)));void main() {
runApp(MaterialApp(home: Scaffold(
body: Column(children: [
Expanded(child: Container(color: Colors.red.shade200,
child: const Center(child: Text('A: no size, fills its area')))),
Expanded(child: Center(child: Container(
width: 160, height: 80, color: Colors.green.shade400,
child: const Center(child: Text('B: 160 x 80'))))),
]),
)));
}class Banner1 extends StatelessWidget {
const Banner1({super.key});
@override
Widget build(BuildContext context) {
final w = MediaQuery.of(context).size.width;
return Container(
width: w * 0.8, padding: const EdgeInsets.all(16),
color: Theme.of(context).colorScheme.primaryContainer,
child: Text('80% of ${w.toInt()} pixels',
style: Theme.of(context).textTheme.titleLarge),
);
}
}
void main() => runApp(MaterialApp(
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange)),
home: const Scaffold(body: Center(child: Banner1())),
));class Play extends StatefulWidget {
const Play({super.key});
@override
State<Play> createState() => _PlayState();
}
class _PlayState extends State<Play> {
double maxW = 200;
@override
Widget build(BuildContext context) => Scaffold(
body: Column(children: [
Slider(value: maxW, min: 50, max: 340, onChanged: (v) => setState(() => maxW = v)),
Text('maxWidth = ${maxW.toInt()}'),
const SizedBox(height: 16),
ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxW),
child: Container(width: 300, height: 70, color: Colors.indigo,
child: const Center(child: Text('I want 300', style: TextStyle(color: Colors.white)))),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Play()));class Tile extends StatefulWidget {
final String label;
const Tile(this.label, {super.key});
@override
State<Tile> createState() => _TileState();
}
class _TileState extends State<Tile> {
int n = 0;
@override
Widget build(BuildContext context) => ListTile(
title: Text('${widget.label}: $n taps'),
onTap: () => setState(() => n++),
);
}
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
List<String> names = ['Dhuska', 'Litti', 'Thekua'];
@override
Widget build(BuildContext context) => Scaffold(
body: Column(children: [
for (final s in names) Tile(s, key: ValueKey(s)),
ElevatedButton(onPressed: () => setState(() => names = names.reversed.toList()), child: const Text('Reverse')),
]),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => _PageState();
}
class _PageState extends State<Page> {
double w = 300;
@override
Widget build(BuildContext context) => Scaffold(
body: Column(children: [
Slider(value: w, min: 120, max: 340, onChanged: (v) => setState(() => w = v)),
SizedBox(
width: w, height: 120,
child: LayoutBuilder(builder: (context, box) {
final wide = box.maxWidth > 220;
return Container(
color: wide ? Colors.teal.shade200 : Colors.orange.shade200,
child: Center(child: Text(wide ? 'Wide layout\n${box.maxWidth.toInt()} px' : 'Narrow\n${box.maxWidth.toInt()} px', textAlign: TextAlign.center)),
);
}),
),
]),
);
}
void main() => runApp(const MaterialApp(home: Page()));class Node {
final String name;
final List<Node> kids;
const Node(this.name, [this.kids = const []]);
}
class TreeRow extends StatelessWidget {
final Node node;
final int depth;
const TreeRow(this.node, this.depth, {super.key});
@override
Widget build(BuildContext context) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(left: 16.0 * depth, top: 4, bottom: 4),
child: Row(children: [
Icon(node.kids.isEmpty ? Icons.circle : Icons.folder, size: node.kids.isEmpty ? 10 : 20, color: Colors.teal),
const SizedBox(width: 8),
Text(node.name),
]),
),
for (final k in node.kids) TreeRow(k, depth + 1),
],
);
}
void main() {
const root = Node('Jharkhand', [
Node('Ranchi', [Node('Kanke'), Node('Namkum'), Node('Bundu')]),
Node('Hazaribagh', [Node('Barhi'), Node('Ichak')]),
Node('Dhanbad', [Node('Jharia')]),
]);
runApp(const MaterialApp(home: Scaffold(body: Padding(padding: EdgeInsets.all(16), child: TreeRow(root, 0)))));
}class StatTile extends StatelessWidget {
final String label, value;
final IconData icon;
final Color color;
const StatTile(this.label, this.value, this.icon, this.color, {super.key});
@override
Widget build(BuildContext context) => Container(
width: 150, padding: const EdgeInsets.all(14),
decoration: BoxDecoration(color: color.withOpacity(0.12), borderRadius: BorderRadius.circular(14)),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Icon(icon, color: color),
const SizedBox(height: 8),
Text(value, style: const TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
Text(label),
]),
);
}
class SectionTitle extends StatelessWidget {
final String text;
const SectionTitle(this.text, {super.key});
@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.fromLTRB(16, 20, 16, 8),
child: Text(text, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600)),
);
}
class Dash extends StatelessWidget {
const Dash({super.key});
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('DAV School Kanke')),
body: ListView(children: const [
SectionTitle('Today'),
Padding(padding: EdgeInsets.symmetric(horizontal: 16), child: Wrap(spacing: 12, runSpacing: 12, children: [
StatTile('Present', '412', Icons.people, Colors.green),
StatTile('Absent', '38', Icons.person_off, Colors.red),
StatTile('Fee due', 'Rs 1.2 L', Icons.currency_rupee, Colors.orange),
StatTile('Teachers', '27', Icons.school, Colors.blue),
])),
SectionTitle('Notices'),
ListTile(leading: Icon(Icons.campaign), title: Text('Chhath holidays from 6 Nov')),
]),
);
}
void main() => runApp(const MaterialApp(home: Dash()));Every screen is widgets inside widgets.
A StatelessWidget class is reusable and tidy.
Parent gives limits, child picks size, parent places it.
Tight has no choice; loose allows 0 to max.
Look up Theme and MediaQuery from the tree.
ValueKey keeps state with the right item.
Size, weight, colour, spacing and decoration of the text.
Bold, colour and links inside one sentence, such as "Rs 349 (MRP 499)".
A small set of ready sizes so every screen uses the same look.
Text shows words. TextStyle decides how they look. Every property is optional.
void main() {
runApp(const MaterialApp(home: Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
Text('Sharma Sweets', style: TextStyle(fontSize: 34, fontWeight: FontWeight.w800, color: Colors.deepOrange)),
Text('Since 1985, Main Road Ranchi', style: TextStyle(fontSize: 16, letterSpacing: 2, color: Colors.grey)),
SizedBox(height: 12),
Text('Rs 499', style: TextStyle(fontSize: 20, decoration: TextDecoration.lineThrough, color: Colors.grey)),
Text('Rs 349', style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold, color: Colors.green)),
])),
)));
}A TextSpan is a piece of text with its own style. Put several spans inside Text.rich and they flow as one paragraph.
void main() {
runApp(const MaterialApp(home: Scaffold(
body: Padding(
padding: EdgeInsets.all(20),
child: Center(
child: Text.rich(TextSpan(
style: TextStyle(fontSize: 22),
children: [
TextSpan(text: 'Order '),
TextSpan(text: '#1042', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' will reach '),
TextSpan(text: 'Kanke Road', style: TextStyle(color: Colors.blue)),
TextSpan(text: ' by '),
TextSpan(text: '6:30 PM', style: TextStyle(color: Colors.red, fontWeight: FontWeight.w700)),
],
)),
),
),
)));
}Material gives you named styles. Using them keeps every screen consistent and lets the user change the system font size.
Theme.of(context).textTheme.titleLarge instead of a fixed fontSize. Your whole app can then change its look in one place.class Scale extends StatelessWidget {
const Scale({super.key});
@override
Widget build(BuildContext context) {
final t = Theme.of(context).textTheme;
return Padding(padding: const EdgeInsets.all(16), child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('headlineMedium', style: t.headlineMedium),
Text('titleLarge', style: t.titleLarge),
Text('titleMedium', style: t.titleMedium),
Text('bodyLarge: normal reading text', style: t.bodyLarge),
Text('bodyMedium: default text', style: t.bodyMedium),
Text('labelSmall: caption', style: t.labelSmall),
],
));
}
}
void main() => runApp(const MaterialApp(home: Scaffold(body: Scale())));Phones are narrow. Decide what happens when text does not fit.
void main() {
const long = 'Ranchi Municipal Corporation has started a clean city drive across all 53 wards from Monday.';
runApp(const MaterialApp(home: Scaffold(
body: Padding(padding: EdgeInsets.all(16), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(long, maxLines: 1, overflow: TextOverflow.ellipsis),
SizedBox(height: 12),
Text(long, maxLines: 2, overflow: TextOverflow.ellipsis),
SizedBox(height: 12),
Text(long, textAlign: TextAlign.center),
])),
)));
}Flutter ships with Roboto on Android. For your own font, add the .ttf file in an assets/fonts folder, list it in pubspec.yaml, then use fontFamily. The package google_fonts loads hundreds of free fonts.
# pubspec.yaml
flutter:
fonts:
- family: Poppins
fonts:
- asset: assets/fonts/Poppins-Regular.ttf
- asset: assets/fonts/Poppins-Bold.ttf
weight: 700
// Dart
Text('Namaste', style: TextStyle(fontFamily: 'Poppins', fontSize: 28));void main() {
runApp(const MaterialApp(home: Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
Text('RANCHI BOOK HOUSE', style: TextStyle(fontSize: 30, fontWeight: FontWeight.w900, letterSpacing: 3, color: Color(0xFF0B3D91))),
Text('Books | Stationery | Gifts', style: TextStyle(fontSize: 16, color: Colors.grey)),
SizedBox(height: 20),
Text('Open 9 AM to 9 PM', style: TextStyle(fontSize: 18, color: Colors.green, fontWeight: FontWeight.w600)),
])),
)));
}void main() {
runApp(const MaterialApp(home: Scaffold(
body: Center(child: Row(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.end, children: [
Text('Rs 899', style: TextStyle(fontSize: 18, color: Colors.grey, decoration: TextDecoration.lineThrough)),
SizedBox(width: 10),
Text('Rs 599', style: TextStyle(fontSize: 34, fontWeight: FontWeight.bold, color: Colors.green)),
SizedBox(width: 10),
Text('33% off', style: TextStyle(fontSize: 16, color: Colors.red, fontWeight: FontWeight.w600)),
])),
)));
}void main() {
runApp(MaterialApp(home: Scaffold(
body: Padding(padding: const EdgeInsets.all(16), child: Card(child: Padding(padding: const EdgeInsets.all(16), child: Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: const [
Text('Jharkhand Foundation Day celebrations begin at Morabadi Ground with cultural programmes from all districts',
maxLines: 2, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
SizedBox(height: 8),
Text('Ranchi Samachar - 2 hours ago', style: TextStyle(fontSize: 13, color: Colors.grey)),
])))),
)));
}void main() {
runApp(MaterialApp(home: Builder(builder: (context) {
final t = Theme.of(context).textTheme;
return Scaffold(body: Padding(padding: const EdgeInsets.all(20), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text('My Orders', style: t.headlineMedium),
const SizedBox(height: 8),
Text('Order #1042', style: t.titleMedium),
Text('2 items delivered to Hinoo, Ranchi', style: t.bodyMedium),
const SizedBox(height: 4),
Text('Delivered on 18 Nov', style: t.labelSmall),
])));
})));
}void main() {
runApp(const MaterialApp(home: Scaffold(
body: Center(child: DefaultTextStyle(
style: TextStyle(fontSize: 22, color: Colors.indigo, fontWeight: FontWeight.w600),
child: Column(mainAxisSize: MainAxisSize.min, children: [
Text('Ranchi'),
Text('Patna'),
Text('Kolkata'),
Text('Plain one', style: TextStyle(color: Colors.black, fontWeight: FontWeight.normal)),
]),
)),
)));
}class Reader extends StatefulWidget {
const Reader({super.key});
@override
State<Reader> createState() => _ReaderState();
}
class _ReaderState extends State<Reader> {
double size = 16;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Reader')),
body: Padding(padding: const EdgeInsets.all(16), child: Column(children: [
Row(children: [
const Text('A', style: TextStyle(fontSize: 12)),
Expanded(child: Slider(value: size, min: 12, max: 32, onChanged: (v) => setState(() => size = v))),
const Text('A', style: TextStyle(fontSize: 26)),
]),
Text('Betla National Park in Latehar district is famous for tigers, elephants and thick sal forests. Many families from Ranchi visit it in winter.',
style: TextStyle(fontSize: size, height: 1.4)),
])),
);
}
void main() => runApp(const MaterialApp(home: Reader()));void main() {
runApp(MaterialApp(home: Scaffold(
body: Center(child: Container(
padding: const EdgeInsets.symmetric(horizontal: 28, vertical: 20),
decoration: BoxDecoration(color: Colors.red.shade700, borderRadius: BorderRadius.circular(16)),
child: const Column(mainAxisSize: MainAxisSize.min, children: [
Text('DIWALI', style: TextStyle(color: Colors.white, fontSize: 40, fontWeight: FontWeight.w900, letterSpacing: 6,
shadows: [Shadow(color: Colors.black54, blurRadius: 6, offset: Offset(2, 3))])),
Text('SALE', style: TextStyle(color: Colors.amber, fontSize: 56, fontWeight: FontWeight.w900, height: 0.9)),
SizedBox(height: 6),
Text('Up to 50% off - Main Road Ranchi', style: TextStyle(color: Colors.white70, fontSize: 14)),
]),
)),
)));
}class Terms extends StatefulWidget {
const Terms({super.key});
@override
State<Terms> createState() => _TermsState();
}
class _TermsState extends State<Terms> {
String msg = 'Tap a link';
@override
Widget build(BuildContext context) => Scaffold(
body: Padding(padding: const EdgeInsets.all(24), child: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
Text.rich(TextSpan(style: const TextStyle(fontSize: 18), children: [
const TextSpan(text: 'I agree to the '),
TextSpan(text: 'Terms', style: const TextStyle(color: Colors.blue, decoration: TextDecoration.underline),
recognizer: TapGestureRecognizer()..onTap = () => setState(() => msg = 'Opening Terms page')),
const TextSpan(text: ' and '),
TextSpan(text: 'Privacy Policy', style: const TextStyle(color: Colors.blue, decoration: TextDecoration.underline),
recognizer: TapGestureRecognizer()..onTap = () => setState(() => msg = 'Opening Privacy page')),
])),
const SizedBox(height: 20),
Text(msg),
]))),
);
}
void main() => runApp(const MaterialApp(home: Terms()));class Line extends StatelessWidget {
final String name; final int qty; final double price;
const Line(this.name, this.qty, this.price, {super.key});
@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Row(children: [
Text('$qty x ', style: const TextStyle(color: Colors.grey)),
Expanded(child: Text(name, maxLines: 1, overflow: TextOverflow.ellipsis)),
const SizedBox(width: 8),
Text('Rs ${(qty * price).toStringAsFixed(2)}', style: const TextStyle(fontWeight: FontWeight.bold)),
]),
);
}
void main() => runApp(const MaterialApp(home: Scaffold(
body: Padding(padding: EdgeInsets.all(20), child: Column(children: [
Text('Bill - Kanke Road Kirana', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
Divider(),
Line('Basmati rice premium long grain 5 kg bag', 1, 640),
Line('Mustard oil 1 L', 2, 165.5),
Line('Sugar', 3, 46),
Divider(),
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [Text('Total'), Text('Rs 1109.00', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18))]),
])),
)));class Search extends StatefulWidget {
const Search({super.key});
@override
State<Search> createState() => _SearchState();
}
class _SearchState extends State<Search> {
String q = 'ra';
final items = ['Ranchi', 'Patna', 'Raipur', 'Gaya', 'Dhanbad', 'Muzaffarpur', 'Bhagalpur'];
Widget hl(String t) {
final i = q.isEmpty ? -1 : t.toLowerCase().indexOf(q.toLowerCase());
if (i < 0) return Text(t);
return Text.rich(TextSpan(children: [
TextSpan(text: t.substring(0, i)),
TextSpan(text: t.substring(i, i + q.length), style: TextStyle(backgroundColor: Colors.yellow.shade300, fontWeight: FontWeight.bold)),
TextSpan(text: t.substring(i + q.length)),
]));
}
@override
Widget build(BuildContext context) => Scaffold(
body: Padding(padding: const EdgeInsets.all(16), child: Column(children: [
TextField(decoration: const InputDecoration(labelText: 'Search city', border: OutlineInputBorder()), onChanged: (v) => setState(() => q = v)),
const SizedBox(height: 12),
for (final c in items) if (c.toLowerCase().contains(q.toLowerCase())) ListTile(title: hl(c)),
])),
);
}
void main() => runApp(const MaterialApp(home: Search()));Size, weight, colour, spacing, decoration.
Many styles in one paragraph.
Use textTheme for consistent screens.
maxLines and ellipsis protect layouts.
TextSpan recognizer makes tappable words.
Split text into spans to mark search matches.
One widget with size, padding, margin and decoration.
Colour, rounded corners, border, shadow, gradient.
Cut a child into a circle, rounded rectangle or custom shape.
A Container draws a box. From outside to inside: margin (space around), border, padding (space inside) and the child.
decoration, put the colour inside the decoration. Using both color and decoration causes an error.void main() {
runApp(MaterialApp(home: Scaffold(
body: Center(
child: Container(
margin: const EdgeInsets.all(24), // outside space
padding: const EdgeInsets.all(20), // inside space
width: 240,
decoration: BoxDecoration(
color: Colors.amber.shade100,
border: Border.all(color: Colors.orange, width: 3),
borderRadius: BorderRadius.circular(16),
),
child: const Text('Chhath Puja special thali', style: TextStyle(fontSize: 20)),
),
),
)));
}Decoration is where Container becomes beautiful. Shadows lift a card off the page, gradients blend two colours, and BoxShape.circle makes a round box.
void main() {
runApp(MaterialApp(home: Scaffold(
body: Center(child: Row(mainAxisSize: MainAxisSize.min, children: [
Container(
width: 110, height: 110,
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(18),
boxShadow: [BoxShadow(color: Colors.black26, blurRadius: 12, offset: const Offset(0, 6))],
),
child: const Center(child: Text('Shadow')),
),
const SizedBox(width: 20),
Container(
width: 110, height: 110,
decoration: const BoxDecoration(
gradient: LinearGradient(colors: [Colors.deepOrange, Colors.amber]),
shape: BoxShape.circle,
),
child: const Center(child: Text('Gradient', style: TextStyle(color: Colors.white))),
),
])),
)));
}Clip widgets cut their child.
void main() {
runApp(MaterialApp(home: Scaffold(
body: Center(child: Row(mainAxisSize: MainAxisSize.min, children: [
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Container(width: 100, height: 100, color: Colors.teal),
),
const SizedBox(width: 20),
ClipOval(child: Container(width: 100, height: 100, color: Colors.purple)),
])),
)));
}Opacity fades a child. Transform.rotate / scale tilts or zooms it (layout space does not change). AspectRatio keeps width : height fixed, such as 16 : 9 for videos.
void main() {
runApp(MaterialApp(home: Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
Opacity(opacity: 0.4, child: Container(width: 120, height: 50, color: Colors.red)),
const SizedBox(height: 20),
Transform.rotate(angle: 0.2, child: Container(width: 120, height: 50, color: Colors.blue)),
const SizedBox(height: 20),
SizedBox(width: 200, child: AspectRatio(aspectRatio: 16 / 9, child: Container(color: Colors.green))),
])),
)));
}void main() {
runApp(const MaterialApp(home: Scaffold(
body: Center(child: Row(mainAxisSize: MainAxisSize.min, children: [
Chip1('Sweets', Colors.orange),
SizedBox(width: 10),
Chip1('Snacks', Colors.green),
SizedBox(width: 10),
Chip1('Drinks', Colors.blue),
])),
)));
}
class Chip1 extends StatelessWidget {
final String label; final MaterialColor color;
const Chip1(this.label, this.color, {super.key});
@override
Widget build(BuildContext context) => Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
decoration: BoxDecoration(color: color.shade100, borderRadius: BorderRadius.circular(20)),
child: Text(label, style: TextStyle(color: color.shade900, fontWeight: FontWeight.w600)),
);
}void main() {
runApp(MaterialApp(home: Scaffold(
backgroundColor: Colors.grey.shade100,
body: Center(child: Container(
width: 220, padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(16),
boxShadow: const [BoxShadow(color: Colors.black12, blurRadius: 14, offset: Offset(0, 6))],
),
child: const Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [
Icon(Icons.shopping_bag, size: 48, color: Colors.deepOrange),
SizedBox(height: 10),
Text('Tussar Silk Saree', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
Text('Bhagalpur weave', style: TextStyle(color: Colors.grey)),
SizedBox(height: 8),
Text('Rs 2,499', style: TextStyle(fontSize: 22, color: Colors.green, fontWeight: FontWeight.bold)),
]),
)),
)));
}void main() {
runApp(MaterialApp(home: Scaffold(
body: Center(child: Row(mainAxisSize: MainAxisSize.min, children: [
for (final n in ['AK', 'RS', 'MP']) Container(
margin: const EdgeInsets.all(8), width: 64, height: 64,
decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.indigo.shade400,
border: Border.all(color: Colors.white, width: 3),
boxShadow: const [BoxShadow(color: Colors.black26, blurRadius: 6)]),
child: Center(child: Text(n, style: const TextStyle(color: Colors.white, fontSize: 22, fontWeight: FontWeight.bold))),
),
])),
)));
}void main() {
runApp(MaterialApp(home: Scaffold(
body: Padding(padding: const EdgeInsets.all(16), child: Container(
height: 150, width: double.infinity, padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
gradient: const LinearGradient(colors: [Color(0xFFFF512F), Color(0xFFF09819)], begin: Alignment.topLeft, end: Alignment.bottomRight),
borderRadius: BorderRadius.circular(20),
),
child: const Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [
Text('Chhath Offer', style: TextStyle(color: Colors.white, fontSize: 28, fontWeight: FontWeight.w800)),
Text('Flat 30% on pooja items', style: TextStyle(color: Colors.white70, fontSize: 16)),
]),
)),
)));
}class Bubble extends StatelessWidget {
final String text; final bool mine;
const Bubble(this.text, this.mine, {super.key});
@override
Widget build(BuildContext context) => Align(
alignment: mine ? Alignment.centerRight : Alignment.centerLeft,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
padding: const EdgeInsets.all(12),
constraints: const BoxConstraints(maxWidth: 240),
decoration: BoxDecoration(
color: mine ? Colors.green.shade200 : Colors.grey.shade300,
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(16), topRight: const Radius.circular(16),
bottomLeft: Radius.circular(mine ? 16 : 0), bottomRight: Radius.circular(mine ? 0 : 16)),
),
child: Text(text),
),
);
}
void main() => runApp(const MaterialApp(home: Scaffold(
body: Column(children: [
SizedBox(height: 30),
Bubble('Namaste, is the Ranchi bus on time?', false),
Bubble('Yes, it leaves Birsa Chowk at 6 PM.', true),
Bubble('Thank you!', false),
]),
)));class Play extends StatefulWidget {
const Play({super.key});
@override
State<Play> createState() => _PlayState();
}
class _PlayState extends State<Play> {
double op = 1, ang = 0;
@override
Widget build(BuildContext context) => Scaffold(
body: Column(children: [
const SizedBox(height: 40),
Opacity(opacity: op, child: Transform.rotate(angle: ang, child: Container(
width: 150, height: 90,
decoration: BoxDecoration(color: Colors.deepPurple, borderRadius: BorderRadius.circular(16)),
child: const Center(child: Text('Sticker', style: TextStyle(color: Colors.white, fontSize: 24))),
))),
const SizedBox(height: 40),
Text('Opacity ${op.toStringAsFixed(2)}'),
Slider(value: op, onChanged: (v) => setState(() => op = v)),
Text('Angle ${ang.toStringAsFixed(2)} rad'),
Slider(value: ang, min: -1, max: 1, onChanged: (v) => setState(() => ang = v)),
]),
);
}
void main() => runApp(const MaterialApp(home: Play()));class Thumb extends StatelessWidget {
final String title; final Color c;
const Thumb(this.title, this.c, {super.key});
@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.all(12),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
ClipRRect(
borderRadius: BorderRadius.circular(12),
child: AspectRatio(aspectRatio: 16 / 9, child: Container(color: c, child: const Center(child: Icon(Icons.play_circle, color: Colors.white, size: 48)))),
),
const SizedBox(height: 6),
Text(title, style: const TextStyle(fontWeight: FontWeight.w600)),
]),
);
}
void main() => runApp(MaterialApp(home: Scaffold(
body: ListView(children: [
Thumb('Jonha Falls drone view', Colors.teal.shade400),
Thumb('Ranchi street food tour', Colors.deepOrange.shade300),
]),
)));class Plans extends StatefulWidget {
const Plans({super.key});
@override
State<Plans> createState() => _PlansState();
}
class _PlansState extends State<Plans> {
int sel = 1;
final plans = const [['Basic', 'Rs 199 / month'], ['Standard', 'Rs 399 / month'], ['Premium', 'Rs 699 / month']];
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Choose plan')),
body: Padding(padding: const EdgeInsets.all(16), child: Column(children: [
for (var i = 0; i < plans.length; i++) GestureDetector(
onTap: () => setState(() => sel = i),
child: Container(
width: double.infinity, margin: const EdgeInsets.only(bottom: 12), padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: sel == i ? Colors.indigo.shade50 : Colors.white,
borderRadius: BorderRadius.circular(14),
border: Border.all(color: sel == i ? Colors.indigo : Colors.grey.shade300, width: sel == i ? 3 : 1),
),
child: Row(children: [
Expanded(child: Text(plans[i][0], style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold))),
Text(plans[i][1]),
]),
),
),
])),
);
}
void main() => runApp(const MaterialApp(home: Plans()));class Wave extends CustomClipper<Path> {
@override
Path getClip(Size size) {
final p = Path();
p.lineTo(0, size.height - 40);
p.quadraticBezierTo(size.width / 2, size.height + 20, size.width, size.height - 40);
p.lineTo(size.width, 0);
p.close();
return p;
}
@override
bool shouldReclip(CustomClipper<Path> old) => false;
}
void main() => runApp(MaterialApp(home: Scaffold(
body: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
ClipPath(
clipper: Wave(),
child: Container(
height: 220, width: double.infinity,
decoration: const BoxDecoration(gradient: LinearGradient(colors: [Colors.indigo, Colors.cyan])),
padding: const EdgeInsets.fromLTRB(24, 70, 24, 0),
child: const Text('Welcome back,\nAsha Kumari', style: TextStyle(color: Colors.white, fontSize: 28, fontWeight: FontWeight.bold)),
),
),
const Padding(padding: EdgeInsets.all(24), child: Text('Ranchi University, BCA 3rd year')),
]),
)));void main() {
runApp(MaterialApp(home: Scaffold(
backgroundColor: Colors.grey.shade200,
body: Center(child: Container(
width: 280,
decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(20),
boxShadow: const [BoxShadow(color: Colors.black26, blurRadius: 16, offset: Offset(0, 8))]),
child: Column(mainAxisSize: MainAxisSize.min, children: [
Stack(clipBehavior: Clip.none, alignment: Alignment.center, children: [
ClipRRect(
borderRadius: const BorderRadius.vertical(top: Radius.circular(20)),
child: Container(height: 110, decoration: const BoxDecoration(gradient: LinearGradient(colors: [Color(0xFF0B6BFF), Color(0xFF8E5DE8)]))),
),
Positioned(bottom: -36, child: Container(
width: 80, height: 80,
decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.orange, border: Border.all(color: Colors.white, width: 4)),
child: const Center(child: Text('NK', style: TextStyle(color: Colors.white, fontSize: 28, fontWeight: FontWeight.bold))),
)),
]),
const SizedBox(height: 46),
const Text('Nandan Kumar', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
const Text('Trainer, BrainBit Infotech Ranchi', style: TextStyle(color: Colors.grey)),
const SizedBox(height: 16),
const Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [Text('5000+\nStudents', textAlign: TextAlign.center), Text('15+\nYears', textAlign: TextAlign.center)]),
const SizedBox(height: 20),
]),
)),
)));
}margin, border, padding, child.
colour, radius, border, shadow, gradient.
BoxShape.circle and BorderRadius.only.
ClipRRect, ClipOval, ClipPath cut children.
Fade, tilt and scale.
Keeps thumbnails in shape on every phone.
Put widgets in a line: horizontal or vertical.
Take all the space that is left, or a share of it.
Pile widgets on top of each other: badge, caption on image.
A Row goes left to right, so its main axis is horizontal. A Column goes top to bottom, so its main axis is vertical. The cross axis is the other direction.
void main() {
runApp(const MaterialApp(home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Ranchi', style: TextStyle(fontSize: 30)),
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [Icon(Icons.home), Icon(Icons.star), Icon(Icons.settings)]),
Text('Patna', style: TextStyle(fontSize: 30)),
],
),
)));
}Inside a Row or Column, Expanded takes the leftover space. If two children are Expanded, they share it by their flex numbers. Spacer is an empty Expanded.
void main() {
runApp(const MaterialApp(home: Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, children: [
Row(children: [
Expanded(flex: 1, child: ColoredBox(color: Colors.red, child: SizedBox(height: 50, child: Center(child: Text('1'))))),
Expanded(flex: 2, child: ColoredBox(color: Colors.green, child: SizedBox(height: 50, child: Center(child: Text('2'))))),
Expanded(flex: 1, child: ColoredBox(color: Colors.blue, child: SizedBox(height: 50, child: Center(child: Text('1'))))),
]),
SizedBox(height: 20),
Row(children: [Text('Left'), Spacer(), Text('Right')]),
])),
)));
}Stack places children on top of each other. The first child is at the bottom. Use Positioned (top, left, right, bottom) or Align to put a child in a corner.
clipBehavior: Clip.none.void main() {
runApp(MaterialApp(home: Scaffold(
body: Center(child: Stack(children: [
Container(width: 220, height: 140, decoration: BoxDecoration(color: Colors.teal, borderRadius: BorderRadius.circular(16))),
const Positioned(top: 10, right: 10, child: Icon(Icons.favorite, color: Colors.white)),
const Align(alignment: Alignment.bottomLeft, child: Padding(padding: EdgeInsets.all(12), child: Text('Betla National Park', style: TextStyle(color: Colors.white, fontSize: 18)))),
])),
)));
}Wrap works like a Row but moves extra items to the next line. When a Row or Column is too big for the screen Flutter shows yellow and black stripes. Fix it by using Expanded, Wrap, SingleChildScrollView or smaller content.
void main() {
runApp(const MaterialApp(home: Scaffold(
body: Padding(padding: EdgeInsets.all(16), child: Wrap(
spacing: 8, runSpacing: 8,
children: [
Chip(label: Text('Ranchi')), Chip(label: Text('Jamshedpur')), Chip(label: Text('Dhanbad')),
Chip(label: Text('Bokaro')), Chip(label: Text('Hazaribagh')), Chip(label: Text('Deoghar')),
],
)),
)));
}void main() {
runApp(const MaterialApp(home: Scaffold(
body: SafeArea(child: Padding(padding: EdgeInsets.all(16), child: Column(children: [
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Text('Ranchi Kart', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.deepOrange)),
Row(children: [Icon(Icons.search), SizedBox(width: 16), Icon(Icons.shopping_cart)]),
]),
]))),
)));
}void main() {
runApp(const MaterialApp(home: Scaffold(
body: Center(child: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [
Text('BrainBit Infotech', style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
SizedBox(height: 6),
Text('Main Road, Ranchi, Jharkhand'),
Text('Phone: 0651 000 0000'),
Text('Open: Mon to Sat, 9 AM to 6 PM'),
])),
)));
}class Stat extends StatelessWidget {
final String v, l; final Color c;
const Stat(this.v, this.l, this.c, {super.key});
@override
Widget build(BuildContext context) => Expanded(child: Container(
margin: const EdgeInsets.all(4), padding: const EdgeInsets.symmetric(vertical: 16),
decoration: BoxDecoration(color: c.withOpacity(0.15), borderRadius: BorderRadius.circular(12)),
child: Column(children: [Text(v, style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: c)), Text(l)]),
));
}
void main() => runApp(const MaterialApp(home: Scaffold(
body: SafeArea(child: Padding(padding: EdgeInsets.all(12), child: Row(children: [
Stat('120', 'Orders', Colors.blue), Stat('98', 'Delivered', Colors.green), Stat('22', 'Pending', Colors.orange),
]))),
)));void main() {
runApp(MaterialApp(home: Scaffold(
body: Padding(padding: const EdgeInsets.all(16), child: Column(children: [
Row(children: [
const CircleAvatar(child: Text('A')),
const SizedBox(width: 12),
const Text('Asha Kumari', style: TextStyle(fontSize: 18)),
const Spacer(),
ElevatedButton(onPressed: () {}, child: const Text('Follow')),
]),
])),
)));
}class Cart extends StatelessWidget {
final int count;
const Cart(this.count, {super.key});
@override
Widget build(BuildContext context) => Stack(clipBehavior: Clip.none, children: [
const Icon(Icons.shopping_cart, size: 48),
Positioned(right: -6, top: -6, child: Container(
padding: const EdgeInsets.all(5),
decoration: const BoxDecoration(color: Colors.red, shape: BoxShape.circle),
child: Text('$count', style: const TextStyle(color: Colors.white, fontSize: 12, fontWeight: FontWeight.bold)),
)),
]);
}
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: Row(mainAxisSize: MainAxisSize.min, children: [Cart(3), SizedBox(width: 40), Cart(12)])))));class Tags extends StatefulWidget {
const Tags({super.key});
@override
State<Tags> createState() => _TagsState();
}
class _TagsState extends State<Tags> {
final all = ['Cricket', 'Hockey', 'Football', 'Archery', 'Kabaddi', 'Chess', 'Badminton', 'Athletics'];
final picked = <String>{'Hockey'};
@override
Widget build(BuildContext context) => Scaffold(
body: Padding(padding: const EdgeInsets.all(16), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
const Text('Pick your sports', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
const SizedBox(height: 12),
Wrap(spacing: 8, runSpacing: 4, children: [
for (final t in all) FilterChip(
label: Text(t), selected: picked.contains(t),
onSelected: (v) => setState(() { if (v) { picked.add(t); } else { picked.remove(t); } }),
),
]),
const SizedBox(height: 16),
Text('Chosen: ${picked.length}'),
])),
);
}
void main() => runApp(const MaterialApp(home: Tags()));void main() {
runApp(MaterialApp(home: Scaffold(
body: Center(child: ClipRRect(
borderRadius: BorderRadius.circular(18),
child: SizedBox(width: 280, height: 180, child: Stack(children: [
Container(decoration: const BoxDecoration(gradient: LinearGradient(colors: [Color(0xFF134E5E), Color(0xFF71B280)], begin: Alignment.topLeft, end: Alignment.bottomRight))),
const Positioned(top: 12, left: 12, child: Chip(label: Text('Waterfall'))),
Align(alignment: Alignment.bottomCenter, child: Container(
width: double.infinity, padding: const EdgeInsets.all(12), color: Colors.black54,
child: const Text('Hundru Falls, Ranchi', style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w600)),
)),
])),
)),
)));
}class Plan extends StatelessWidget {
final String name, desc; final Color c;
const Plan(this.name, this.desc, this.c, {super.key});
@override
Widget build(BuildContext context) => Expanded(child: Container(
margin: const EdgeInsets.all(4), padding: const EdgeInsets.all(10),
decoration: BoxDecoration(color: c.withOpacity(0.18), borderRadius: BorderRadius.circular(12)),
child: Column(children: [Text(name, style: const TextStyle(fontWeight: FontWeight.bold)), const SizedBox(height: 6), Text(desc, textAlign: TextAlign.center, style: const TextStyle(fontSize: 12))]),
));
}
void main() => runApp(const MaterialApp(home: Scaffold(
body: Padding(padding: EdgeInsets.fromLTRB(8, 60, 8, 8), child: Column(children: [IntrinsicHeight(child: Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Plan('Basic', 'Notes', Colors.blue),
Plan('Pro', 'Notes, live classes, doubt support and weekly tests', Colors.green),
Plan('Max', 'Everything plus placement help', Colors.orange),
]))])),
)));class Summary extends StatelessWidget {
const Summary({super.key});
@override
Widget build(BuildContext context) {
final items = [['Tussar silk saree with hand embroidery border', 2499], ['Dokra brass elephant figurine', 899], ['Sohrai painting small', 650], ['Lac bangles set of 12', 320], ['Bamboo basket large', 450], ['Mahua jam 250 g', 180]];
int total = 0;
for (final i in items) { total += i[1] as int; }
return Scaffold(
appBar: AppBar(title: const Text('Order summary')),
body: Column(children: [
Expanded(child: SingleChildScrollView(child: Column(children: [
for (final i in items) ListTile(
title: Text(i[0] as String, maxLines: 1, overflow: TextOverflow.ellipsis),
trailing: Text('Rs ${i[1]}'),
),
]))),
Container(
padding: const EdgeInsets.all(16), color: Colors.grey.shade200,
child: Row(children: [const Text('Total', style: TextStyle(fontSize: 18)), const Spacer(), Text('Rs $total', style: const TextStyle(fontSize: 22, fontWeight: FontWeight.bold))]),
),
]),
);
}
}
void main() => runApp(const MaterialApp(home: Summary()));Widget col(String l, String v) => Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(l, style: const TextStyle(color: Colors.grey, fontSize: 12)), Text(v, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600))]);
void main() {
runApp(MaterialApp(home: Scaffold(
backgroundColor: Colors.blueGrey.shade50,
body: Center(child: Container(
width: 300, padding: const EdgeInsets.all(18),
decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(18), boxShadow: const [BoxShadow(color: Colors.black12, blurRadius: 12)]),
child: Stack(children: [
Column(mainAxisSize: MainAxisSize.min, children: [
Row(children: [
Expanded(child: col('FROM', 'Ranchi')),
const Icon(Icons.directions_bus, color: Colors.blue),
Expanded(child: Align(alignment: Alignment.centerRight, child: col('TO', 'Patna'))),
]),
const Divider(height: 28),
Wrap(spacing: 28, runSpacing: 12, children: [col('DATE', '24 Nov'), col('TIME', '9:30 PM'), col('SEAT', '12A'), col('FARE', 'Rs 650')]),
]),
const Positioned(right: 0, bottom: 0, child: Text('PAID', style: TextStyle(color: Colors.green, fontSize: 18, fontWeight: FontWeight.w900))),
]),
)),
)));
}Main axis and cross axis alignment.
Shares leftover space by flex.
An empty Expanded gap.
Overlap widgets with Positioned or Align.
Tags flow to the next line.
Expanded, Wrap or scroll fixes stripes.