text
stringlengths
1
372
@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'),
),
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
for more information on using forms,