text stringlengths 1 372 |
|---|
widget build(BuildContext context) { |
return scaffold( |
appBar: AppBar( |
title: const Text('Sample app'), |
), |
body: ListView(children: _getListData()), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
detecting what was clicked |
in UIKit, you implement the delegate method, |
tableView:didSelectRowAtIndexPath:. |
in flutter, use the touch handling provided by the passed-in widgets. |
<code_start> |
import 'dart:developer' as developer; |
import 'package:flutter/material.dart'; |
void main() { |
runApp(const SampleApp()); |
} |
class SampleApp extends StatelessWidget { |
const SampleApp({super.key}); |
// this widget is the root of your application. |
@override |
widget build(BuildContext context) { |
return const MaterialApp( |
title: 'sample app', |
home: SampleAppPage(), |
); |
} |
} |
class SampleAppPage extends StatefulWidget { |
const SampleAppPage({super.key}); |
@override |
State<SampleAppPage> createState() => _SampleAppPageState(); |
} |
class _SampleAppPageState extends State<SampleAppPage> { |
List<Widget> _getListData() { |
List<Widget> widgets = []; |
for (int i = 0; i < 100; i++) { |
widgets.add( |
GestureDetector( |
onTap: () { |
developer.log('row tapped'); |
}, |
child: padding( |
padding: const EdgeInsets.all(10), |
child: Text('Row $i'), |
), |
), |
); |
} |
return widgets; |
} |
@override |
widget build(BuildContext context) { |
return scaffold( |
appBar: AppBar( |
title: const Text('Sample app'), |
), |
body: ListView(children: _getListData()), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
dynamically updating ListView |
in UIKit, you update the data for the list view, |
and notify the table or collection view using the |
reloadData method. |
in flutter, if you update the list of widgets inside a setState(), |
you quickly see that your data doesn’t change visually. |
this is because when setState() is called, |
the flutter rendering engine looks at the widget tree |
to see if anything has changed. |
when it gets to your ListView, it performs an == check, |
and determines that the two ListViews are the same. |
nothing has changed, so no update is required. |
for a simple way to update your ListView, |
create a new list inside of setState(), |
and copy the data from the old list to the new list. |
while this approach is simple, |
it is not recommended for large data sets, |
as shown in the next example. |
<code_start> |
import 'dart:developer' as developer; |
import 'package:flutter/material.dart'; |
void main() { |
runApp(const SampleApp()); |
} |
class SampleApp extends StatelessWidget { |
const SampleApp({super.key}); |
// this widget is the root of your application. |
@override |
widget build(BuildContext context) { |
return const MaterialApp( |
title: 'sample app', |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.