text
stringlengths
1
372
aligning components horizontally
in SwiftUI, stack views play a big part in designing your layouts.
two separate structures allow you to create stacks:
HStack for horizontal stack views
VStack for vertical stack views
the following SwiftUI view adds a globe image and
text to a horizontal stack view:
flutter uses row rather than HStack:
<code_start>
row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(CupertinoIcons.globe),
Text('Hello, world!'),
],
),
<code_end>
the row widget requires a List<Widget> in the children parameter.
the mainAxisAlignment property tells flutter how to position children
with extra space. MainAxisAlignment.center positions children in the
center of the main axis. for row, the main axis is the horizontal
axis.
<topic_end>
<topic_start>
aligning components vertically
the following examples build on those in the previous section.
in SwiftUI, you use VStack to arrange the components into a
vertical pillar.
flutter uses the same dart code from the previous example,
except it swaps column for row:
<code_start>
column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(CupertinoIcons.globe),
Text('Hello, world!'),
],
),
<code_end>
<topic_end>
<topic_start>
displaying a list view
in SwiftUI, you use the list base component to display sequences
of items.
to display a sequence of model objects, make sure that the user can
identify your model objects.
to make an object identifiable, use the identifiable protocol.
this resembles how flutter prefers to build its list widgets.
flutter doesn’t need the list items to be identifiable.
you set the number of items to display then build a widget for each item.
<code_start>
class person {
string name;
person(this.name);
}
var items = [
Person('Person 1'),
Person('Person 2'),
Person('Person 3'),
];
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
widget build(BuildContext context) {
return scaffold(
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: text(items[index].name),
);
},
),
);
}
}
<code_end>
flutter has some caveats for lists:
the ListView widget has a builder method.
this works like the ForEach within SwiftUI’s list struct.
the itemCount parameter of the ListView sets how many items
the ListView displays.
the itemBuilder has an index parameter that will be between zero
and one less than itemCount.
the previous example returned a ListTile widget for each item.
the ListTile widget includes properties like height and font-size.
these properties help build a list. however, flutter allows you to return
almost any widget that represents your data.
<topic_end>
<topic_start>
displaying a grid
when constructing non-conditional grids in SwiftUI,
you use grid with GridRow.
to display grids in flutter, use the GridView widget.
this widget has various constructors. each constructor has
a similar goal, but uses different input parameters.
the following example uses the .builder() initializer:
<code_start>
const widgets = [
Text('Row 1'),