text
stringlengths
1
474
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 {
/// 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 = [];
@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.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(
appBar: AppBar(title: const Text('Sample App')),
body: ListView.builder(
itemCount: widgets.length,
itemBuilder: (context, index) {
return getRow(index);
},
),
);
}
}<code_end>
Instead of creating a ListView, create a ListView.builder
that takes two key parameters: the initial length of the list,
and an item builder function.The item builder 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.For more information, see
Your first Flutter app codelab.<topic_end>
<topic_start>
Working with text
<topic_end>
<topic_start>
How do I set custom fonts on my text widgets?
In Xamarin.Forms, you would have to add a custom font in each native project.
Then, in your Element you would assign this font name
to the FontFamily attribute using filename#fontname
and just fontname for iOS.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')),
body: const Center(
child: Text(
'This is a custom font text',
style: TextStyle(fontFamily: 'MyCustomFont'),
),
),
);
}<code_end>
<topic_end>
<topic_start>
How do I style my text widgets?
Along with fonts, you can customize other styling elements on a Text widget.
The style parameter of a Text widget takes a TextStyle object,
where you can customize many parameters, such as:<topic_end>
<topic_start>
Form input