text stringlengths 1 372 |
|---|
} |
class SampleAppPage extends StatefulWidget { |
const SampleAppPage({super.key}); |
@override |
State<SampleAppPage> createState() => _SampleAppPageState(); |
} |
class _SampleAppPageState extends State<SampleAppPage> { |
List<Widget> _getListData() { |
return List<Widget>.generate( |
100, |
(index) => GestureDetector( |
onTap: () { |
developer.log('Row $index tapped'); |
}, |
child: padding( |
padding: const EdgeInsets.all(10), |
child: Text('Row $index'), |
), |
), |
); |
} |
@override |
widget build(BuildContext context) { |
return scaffold( |
appBar: AppBar(title: const Text('Sample app')), |
body: ListView(children: _getListData()), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
how do i update a ListView dynamically? |
in Xamarin.Forms, if you bound the |
ItemsSource property to an ObservableCollection, |
you would just update the list in your ViewModel. |
alternatively, you could assign a new list to the ItemSource property. |
in flutter, things work a little differently. |
if you update the list of widgets inside a setState() method, |
you would quickly see that your data did not 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 a == 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 { |
/// this widget is the root of your application. |
const SampleApp({super.key}); |
@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> widgets = <widget>[]; |
@override |
void initState() { |
super.initState(); |
for (int i = 0; i < 100; i++) { |
widgets.add(getRow(i)); |
} |
} |
widget getRow(int index) { |
return GestureDetector( |
onTap: () { |
setState(() { |
widgets = List<Widget>.from(widgets); |
widgets.add(getRow(widgets.length)); |
developer.log('Row $index'); |
}); |
}, |
child: padding( |
padding: const EdgeInsets.all(10), |
child: Text('Row $index'), |
), |
); |
} |
@override |
widget build(BuildContext context) { |
return scaffold( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.