Skip to content

Instantly share code, notes, and snippets.

@chamithchathuka
Last active October 30, 2020 08:30
Show Gist options
  • Save chamithchathuka/8a84edf343f585b5fb516e9d68ba7eb5 to your computer and use it in GitHub Desktop.
Save chamithchathuka/8a84edf343f585b5fb516e9d68ba7eb5 to your computer and use it in GitHub Desktop.
Remote config Method
Future<RemoteConfig> setupRemoteConfig() async {
await Firebase.initializeApp();
final RemoteConfig remoteConfig = await RemoteConfig.instance;
remoteConfig.setConfigSettings(RemoteConfigSettings(debugMode: true));
remoteConfig.setDefaults(<String, dynamic>{
'primary_colour': 0xFFB74093,
'text_body_colour': 0xFFB71000,
'appbar_colour': 0xFFB71000,
'theme': 'system',
'enable_custom_theme': false,
});
try {
// Using default duration to force fetching from remote server.
await remoteConfig.fetch(expiration: const Duration(seconds: 0));
await remoteConfig.activateFetched();
bool enableCustomTheme = await remoteConfig.getBool('enable_custom_theme');
int primaryColour = await remoteConfig.getInt('primary_colour');
int textBodyColour = await remoteConfig.getInt('text_body_colour');
int appBarColour = await remoteConfig.getInt('appbar_colour');
String theme = await remoteConfig.getString('theme');
if (enableCustomTheme) {
switch (theme) {
case 'dark':
{
Get.changeTheme(ThemeData.dark().copyWith(
appBarTheme: AppBarTheme(color: Color(appBarColour)),
primaryColor: Color(primaryColour),
textTheme: TextTheme(
bodyText1: TextStyle(),
bodyText2: TextStyle(),
).apply(
bodyColor: Color(textBodyColour),
displayColor: Color(textBodyColour),
),
));
}
break;
case 'light':
{
Get.changeTheme(ThemeData.light().copyWith(
appBarTheme: AppBarTheme(color: Color(appBarColour)),
primaryColor: Color(primaryColour),
textTheme: TextTheme(
bodyText1: TextStyle(),
bodyText2: TextStyle(),
).apply(
bodyColor: Color(textBodyColour),
displayColor: Color(textBodyColour),
),
));
}
break;
default:
{
//statements;
}
break;
}
}
print('cloud app theme ${enableCustomTheme}');
print('cloud app theme ${theme}');
} on FetchThrottledException catch (exception) {
print(exception);
} catch (exception) {
print('Unable to fetch remote config. Cached or default values will be '
'used');
}
return remoteConfig;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment