text
stringlengths 1
474
|
---|
Widget build(BuildContext context) { |
return MaterialApp( |
title: 'Sample App', |
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
), |
home: const SampleAppPage(), |
); |
} |
} |
class SampleAppPage extends StatefulWidget { |
const SampleAppPage({super.key}); |
@override |
State<SampleAppPage> createState() => _SampleAppPageState(); |
} |
class _SampleAppPageState extends State<SampleAppPage> { |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
title: const Text('Sample App'), |
), |
body: ListView(children: _getListData()), |
); |
} |
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; |
} |
}<code_end> |
<topic_end> |
<topic_start> |
How do I update ListView’s dynamically? |
On Android, you update the adapter and call notifyDataSetChanged.In Flutter, if you were to update the list of widgets inside a setState(), |
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 { |
const SampleApp({super.key}); |
// This widget is the root of your application. |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
title: 'Sample App', |
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
), |
home: const SampleAppPage(), |
); |
} |
} |
class SampleAppPage extends StatefulWidget { |
const SampleAppPage({super.key}); |
@override |
State<SampleAppPage> createState() => _SampleAppPageState(); |
} |
class _SampleAppPageState extends State<SampleAppPage> { |
List<Widget> widgets = []; |
@override |
void initState() { |
super.initState(); |
for (int i = 0; i < 100; i++) { |
widgets.add(getRow(i)); |
} |
} |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
title: const Text('Sample App'), |
), |
body: ListView(children: widgets), |
); |
} |
Widget getRow(int i) { |
return GestureDetector( |
onTap: () { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.