text
stringlengths 1
372
|
|---|
appBar: AppBar(title: const Text('Sample app')),
|
body: ListView(children: widgets),
|
);
|
}
|
}
|
<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 {
|
/// 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(
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.