text
stringlengths 1
474
|
---|
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.During a build, Flutter places assets into a special archive
|
called the asset bundle, which apps read from at runtime.
|
When an asset’s path is specified in the assets’ section of pubspec.yaml,
|
the build process looks for any files
|
with the same name in adjacent subdirectories.
|
These files are also included in the asset bundle
|
along with the specified asset. Flutter uses asset variants
|
when choosing resolution-appropriate images for your app.In React Native, you would add a static image by placing the image file
|
in a source code directory and referencing it.In Flutter, add a static image to your app
|
using the Image.asset constructor in a widget’s build method.
|
<code_start>Image.asset('assets/background.png');<code_end>
|
For more information, see Adding Assets and Images in Flutter.<topic_end>
|
<topic_start>
|
How do I load images over a network?
|
In React Native, you would specify the uri in the
|
source prop of the Image component and also provide the
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.