Giter VIP home page Giter VIP logo

flutter-global-configs's Introduction

Global Configs

gc-logo

GitHub license

A flutter package to manage application configurations via singleton pattern.

Show some ❤️ and star the repo to support the project

Installation

Add global_configs: ^1.1.1 to your pubspec.yaml dependencies. And import it:

import 'package:global_configs/global_configs.dart';

How to use

Simply load your configurations and use them everywhere in your application.

Load configurations

Before usgin configurations you need to load them from a Map or a JSON file.

Load from Map

If you consider to keep your configs in a dart file, create a file in project's directory like this:

/configs/dev.dart

final Map<String, dynamic> dev = {
  "appearance": {
    "defaultTheme": "Dark",
    "templateScale": 1,
  }
};

Then import the file at the top level of the project, and use loadFromMap function to simply load the configs.

import 'configs/dev.dart' as configs;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // Load configs from map
  GlobalConfigs().loadFromMap(configs.dev);

  runApp(MyApp());
}

Use pathoption to load configs into a specific path.

// Load configs from map
GlobalConfigs().loadFromMap(configs.dev, path: 'appearance.theme');

Load from JSON file

Create your JSON configuration file in your assets folder. for example: configs/dev.json

{
    "config1": "test",
    "config2": 1
}

Don't forget to update your pubspec.yaml file

flutter:
  assets:
    - configs/dev.json

Then use loadJsonFromdir function to load your JSON file

import 'package:global_configs/global_configs.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Load configs from json
  await GlobalConfigs().loadJsonFromdir('configs/dev.json');

  runApp(MyApp());
}

Use pathoption to load configs into a specific path.

// Load configs from json
await GlobalConfigs().loadJsonFromdir('configs/dev.json', path: 'appearance.theme');

Get configuration

After loading configurations use get function to simply access to a configuration.

T get<T>(String path, {T Function(dynamic)? converter})

Arguments

  • path (String): The path of the config to get.
  • converter (T Function(dynamic)?): The function to cast the value to a custom type

Returns

  • <T>|null: Returns the resolved value or null if the config is not found.

Example

Map<String, dynamic> configs = {
  'appearance': {
    'defaultTheme': 'Dark',
    'color': '0xFFB74093',
  },
  'size': 100,
};

// Load configs from map
GlobalConfigs().loadFromMap(configs.dev);

var size = GlobalConfigs().get('size'); // 100
var defaultTheme = GlobalConfigs().get('appearance.defaultTheme'); // Dark
Color color = GlobalConfigs().get<Color>(
  'appearance.color',
  converter: (value) => Color(int.parse(value)),
);

Set configuration

Use set function to update or add a new configuration.

void set<T>(String path, T value)

Arguments

  • path (String): The path of the config to set.
  • value (T): The value to set.

Example

Map<String, dynamic> configs = {
  'appearance': {
    'defaultTheme': 'Dark',
    'color': 'red',
  },
  'size': 100,
};

// Load configs from map
GlobalConfigs().loadFromMap(configs.dev);

GlobalConfigs().set('appearance.defaultTheme', 'Light'); // Light

Unset configuration

Use unset function to remove a configuration.

void unset(String path)

Arguments

  • path (String): The path of the property to remove.

Example

Map<String, dynamic> configs = {
  'appearance': {
    'defaultTheme': 'Dark',
    'color': 'red',
  },
  'size': 100,
};

// Load configs from map
GlobalConfigs().loadFromMap(configs.dev);

var defaultTheme = GlobalConfigs().unset('appearance.defaultTheme'); // {'appearance': {'color': 'red'}, 'size': 100}

For more information see examples

flutter-global-configs's People

Contributors

mehdizarepour avatar ahmed-osama-saad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.