text
stringlengths 1
474
|
---|
setState(() { |
widgets = List.from(widgets); |
widgets.add(getRow(widgets.length)); |
developer.log('row $i'); |
}); |
}, |
child: Padding( |
padding: const EdgeInsets.all(10), |
child: Text('Row $i'), |
), |
); |
} |
}<code_end> |
The recommended, efficient, and effective way to build a list uses a |
ListView.Builder. This method is great when you have a dynamic |
List or a List with very large amounts of data. This is essentially |
the equivalent of RecyclerView on Android, which automatically |
recycles list elements for you: |
<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.builder( |
itemCount: widgets.length, |
itemBuilder: (context, position) { |
return getRow(position); |
}, |
), |
); |
} |
Widget getRow(int i) { |
return GestureDetector( |
onTap: () { |
setState(() { |
widgets.add(getRow(widgets.length)); |
developer.log('row $i'); |
}); |
}, |
child: Padding( |
padding: const EdgeInsets.all(10), |
child: Text('Row $i'), |
), |
); |
} |
}<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 getView |
function in an Android adapter; it takes a position, |
and returns the row 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> |
Working with text |
<topic_end> |
<topic_start> |
How do I set custom fonts on my Text widgets? |
In Android SDK (as of Android O), you create a Font resource file and |
pass it into the FontFamily param for your TextView.In Flutter, place the font file in a folder and reference it in the |
pubspec.yaml file, similar to how you import images.Then assign the font to your Text widget: |
<code_start>@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
title: const Text('Sample App'), |
), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.