text
stringlengths 1
474
|
---|
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'),
|
Icon(CupertinoIcons.arrow_down_square),
|
Icon(CupertinoIcons.arrow_up_square),
|
Text('Row 2'),
|
Icon(CupertinoIcons.arrow_down_square),
|
Icon(CupertinoIcons.arrow_up_square),
|
];
|
class HomePage extends StatelessWidget {
|
const HomePage({super.key});
|
@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
body: GridView.builder(
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
crossAxisCount: 3,
|
mainAxisExtent: 40,
|
),
|
itemCount: widgets.length,
|
itemBuilder: (context, index) => widgets[index],
|
),
|
);
|
}
|
}<code_end>
|
The SliverGridDelegateWithFixedCrossAxisCount delegate determines
|
various parameters that the grid uses to lay out its components.
|
This includes crossAxisCount that dictates the number of items
|
displayed on each row.How SwiftUI’s Grid and Flutter’s GridView differ in that Grid
|
requires GridRow. GridView uses the delegate to decide how the
|
grid should lay out its components.<topic_end>
|
<topic_start>
|
Creating a scroll view
|
In SwiftUI, you use ScrollView to create custom scrolling
|
components.
|
The following example displays a series of PersonView instances
|
in a scrollable fashion.To create a scrolling view, Flutter uses SingleChildScrollView.
|
In the following example, the function mockPerson mocks instances
|
of the Person class to create the custom PersonView widget.
|
<code_start> SingleChildScrollView(
|
child: Column(
|
children: mockPersons
|
.map(
|
(person) => PersonView(
|
person: person,
|
),
|
)
|
.toList(),
|
),
|
),<code_end>
|
<topic_end>
|
<topic_start>
|
Responsive and adaptive design
|
In SwiftUI, you use GeometryReader to create relative view sizes.For example, you could:You can also see if the size class has .regular or .compact
|
using horizontalSizeClass.To create relative views in Flutter, you can use one of two options:To learn more, check out Creating responsive and adaptive apps.<topic_end>
|
<topic_start>
|
Managing state
|
In SwiftUI, you use the @State property wrapper to represent the
|
internal state of a SwiftUI view.SwiftUI also includes several options for more complex state
|
management such as the ObservableObject protocol.Flutter manages local state using a StatefulWidget.
|
Implement a stateful widget with the following two classes:The State object stores the widget’s state.
|
To change a widget’s state, call setState() from the State subclass
|
to tell the framework to redraw the widget.The following example shows a part of a counter app:
|
<code_start>class MyHomePage extends StatefulWidget {
|
const MyHomePage({super.key});
|
@override
|
State<MyHomePage> createState() => _MyHomePageState();
|
}
|
class _MyHomePageState extends State<MyHomePage> {
|
int _counter = 0;
|
@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
body: Center(
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.