We use SizedBox to create empty space or fix a widget's size. Padding adds space inside a widget around its content.
SizedBox and Padding in Flutter
Introduction
Syntax
Flutter
SizedBox( width: double?, height: double?, child: Widget?, ) Padding( padding: EdgeInsets, child: Widget, )
SizedBox can have width, height, or both. If no child is given, it creates empty space.
Padding uses EdgeInsets to set space inside the widget around its child.
Examples
Flutter
SizedBox(width: 20, height: 20)
Flutter
SizedBox(width: 100, child: Text('Hello'))
Flutter
Padding(padding: EdgeInsets.all(10), child: Text('Hi'))
Flutter
Padding(padding: EdgeInsets.symmetric(horizontal: 20), child: Icon(Icons.star))Sample App
This app shows three widgets stacked vertically:
- A text above a space.
- A text with padding around it.
- A button inside a fixed size box.
The SizedBox adds vertical space and fixes button size. The Padding adds space inside the text widget.
Flutter
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('SizedBox and Padding Example')), body: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text('Above SizedBox'), const SizedBox(height: 30), const Padding( padding: EdgeInsets.all(20), child: Text('Text with Padding'), ), const SizedBox(height: 30), SizedBox( width: 150, height: 50, child: ElevatedButton( onPressed: () {}, child: const Text('SizedBox Button'), ), ), ], ), ), ), ); } }
Important Notes
If SizedBox has no child, it creates empty space.
Padding always needs a child widget to wrap.
Use EdgeInsets.symmetric or EdgeInsets.only for custom padding sides.
Summary
SizedBox controls space or fixes widget size.
Padding adds space inside a widget around its content.
Both help make layouts neat and readable.
Practice
1. What does the
SizedBox widget do in Flutter?easy
Solution
Step 1: Understand SizedBox purpose
SizedBox is used to create a box with fixed width and height to control space.Step 2: Compare with Padding
Padding adds space inside a widget, not fixes size.Final Answer:
It creates a box with a fixed width and height. -> Option BQuick Check:
SizedBox fixes size = A [OK]
Hint: SizedBox fixes size; Padding adds inner space [OK]
Common Mistakes:
- Confusing Padding with SizedBox
- Thinking SizedBox adds inner space
- Assuming SizedBox changes color
2. Which of the following is the correct syntax to add 20 pixels of padding on all sides of a widget?
easy
Solution
Step 1: Identify Padding syntax
Padding usespadding: EdgeInsets.all(value)to add space inside.Step 2: Check other options
SizedBox does not have padding property; margin is not valid in Padding widget.Final Answer:
Padding(padding: EdgeInsets.all(20), child: Text('Hello')) -> Option AQuick Check:
Padding with EdgeInsets.all = B [OK]
Hint: Padding uses EdgeInsets.all for uniform space [OK]
Common Mistakes:
- Using SizedBox with padding property
- Confusing margin with padding
- Using SizedBox to add padding
3. What will be the size of the red box on screen in this code?
SizedBox( width: 100, height: 50, child: Container(color: Colors.red), )
medium
Solution
Step 1: Understand SizedBox effect
SizedBox fixes the child's size to width 100 and height 50 pixels.Step 2: Container inside SizedBox
Container will fill the SizedBox size, so red box is 100x50 pixels.Final Answer:
100 pixels wide and 50 pixels tall -> Option DQuick Check:
SizedBox fixes size = A [OK]
Hint: SizedBox sets fixed size regardless of child content [OK]
Common Mistakes:
- Thinking Container size controls box size
- Assuming full screen size
- Believing zero size without explicit child size
4. Identify the error in this code snippet:
Padding(
padding: EdgeInsets.symmetric(10),
child: Text('Hello'),
)medium
Solution
Step 1: Check EdgeInsets.symmetric parameters
EdgeInsets.symmetric requires named parameters horizontal and/or vertical, not a single number.Step 2: Validate other options
Padding can have Text child; comma is present; EdgeInsets.all is different but not error.Final Answer:
EdgeInsets.symmetric requires two parameters: horizontal and vertical -> Option CQuick Check:
EdgeInsets.symmetric needs named params = C [OK]
Hint: EdgeInsets.symmetric needs horizontal and/or vertical named args [OK]
Common Mistakes:
- Passing single number to EdgeInsets.symmetric
- Confusing EdgeInsets.symmetric with EdgeInsets.all
- Thinking Padding can't wrap Text
5. You want to create a button with fixed width 150, height 50, and 10 pixels padding inside around the text. Which widget tree achieves this correctly?
hard
Solution
Step 1: Understand padding inside fixed size
To have fixed outer size with padding inside around text, use SizedBox containing Padding containing Text. SizedBox constrains total to 150x50; Padding adds space within that.Step 2: Analyze correct structure
SizedBox(width: 150, height: 50, child: Padding(padding: EdgeInsets.all(10), child: Text('Click'))) fixes outer size, adds inner padding.Step 3: Check other options
SizedBox(width: 150, height: 50, child: Text('Click')) lacks padding; Container(width: 150, height: 50, margin: EdgeInsets.all(10), child: Text('Click')) adds margin outside (total larger), no inner padding; Padding(padding: EdgeInsets.all(10), child: SizedBox(width: 150, height: 50, child: Text('Click'))) adds padding outside fixed box (total 170x70), no inner padding.Final Answer:
SizedBox(width: 150, height: 50, child: Padding(padding: EdgeInsets.all(10), child: Text('Click'))) -> Option AQuick Check:
SizedBox containing Padding = A [OK]
Hint: Padding inside SizedBox for fixed size with inner space [OK]
Common Mistakes:
- Putting SizedBox inside Padding (adds outer space)
- Using Container with margin
- Forgetting to add padding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
