This demonstrates a basic usage of the Column and Row widgets.
Column(
children: <Widget>[
Text('Item 1 in Column'),
Text('Item 2 in Column'),
Row(
children: <Widget>[
Text('Item 1 in Row'),
Text('Item 2 in Row'),
],
),
],
)
mainAxisAlignment:This demonstrates how to align the children vertically in a Column and horizontally in a Row.
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, // evenly distribute space between children
children: <Widget>[
Text('Top'),
Text('Middle'),
Text('Bottom'),
],
)
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, // place space between each child
children: <Widget>[
Text('Left'),
Text('Center'),
Text('Right'),
],
)
crossAxisAlignment:This demonstrates how to align children across the main axis.
Column(
crossAxisAlignment: CrossAxisAlignment.start, // align children to the left
children: <Widget>[
Text('Aligned Left 1'),
Text('Aligned Left 2'),
],
)
Row(
crossAxisAlignment: CrossAxisAlignment.center, // vertically center children
children: <Widget>[
Icon(Icons.star),
Text('Vertically Centered with Icon'),
],
)
This demonstrates nesting of Column and Row widgets.
Column(
children: <Widget>[
Text('Column Item 1'),
Row(
children: <Widget>[
Text('Row Item 1'),
Column(
children: <Widget>[
Text('Nested Column Item 1'),
Text('Nested Column Item 2'),
],
),
Text('Row Item 2'),
],
),
Text('Column Item 2'),
],
)
Expanded with Column & Row:The Expanded widget can be used to distribute available space among children.
Row(
children: <Widget>[
Expanded(
child: Container(color: Colors.red, child: Text('Takes up half the space')),
flex: 1,
),
Expanded(
child: Container(color: Colors.blue, child: Text('Takes up half the space')),
flex: 1,
),
],
)
This demonstrates combining properties like mainAxisAlignment, crossAxisAlignment, and Expanded for complex layouts.
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text('Top of the column'),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Left in Row'),
Text('Right in Row'),
],
),
Expanded(
child: Container(
color: Colors.green,
child: Center(child: Text('Expanded container')),
),
),
Text('Bottom of the column'),
],
)
With these demonstrations, you can showcase how Column and Row widgets in Flutter can be used in various combinations to achieve different layouts. Make sure to run these code snippets in a Flutter app and show the visual results to your audience. They'll get a clearer understanding when they see the visual representations alongside the code.