Menu
CoddyTech

Spacing Inside a Wrap

Part of the Layout That Fits section of Coddy's Flutter journey. Lesson 9 of 30.

The tags fit now, but they touch. A Wrap has two settings for the space between its children:

Wrap(
  spacing: 8,
  runSpacing: 12,
  children: [...],
)

spacing: is the gap between two children on the same line. runSpacing: is the gap between one line and the next. Each line of a Wrap is called a run, which is where the name comes from.

The gaps count when the Wrap decides what fits on a line, so a bigger spacing can move a child down to the next line.

challenge icon

Challenge

Easy

Give the tags room:

  • 8 pixels between tags on the same line
  • 12 pixels between the lines

Try it yourself

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    final tags = ['Dart', 'Flutter', 'Mobile', 'Design', 'Games', 'Web'];
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: const Text('Interests')),
        body: Padding(
          padding: const EdgeInsets.all(16),
          child: Wrap(
            children: [
              for (final tag in tags)
                Container(
                  width: 110,
                  height: 40,
                  decoration: BoxDecoration(
                    color: Colors.purple.shade100,
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: Center(child: Text(tag)),
                ),
            ],
          ),
        ),
      ),
    );
  }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Layout That Fits

Practice on your own: Online Flutter compiler