0
0
Fluttermobile~20 mins

MainAxisAlignment and CrossAxisAlignment in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Alignment Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Effect of MainAxisAlignment.spaceBetween in a Row
Consider a Row widget with three Text widgets as children. The Row uses MainAxisAlignment.spaceBetween. What will be the visual arrangement of the Text widgets inside the Row?
Flutter
Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    Text('One'),
    Text('Two'),
    Text('Three'),
  ],
)
AThe three Text widgets are evenly spaced with equal gaps between them.
BThe three Text widgets are grouped together at the start of the Row with no space between them.
CThe three Text widgets are centered in the Row with equal space on both sides.
DThe three Text widgets are aligned at the end of the Row with no space between them.
Attempts:
2 left
💡 Hint
Think about how spaceBetween distributes space only between children, not at the edges.
ui_behavior
intermediate
2:00remaining
CrossAxisAlignment.stretch effect in Column
What happens when you set CrossAxisAlignment.stretch in a Column containing Text widgets with no width constraints?
Flutter
Column(
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: [
    Text('Hello'),
    Text('World'),
  ],
)
AEach Text widget keeps its natural width and is centered horizontally.
BEach Text widget is aligned to the start horizontally with no stretching.
CEach Text widget expands horizontally to fill the full width of the Column.
DThe Column throws a layout error because Text widgets cannot stretch.
Attempts:
2 left
💡 Hint
CrossAxisAlignment.stretch provides tight cross-axis constraints, but inflexible widgets like Text size to their intrinsic dimensions.
lifecycle
advanced
2:30remaining
Impact of Changing MainAxisAlignment on Widget Rebuild
If you change the mainAxisAlignment property of a Row dynamically in a StatefulWidget, what happens to the widget tree and UI?
Flutter
class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  MainAxisAlignment alignment = MainAxisAlignment.start;

  void toggleAlignment() {
    setState(() {
      alignment = alignment == MainAxisAlignment.start
          ? MainAxisAlignment.center
          : MainAxisAlignment.start;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: alignment,
          children: [Text('A'), Text('B'), Text('C')],
        ),
        ElevatedButton(
          onPressed: toggleAlignment,
          child: Text('Toggle Alignment'),
        ),
      ],
    );
  }
}
AThe Row rebuilds with the new mainAxisAlignment, updating the UI to reflect the new alignment immediately.
BThe Row does not rebuild; the UI remains unchanged until the app restarts.
CThe app crashes because mainAxisAlignment cannot be changed dynamically.
DThe Row rebuilds but the alignment property is ignored, so the UI does not change.
Attempts:
2 left
💡 Hint
setState triggers a rebuild of the widget subtree with updated properties.
📝 Syntax
advanced
2:00remaining
Correct Syntax for Using CrossAxisAlignment.baseline
Which option shows the correct way to use CrossAxisAlignment.baseline in a Row with Text widgets?
Flutter
Row(
  crossAxisAlignment: CrossAxisAlignment.baseline,
  textBaseline: TextBaseline.alphabetic,
  children: [
    Text('Hello', style: TextStyle(fontSize: 20)),
    Text('World', style: TextStyle(fontSize: 14)),
  ],
)
ARow(crossAxisAlignment: CrossAxisAlignment.baseline, textBaseline: null, children: [Text('A'), Text('B')])
BRow(crossAxisAlignment: CrossAxisAlignment.baseline, children: [Text('A'), Text('B')])
CRow(crossAxisAlignment: CrossAxisAlignment.baseline, textBaseline: TextBaseline.alphabetic, children: [Text('A'), Text('B')])
DRow(crossAxisAlignment: CrossAxisAlignment.baseline, textBaseline: TextBaseline.ideographic, children: [Text('A'), Text('B')])
Attempts:
2 left
💡 Hint
CrossAxisAlignment.baseline requires a non-null textBaseline property.
🧠 Conceptual
expert
3:00remaining
Why does CrossAxisAlignment.stretch not work in Row with unbounded height?
In a Row widget, setting CrossAxisAlignment.stretch on children that have no height constraints often does not stretch them as expected. Why does this happen?
ABecause Row gives infinite horizontal space to its children, so they cannot determine a finite width to stretch to.
BBecause CrossAxisAlignment.stretch only works in Columns, not Rows.
CBecause Text widgets ignore CrossAxisAlignment.stretch by design.
DBecause CrossAxisAlignment.stretch requires the Row to have a fixed height.
Attempts:
2 left
💡 Hint
Think about how Row constraints its children in the cross axis (height).