text
stringlengths
1
372
itemCount: widgets.length,
itemBuilder: (context, position) {
return getRow(position);
},
),
);
}
}
<code_end>
instead of creating a ListView, create a ListView.builder
that takes two key parameters: the initial length of the list,
and an ItemBuilder function.
the ItemBuilder function is similar to the cellForItemAt
delegate method in an iOS table or collection view,
as it takes a position, and returns the
cell you want rendered at that position.
finally, but most importantly, notice that the onTap() function
doesn’t recreate the list anymore, but instead .adds to it.
<topic_end>
<topic_start>
creating a scroll view
in UIKit, you wrap your views in a ScrollView that
allows a user to scroll your content if needed.
in flutter the easiest way to do this is using the ListView widget.
this acts as both a ScrollView and an iOS TableView,
as you can lay out widgets in a vertical format.
<code_start>
@override
widget build(BuildContext context) {
return ListView(
children: const <widget>[
Text('Row one'),
Text('Row two'),
Text('Row three'),
Text('Row four'),
],
);
}
<code_end>
for more detailed docs on how to lay out widgets in flutter,
see the layout tutorial.
<topic_end>
<topic_start>
gesture detection and touch event handling
this section discusses how to detect gestures
and handle different events in flutter,
and how they compare with UIKit.
<topic_end>
<topic_start>
adding a click listener
in UIKit, you attach a GestureRecognizer to a view to
handle click events.
in flutter, there are two ways of adding touch listeners:
if the widget supports event detection, pass a function to it,
and handle the event in the function. for example, the
ElevatedButton widget has an onPressed parameter:
<code_start>
@override
widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
developer.log('click');
},
child: const Text('Button'),
);
}
<code_end>
if the widget doesn’t support event detection,
wrap the widget in a GestureDetector and pass a function
to the onTap parameter.
<code_start>
class SampleTapApp extends StatelessWidget {
const SampleTapApp({super.key});
@override
widget build(BuildContext context) {
return scaffold(
body: center(
child: GestureDetector(
onTap: () {
developer.log('tap');
},
child: const FlutterLogo(
size: 200,
),
),
),
);
}
}
<code_end>
<topic_end>
<topic_start>
handling other gestures
using GestureDetector you can listen
to a wide range of gestures such as:
tapping
double tapping
long pressing
vertical dragging
horizontal dragging