Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Operating Systems 2 - Lab 4
Combining Row and Column, Expanded vs Flexible, and separating data from UI
Column, then place several of those columns inside a RowRow for horizontal placement, Column for vertical placement, combined for grids of itemsRow(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(children: [Icon(Icons.home), Text('Home')]),
Column(children: [Icon(Icons.search), Text('Search')]),
],
)class NavItem extends StatelessWidget {
final IconData icon;
final String label;
const NavItem({required this.icon, required this.label});
@override
Widget build(BuildContext context) {
return Column(children: [Icon(icon), Text(label)]);
}
}NavItem class replaces three copy-pasted Columns.Row
NavItem
icon: home
NavItem
icon: search
NavItem
icon: person
ExpandedRow or Column to force a child to fill all remaining available space.Row(
children: [
Icon(Icons.label),
Expanded(child: Text('Stretches to fill the rest')),
Icon(Icons.arrow_forward),
],
)flex value: the system sums all flex values and divides space by ratio7, 1, 2 (total 10) get 70%, 10%, and 20% of the space respectivelyExpanded, a Flexible child can end up smaller than its share if it doesn't need the full spaceColumn(
children: [
Flexible(flex: 7, child: Container(color: Colors.blue[100])),
Flexible(flex: 1, child: Container(color: Colors.grey[300])),
Flexible(flex: 2, child: Container(color: Colors.green[100])),
],
)Flexible flex: 7
70%
Flexible flex: 1
10%
Flexible flex: 2
20%
| Widget | Behavior |
|---|---|
Expanded | Always fills all remaining space (equivalent to Flexible with a tight fit) |
Flexible | Takes up to its proportional share; the child may render smaller |
build method makes it hard to read and maintainWidget build(BuildContext context) {
return Scaffold(
body: Column(
children: [ImageSection(), TitleSection(), DescriptionSection()],
),
);
}Main Page
ImageSection
TitleSection
DescriptionSection
class Product {
final String name;
final double price;
const Product({required this.name, required this.price});
}Row and Column to build the basic structureExpanded and Flexible so the screen scales across devicesColumn inside Row (or vice versa) builds grid-like, icon+label layoutsExpanded fills all remaining space; Flexible allocates space proportionally and can shrinkbuild methodMahmoud Abas|Lab: Advanced UI Layouts, Space Management, and Code Organization