resource_manager 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 44

resource_manager #

This is a library of Flutter for resource manager works as similar as what Android does. It works with JSON file.



How to use #

  1. Use initBaseResources(double devicePixelRatio, String baseResourceFile) to init the base resources from JSON file.

使用initBaseResources(double devicePixelRatio, String baseResourceFile)从指定JSON文件初始化基础资源。

eg.,initBaseResources(window.devicePixelRatio, 'res/values.json')

  1. Use getResources(String jsonFile) to get resources from JSON file.

使用getResources(String jsonFile)从JSON文件获取资源。

eg., getResources('res/example.json')

  1. Use getXXX(String name, {Resources fromResources, Set<String> soughtNames}) to get the resource by name.

使用getXXX(String name, {Resources fromResources, Set<String> soughtNames})获取指定名称资源。

JSON file #

The JSON file for resources has nodes named dimens, colors, drawable, strings, integers, doubles.

资源JSON文件的节点有 dimens, colors, drawable, strings, integers, doubles

eg., value.json

    "dimens": {
        "h1_text_size": "32dp",
        "h2_text_size": "24dp",
        "h3_text_size": "19dp",
        "h4_text_size": "16dp",
        "h5_text_size": "14dp",
        "h6_text_size": "13dp"
    "colors": {
        "black": "#000000",
        "FireBrick": "#B22222",
        "DeepPink": "#FF1493",
        "Gold": "#FFD700",
        "OliveDrab": "#6B8E23",
        "Lime": "#00FF00",
        "RoyalBlue": "#4169E1",
        "DarkOrchid": "#9932CC"
    "drawable": {
        "logo": "graphics/FlutterDart.png"
    "strings": {
        "string 1": "base string 1"
    "integers": {
        "integer 1": 10,
        "integer 2": 20
    "doubles": {
        "double 1": 30.3,
        "double 2": 40

And example.json

    "dimens": {
        "example_text_size": "@dimen/to base h2",
        "to base h2": "@dimen/h2_text_size",
        "color_bar_height": "30dp",
        "color_bar_padding": "5dp",
        "list_padding": "20dp",
        "gap": "50dp",
        "shown dp": "50dp",
        "shown px": "50px"
    "colors": {
        "As FireBrick": "@color/to base fire brick",
        "to base fire brick": "@color/FireBrick",
        "H1 color": "@color/RoyalBlue",
        "H2 color": "@color/RoyalBlue",
        "H3 color": "@color/RoyalBlue",
        "H4 color": "@color/RoyalBlue",
        "H5 color": "@color/RoyalBlue",
        "H6 color": "@color/RoyalBlue",
        "image label color": "@color/DarkOrchid",
        "num label color": "@color/Gold",
        "pixel label color": "@color/Lime",
        "string label color": "@color/OliveDrab"
    "drawable": {
        "logo": "graphics/flutter.PNG"
    "strings": {
        "string 1": "example string 1"
    "integers": {
        "integer 1": "@integer/integer 3",
        "integer 2": 21,
        "integer 3": "@integer/integer 1"
    "doubles": {
        "double 1": "@double/double 1",
        "double 2": 41

Value can be a string with prefix, such @dimen/, @color/, @drawable/, @string/, @integer/ and @double/, and then refer to another resource. If the resource can't be find in the referred resources, it would keep looking for the resource in the base resources.

值可以是有 @dimen/, @color/, @drawable/, @string/, @integer/@double/ 为前缀的字符串,指向其他资源。当在指定资源文件中找不到指定名称资源时,继续在基础资源文件中寻找。

Methods for getting resource #

  1. Dimen: getDimension(String name, {Resources fromResources, Set<String> soughtNames})

  2. Color: getColor(String name, {Resources fromResources, Set<String> soughtNames})

  3. Drawable: getDrawable(String name, {Resources fromResources, Set<String> soughtNames})

  4. String: getString(String name, {Resources fromResources, Set<String> soughtNames})

  5. Integer: getInteger(String name, {Resources fromResources, Set<String> soughtNames})

  6. Double: getDouble(String name, {Resources fromResources, Set<String> soughtNames})

If optional parameter fromResources is ignored, then the methods would try to get resources from base resource file.


Ignore optional parameter soughtNames, please.


[1.0.1] - 26/03/2019.

  • Add type annotations

[1.0.0] - 21/03/2019.

  • Including 'dimens', 'colors', 'drawable', 'strings', 'integers' and 'doubles'
  • With example app.


Example app for resource_manager

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  resource_manager: ^1.0.2

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:resource_manager/resource_manager.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Health suggestions

Fix lib/resource_manager.dart. (-0.50 points)

Analysis of lib/resource_manager.dart reported 1 hint:

line 38 col 5: The declaration '_dpToPxWithDensity' isn't referenced.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (json_annotation).

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
json_annotation ^2.0.0 2.4.0 3.0.1
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
build_runner ^1.0.0
json_serializable ^2.0.0