Guide to Flutter for Android Developers: Engineering Structures and Resource files

1. Where do I put the image files that are relevant to the resolution?

Although Android treats resources and assets differently, the Flutter application only treats assets. All resource files that should be placed in the res/drawable-* folder in Android are placed in an Assets folder in Flutter.

Flutter follows a simple ios-like density-dependent format. Files can be doubled (1.0x), doubled (2.0x), tripled (3.0x), or any other multiple. The Flutter does not have dp units, but has a logical pixel size that is basically the same regardless of the device. The size named devicePixelRatio represents the proportion of the device’s physical pixels under a single logical pixel standard.

The comparison table of density classification with Android is as follows:

Android density modifier Flutter pixel ratio
ldpi 0.75 x
mdpi 1.0 x
hdpi 1.5 x
xhdpi 2.0 x
xxhdpi 3.0 x
xxxhdpi 4.0 x

Files are placed in arbitrary folders – Flutter does not have a predefined folder structure. You define files (including location information) in a pubspec.yaml file, and Flutter is responsible for finding them.

Note that prior to Flutter 1.0 beta 2, files defined in Flutter could not be accessed by native ends and vice versa. Assets and resources defined by the native end cannot be accessed by Flutter because they are placed in different folders.

For Flutter Beta 2, the files are placed in the native asset folder, so they can be accessed by the native AssetManager:

val flutterAssetStream = assetManager.open("flutter_assets/assets/my_flutter_asset.png")
Copy the code

However, with Flutter Beta 2, Flutter still cannot access native resources or assets.

If you want to add a new image resource called my_icon. PNG to the Flutter project and place it in a folder we randomly named images, you need to place the base image (1.0x) in the images folder. And put images of other multiples into a subfolder named for that particular multiple:

Images /my_icon. PNG // Base: 1.0x image images/2.0x/my_icon. PNG // 2.0x image images/3.0x/my_icon. PNG // 3.0x imageCopy the code

Next, you need to define these images in the pubspec.yaml file:

assets:
 - images/my_icon.jpeg
Copy the code

You can then access your images using AssetImage:

return AssetImage("images/my_icon.jpeg");
Copy the code

Or directly through the Image Widget:

@override
Widget build(BuildContext context) {
  return Image.asset("images/my_image.png");
}
Copy the code

2. Where are strings stored? How do you handle localization?

Flutter does not currently have a specific resource management system for managing strings. For now, the best approach is to store strings in a class as static fields and access them through the class. Such as:

class Strings {
  static String welcomeMessage = "Welcome To Flutter";
}
Copy the code

Then in your code, you can access your string like this:

Text(Strings.welcomeMessage)
Copy the code

Flutter provides basic frictionless support on Android, but this feature is still under development.

We encourage Flutter developers to internationalize and localize using intL packages.

3. What are the counterparts of Gradle files? How do I add dependencies?

In Android, you add dependencies to Gradle build scripts. Flutter uses Dart’s own build system as well as the Pub package manager. The build tool proxies the build of native Android and iOS shell apps to the corresponding build system.

Although there are Gradle files in the Android folder of your Flutter project, they are only used to add native dependencies to the integration of the corresponding platform. In general, the external dependencies used in Flutter are defined in the pubspec.yaml file. Pub. Dev is a good place to find Flutter Packages.