text
stringlengths 1
372
|
---|
ListView, which renders each row with what your adapter returns. however, you |
have to make sure you recycle your rows, otherwise, you get all sorts of crazy |
visual glitches and memory issues. |
due to flutter’s immutable widget pattern, you pass a list of |
widgets to your ListView, and flutter takes care of making sure |
that scrolling is fast and smooth. |
<code_start> |
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> { |
@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(Padding( |
padding: const EdgeInsets.all(10), |
child: Text('Row $i'), |
)); |
} |
return widgets; |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
how do i know which list item is clicked on? |
in android, the ListView has a method to find out which item was clicked, |
‘onitemclicklistener’. |
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 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'); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.