Bird
Raised Fist0
Fluttermobile~15 mins

MainAxisAlignment and CrossAxisAlignment in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Alignment Demo Screen
This screen shows three colored boxes arranged in a row. The boxes are aligned using MainAxisAlignment and CrossAxisAlignment properties to demonstrate how they affect layout.
Target UI
---------------------------------
| Alignment Demo Screen          |
|-------------------------------|
| [ Red ] [ Green ] [ Blue ]    |
|                               |
|                               |
|                               |
---------------------------------
Use a Row widget to arrange three Containers horizontally.
Set MainAxisAlignment to spaceEvenly to spread boxes evenly across the row.
Set CrossAxisAlignment to center to vertically center the boxes.
Each box should be a square of 80x80 pixels with colors red, green, and blue.
Add a title text at the top with the screen name.
Starter Code
Flutter
import 'package:flutter/material.dart';

class AlignmentDemoScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Alignment Demo Screen'),
      ),
      body: Center(
        child: Row(
          // TODO: Add MainAxisAlignment and CrossAxisAlignment here
          children: [
            Container(
              width: 80,
              height: 80,
              color: Colors.red,
            ),
            Container(
              width: 80,
              height: 80,
              color: Colors.green,
            ),
            Container(
              width: 80,
              height: 80,
              color: Colors.blue,
            ),
          ],
        ),
      ),
    );
  }
}
Task 1
Task 2
Solution
Flutter
import 'package:flutter/material.dart';

class AlignmentDemoScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Alignment Demo Screen'),
      ),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              width: 80,
              height: 80,
              color: Colors.red,
            ),
            Container(
              width: 80,
              height: 80,
              color: Colors.green,
            ),
            Container(
              width: 80,
              height: 80,
              color: Colors.blue,
            ),
          ],
        ),
      ),
    );
  }
}

We used a Row widget to arrange three colored boxes horizontally. The mainAxisAlignment property is set to MainAxisAlignment.spaceEvenly which spreads the boxes evenly across the horizontal axis with equal space between and around them. The crossAxisAlignment is set to CrossAxisAlignment.center to vertically center the boxes within the row's height. Each box is a square container of 80x80 pixels with distinct colors to clearly see the alignment effect.

Final Result
Completed Screen
---------------------------------
| Alignment Demo Screen          |
|-------------------------------|
|  [ Red ]   [ Green ]   [ Blue ]|
|                               |
|                               |
|                               |
---------------------------------
The user sees three colored squares spaced evenly in a horizontal row.
The boxes are vertically centered in the available space.
No interactive elements on this screen.
Stretch Goal
Add a Column below the Row that uses CrossAxisAlignment.start to align three text labels to the left.
💡 Hint
Use a Column widget with crossAxisAlignment set to CrossAxisAlignment.start and add three Text widgets inside.

Practice

(1/5)
1. What does MainAxisAlignment control in a Flutter Row widget?
easy
A. How children are aligned vertically along the row
B. How children are aligned vertically along the column
C. How children are aligned horizontally along the row
D. How children are aligned horizontally along the column

Solution

  1. Step 1: Understand the main axis in a Row

    In a Row widget, the main axis runs horizontally from left to right.
  2. Step 2: Role of MainAxisAlignment

    MainAxisAlignment controls how children are placed along this horizontal main axis.
  3. Final Answer:

    How children are aligned horizontally along the row -> Option C
  4. Quick Check:

    MainAxisAlignment = horizontal alignment in Row [OK]
Hint: MainAxisAlignment aligns along the main direction [OK]
Common Mistakes:
  • Confusing main axis with cross axis
  • Mixing Row and Column directions
  • Thinking MainAxisAlignment controls vertical alignment in Row
2. Which of the following is the correct syntax to center children horizontally in a Row using MainAxisAlignment?
easy
A. crossAxisAlignment: CrossAxisAlignment.center,
B. mainAxisAlignment: CrossAxisAlignment.center,
C. crossAxisAlignment: MainAxisAlignment.center,
D. mainAxisAlignment: MainAxisAlignment.center,

