Working out Flutter Flavors. Have you ever ever puzzled the best way to arrange… | via Muayad alrazehi | Feb, 2024

Have you ever ever puzzled the best way to 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 proportion a commonplace codebase however have distinct configurations, options, or branding. That is in particular helpful when managing more than a few environments corresponding to building, staging, and manufacturing.

Let’s get our fingers grimy and notice what we’re speaking about in motion.
Function: create a flutter Android app with two environments every with a unique colour and name and construct APKs from every surroundings which can be distinct of their utility ID and construct identify.

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

elegance 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 means and exchange it with this one

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

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

elegance FlavorsApp extends StatelessWidget {
ultimate EnvConfig envConfig;

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

@override
Widget construct(BuildContext context) {
go back MaterialApp(
name: 'Flutter Flavors Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: envConfig.mainColor),
),
house: Scaffold(
appBar: AppBar(
name: Textual content('Env is ${envConfig.env}'),
),
frame: const Middle(
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 be able to run and take a look at if the whole lot goes neatly to this point

and the similar factor for env2.

now we will be able to make a choice the surroundings to run on

and notice the adjustments showing

Just right So A long way 🤩 , however we didn’t use taste but😱
and we don’t know the way we will be able to construct APKs with other IDs and Construct in response to the surroundings, right here the place flavorDimensions come

let’s cross to app-level construct.gradle record 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 have more than one dimensions like flavor_demo, flavor_demo2 and so forth

applicationIdSuffix Shall be appended to the applicationId

versionNameSuffix Shall be appended to the versionName

now you’ll 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

Smartly, this was once a small clarification about flavors in Android, and identical steps the usage of schemes may also be finished in IOS, That is simply a kick off point. You’ll be able to use flavors for extra complicated eventualities:

  • Hook up with other Firebase tasks: Use environment-specific GoogleServices-Data.plist information for every taste.
  • Permit/disable options: Use conditional good judgment in response to the flavour to turn/disguise particular UI components or capability.
  • Organize secrets and techniques: Retailer environment-specific secrets and techniques securely and get entry to them in response to the flavour.

Take note, flavors are a formidable software for managing app permutations successfully. Via working out their doable and imposing them successfully, you’ll create adaptable and centered stories in your customers throughout other environments.

Thank you for attaining this a long way !.

Leave a Reply

Your email address will not be published. Required fields are marked *