text
stringlengths
1
372
you should break deeply nested widgets into
functions that return the widget or smaller classes.
creating separate functions
and widgets allows you to reuse the components within the app.
<topic_end>
<topic_start>
how do i create reusable components?
in react native, you would define a class to create a
reusable component and then use props methods to set
or return properties and values of the selected elements.
in the example below, the CustomCard class is defined
and then used inside a parent class.
in flutter, define a class to create a custom widget and then reuse the
widget. you can also define and call a function that returns a
reusable widget as shown in the build function in the following example.
<code_start>
/// flutter
class CustomCard extends StatelessWidget {
const CustomCard({
super.key,
required this.index,
required this.onPress,
});
final int index;
final void function() onPress;
@override
widget build(BuildContext context) {
return card(
child: column(
children: <widget>[
Text('Card $index'),
TextButton(
onPressed: onPress,
child: const Text('Press'),
),
],
),
);
}
}
class UseCard extends StatelessWidget {
const UseCard({super.key, required this.index});
final int index;
@override
widget build(BuildContext context) {
/// usage
return CustomCard(
index: index,
onPress: () {
print('Card $index');
},
);
}
}
<code_end>
in the previous example, the constructor for the CustomCard
class uses dart’s curly brace syntax { } to indicate named parameters.
to require these fields, either remove the curly braces from
the constructor, or add required to the constructor.
the following screenshots show an example of the reusable
CustomCard class.
<topic_end>
<topic_start>
project structure and resources
<topic_end>
<topic_start>
where do i start writing the code?
start with the lib/main.dart file.
it’s autogenerated when you create a flutter app.
<code_start>
// dart
void main() {
print('Hello, this is the main function.');
}
<code_end>
in flutter, the entry point file is
{project_name}/lib/main.dart and execution
starts from the main function.
<topic_end>
<topic_start>
how are files structured in a flutter app?
when you create a new flutter project,
it builds the following directory structure.
you can customize it later, but this is where you start.
<topic_end>
<topic_start>
where do i put my resources and assets and how do i use them?
a flutter resource or asset is a file that is bundled and deployed
with your app and is accessible at runtime.
flutter apps can include the following asset types:
flutter uses the pubspec.yaml file,
located at the root of your project, to
identify assets required by an app.
the assets subsection specifies files that should be included with the app.
each asset is identified by an explicit path
relative to the pubspec.yaml file, where the asset file is located.
the order in which the assets are declared does not matter.
the actual directory used (assets in this case) does not matter.
however, while assets can be placed in any app directory, it’s a
best practice to place them in the assets directory.