Solution

  1. Step 1: Identify the property for main axis alignment

    The property to align children along the main axis is mainAxisAlignment.
  2. Step 2: Use the correct enum value

    To center children, use MainAxisAlignment.center.
  3. Final Answer:

    mainAxisAlignment: MainAxisAlignment.center, -> Option D
  4. Quick Check:

    Center horizontally in Row = mainAxisAlignment.center [OK]
Hint: Use mainAxisAlignment for main axis, CrossAxisAlignment for cross axis [OK]
Common Mistakes:
  • Using crossAxisAlignment instead of mainAxisAlignment
  • Mixing enum types for properties
  • Forgetting the comma at the end
3. Given this Flutter code snippet, what will be the vertical alignment of the children inside the Row?
Row(
  crossAxisAlignment: CrossAxisAlignment.end,
  children: [
    Container(height: 50, width: 50, color: Colors.red),
    Container(height: 100, width: 50, color: Colors.blue),
  ],
)
medium
A. Children aligned at the bottom of the Row
B. Children aligned at the top of the Row
C. Children centered vertically in the Row
D. Children stretched to fill the Row height

Solution

  1. Step 1: Understand crossAxisAlignment in Row

    In a Row, the cross axis is vertical. CrossAxisAlignment.end aligns children at the bottom.
  2. Step 2: Apply to given containers

    The containers have different heights, so aligning at the bottom means their bottom edges line up.
  3. Final Answer:

    Children aligned at the bottom of the Row -> Option A
  4. Quick Check:

    CrossAxisAlignment.end = bottom alignment in Row [OK]
Hint: CrossAxisAlignment.end aligns children bottom in Row [OK]
Common Mistakes:
  • Confusing CrossAxisAlignment.end with top alignment
  • Mixing main and cross axis directions
  • Assuming children stretch by default
4. Identify the error in this Flutter code snippet:
Column(
  mainAxisAlignment: MainAxisAlignment.left,
  children: [
    Text('Hello'),
    Text('World'),
  ],
)
medium
A. Children list is empty
B. MainAxisAlignment.left is invalid; use start or end instead
C. Column cannot have Text widgets as children
D. Missing comma after children list

Solution

  1. Step 1: Check MainAxisAlignment values

    MainAxisAlignment enum does not have a 'left' value; valid values include start, end, center, spaceBetween, etc.
  2. Step 2: Confirm correct usage

    To align children to the start horizontally in a Column, use MainAxisAlignment.start.
  3. Final Answer:

    MainAxisAlignment.left is invalid; use start or end instead -> Option B
  4. Quick Check:

    MainAxisAlignment.left is not a valid enum [OK]
Hint: Use start/end/center, not left/right in MainAxisAlignment [OK]
Common Mistakes:
  • Using left or right instead of start or end
  • Confusing MainAxisAlignment with CrossAxisAlignment
  • Ignoring enum value errors
5. You want to create a Column where children are spaced evenly vertically, but each child is aligned to the start horizontally. Which combination of MainAxisAlignment and CrossAxisAlignment should you use?
hard
A. mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start
B. mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center
C. mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.end
D. mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.stretch

Solution

  1. Step 1: Understand vertical spacing in Column

    MainAxisAlignment controls vertical spacing in Column. To space children evenly, use MainAxisAlignment.spaceEvenly.
  2. Step 2: Understand horizontal alignment in Column

    CrossAxisAlignment controls horizontal alignment. To align children to the start (left), use CrossAxisAlignment.start.
  3. Final Answer:

    mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start -> Option A
  4. Quick Check:

    Vertical spacing evenly + horizontal start = mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start [OK]
Hint: MainAxisAlignment for vertical, CrossAxisAlignment for horizontal in Column [OK]
Common Mistakes:
  • Mixing up main and cross axis in Column
  • Using spaceBetween instead of spaceEvenly
  • Choosing wrong crossAxisAlignment for start alignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard