What is the difference between padding and margin in Flutter?
Padding is space inside the widget boundary. Margin (via Container) is space outside the widget boundary.
Control spacing inside and outside widgets precisely
Passing the Quiz & Exercises is mandatory to earn FX and proceed. If you fail, try again to gain the required FX, then the Proceed button will become available.
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Spacing is crucial for building readable, professional user interfaces. Flutter handles spacing differently from CSS, with a clear distinction between padding (space inside a widget) and margin (space outside a widget).
1// ── Padding Widget - adds space INSIDE ──2Padding(3padding: const EdgeInsets.all(16), // all sides4// padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),5// padding: const EdgeInsets.only(left: 16, top: 8),6// padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),7child: Text('Hello with padding'),8)910// ── Container with margin + padding ──11Container(12margin: const EdgeInsets.symmetric(horizontal: 16), // outside spacing13padding: const EdgeInsets.all(12), // inside spacing14decoration: BoxDecoration(15color: Colors.blue.shade50,16borderRadius: BorderRadius.circular(8),17),18child: const Text('Card with margin and padding'),19)
1// ── Center Widget ──2Center(3child: Text('I am centered!'),4)56// ── SizedBox for fixed gaps ──7Column(8children: [9const Text('Item 1'),10const SizedBox(height: 16), // vertical gap11const Text('Item 2'),12const SizedBox(height: 8),13const Text('Item 3'),14],15)1617// ── Align for precise positioning ──18Align(19alignment: Alignment.topRight, // or Alignment(0.5, -0.5)20child: const Text('Top right'),21)
In CSS, margin is outside the element and padding is inside. Flutter works the same way conceptually. The Padding widget adds space inside, and Container margin adds space outside. Use SizedBox for explicit spacing between widgets in Row or Column.
Visual quality often depends more on spacing than colors.
Use a spacing scale and repeat it consistently. For example: 4, 8, 12, 16, 24, 32.
Predictable spacing improves readability, scanning speed, and perceived polish.
Control spacing inside and outside widgets precisely. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.
Build confidence with basics first; speed comes later.
Sketch the widget tree first, then code from top to bottom.
Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.
Padding is space inside the widget boundary. Margin (via Container) is space outside the widget boundary.
SizedBox(height: n) adds a fixed gap. Spacer() takes remaining space. Both work for spacing in Column. SizedBox is more predictable.
EdgeInsets.symmetric(horizontal: 16) sets left=16 and right=16. Use vertical for top/bottom. Use all() for all four sides.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue