A Flutter package for managing application configuration with support for multiple environments (UAT, PROD) and a user-friendly configuration UI.
- ๐ Easy environment switching between UAT and PROD
- ๐จ Material 3 design with a modern bottom sheet UI
- ๐ Secure configuration storage
- ๐ฑ Support for both mobile and web platforms
- ๐ฏ Provider-based state management
- ๐ Debug mode toggle
- ๐ Additional configuration support
- ๐ฎ Customizable configuration UI
Add this to your package's pubspec.yaml file:
dependencies:
app_config_manager: ^1.0.0- First, initialize the configuration manager with your default configuration:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Create default configuration
final defaultConfig = AppConfig(
uat: EnvironmentConfig(
baseUrl: 'https://api-uat.example.com',
additionalConfig: {
'apiVersion': 'v1',
'timeout': 30000,
'debugMode': true,
},
),
prod: EnvironmentConfig(
baseUrl: 'https://api.example.com',
additionalConfig: {
'apiVersion': 'v1',
'timeout': 30000,
'debugMode': false,
},
),
);
final configManager = ConfigManager();
await configManager.initialize(defaultConfig: defaultConfig);
runApp(
ChangeNotifierProvider(
create: (_) => AppConfigNotifier(configManager),
child: const MyApp(),
),
);
}- Use the configuration in your app:
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<AppConfigNotifier>(
builder: (context, configNotifier, child) {
final config = configNotifier.config;
return Text('Current Base URL: ${config.currentConfig.baseUrl}');
},
);
}
}- Show the configuration UI:
IconButton(
icon: const Icon(Icons.settings),
onPressed: () {
showAppConfigBottomSheet(
context: context,
onConfigChanged: (config) {
// Optional callback when configuration changes
},
);
},
)The main configuration class that holds all environment configurations:
final config = AppConfig(
uat: EnvironmentConfig(
baseUrl: 'https://api-uat.example.com',
additionalConfig: {
'apiVersion': 'v1',
'timeout': 30000,
'debugMode': true,
},
),
prod: EnvironmentConfig(
baseUrl: 'https://api.example.com',
additionalConfig: {
'apiVersion': 'v1',
'timeout': 30000,
'debugMode': false,
},
),
);Configuration for a specific environment:
final uatConfig = EnvironmentConfig(
baseUrl: 'https://api-uat.example.com',
additionalConfig: {
'apiVersion': 'v1',
'timeout': 30000,
'debugMode': true,
},
);The package uses Provider for state management. The AppConfigNotifier class manages the configuration state and notifies listeners when changes occur.
// Access the configuration
final configNotifier = context.read<AppConfigNotifier>();
final config = configNotifier.config;
// Listen to changes
Consumer<AppConfigNotifier>(
builder: (context, configNotifier, child) {
final config = configNotifier.config;
return Text('Current Environment: ${config.currentEnvironment}');
},
)class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Automatically rebuilds when config changes
final config = context.watch<AppConfigNotifier>().config;
return Column(
children: [
Text('Environment: ${config.currentEnvironment}'),
Text('Base URL: ${config.currentConfig.baseUrl}'),
Text('Debug Mode: ${config.currentConfig.additionalConfig['debugMode']}'),
],
);
}
}void onButtonPressed(BuildContext context) {
// Access configuration once (doesn't rebuild on changes)
final configNotifier = context.read<AppConfigNotifier>();
final config = configNotifier.config;
print('Current Base URL: ${config.currentConfig.baseUrl}');
}class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
// listen: true means it will rebuild on changes
final config = Provider.of<AppConfigNotifier>(context, listen: true).config;
return Text('Current Environment: ${config.currentEnvironment}');
}
}The configuration bottom sheet can be customized:
showAppConfigBottomSheet(
context: context,
onConfigChanged: (config) {
// Handle configuration changes
},
closeIcon: Icons.close, // Custom close icon
);The package supports both mobile and web platforms. For web support, add the following to your web/index.html:
<script src="flutter_web_plugins.js" defer></script>Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.