Figuring out Flutter Flavors. Have you ever ever puzzled how you can arrange… | via Muayad alrazehi | Feb, 2024

Have you ever ever puzzled how you can arrange other environments for your Flutter app?

Flutter’s flexibility lets in builders to create flexible packages with a unmarried codebase. One tough function contributing to this adaptability is “flavors.”
Flavors (referred to as construct configurations, Scheme in iOS and macOS) or flavorDimensions in Android permit the introduction of various variations of an app that percentage a commonplace codebase however have distinct configurations, options, or branding. That is specifically helpful when managing quite a lot of environments similar to construction, staging, and manufacturing.

Let’s get our arms grimy and spot what we’re speaking about in motion.
Goal: create a flutter Android app with two environments every with a unique colour and identify and construct APKs from every surroundings which might be distinct of their utility ID and construct title.

first, let’s create a category representing our surroundings and major colour

magnificence EnvConfig {
ultimate String env;
ultimate Colour mainColor;

EnvConfig({
required this.env,
required this.mainColor,
});
}

let’s edit the major.dart, take away the default major manner and substitute it with this one

void runWithConfig(EnvConfig config) {
runApp(FlavorsApp(envConfig: config));
}

our easy app simply to peer the adjustments, not anything fancy.

magnificence FlavorsApp extends StatelessWidget {
ultimate EnvConfig envConfig;

const FlavorsApp({tremendous.key, required this.envConfig});

@override
Widget construct(BuildContext context) {
go back MaterialApp(
identify: 'Flutter Flavors Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: envConfig.mainColor),
),
house: Scaffold(
appBar: AppBar(
identify: Textual content('Env is ${envConfig.env}'),
),
frame: const Heart(
kid: Textual content('Hi, global!'),
),
),
);
}
}

let’s create two dart main_env1 and main_env2

void major() {
ultimate env1 = EnvConfig(
env: 'Env 1',
mainColor: Colours.deepPurple,
);
runWithConfig(env1);
}
void major() {
ultimate env1 = EnvConfig(
env: 'Env 2',
mainColor: Colours.deepOrange,
);
runWithConfig(env1);
}

now let’s upload a brand new construct config to Android Studio so we will run and take a look at if the whole thing goes smartly to this point

and the similar factor for env2.

now we will select the surroundings to run on

and spot the adjustments showing

Excellent So Some distance 🤩 , however we didn’t use taste but😱
and we don’t understand how we will construct APKs with other IDs and Construct in line with the surroundings, right here the place flavorDimensions come

let’s cross to app-level construct.gradle report and upload the next to android block

flavorDimensions "flavor_demo" 
productFlavors {
env1 {
measurement "flavor_demo"
applicationIdSuffix ".env1"
versionNameSuffix "-env1"
}
env2 {
measurement "flavor_demo"
applicationIdSuffix ".env2"
versionNameSuffix "-env2"
}
}

flavorDimensions is some way of grouping product flavors , you’ll be able to have more than one dimensions like flavor_demo, flavor_demo2 and many others

applicationIdSuffix Will probably be appended to the applicationId

versionNameSuffix Will probably be appended to the versionName

now you’ll be able to run the next and get separate APKs along with your config and other app IDs and construct names

flutter construct apk --flavor env1 -t lib/main_env1.dart
flutter construct apk --flavor env2 -t lib/main_env2.dart

Neatly, this was once a small rationalization about flavors in Android, and identical steps the use of schemes may also be finished in IOS, That is simply a place to begin. You’ll use flavors for extra complex situations:

  • Connect with other Firebase tasks: Use environment-specific GoogleServices-Information.plist information for every taste.
  • Allow/disable options: Use conditional good judgment in line with the flavour to turn/conceal particular UI components or capability.
  • Set up secrets and techniques: Retailer environment-specific secrets and techniques securely and get entry to them in line with the flavour.

Take note, flavors are an impressive software for managing app permutations successfully. By means of working out their doable and enforcing them successfully, you’ll be able to create adaptable and centered studies to your customers throughout other environments.

Thank you for attaining this a long way !.

Leave a Reply

Scroll to Top