如何在flutter中的单独文件中以单独的方法加载json数据?

发布于 2025-01-11 15:41:46 字数 1384 浏览 0 评论 0原文

我有一个用于更改背景颜色的 json 文件:

# settings.json

{
    "view_background_color": "#f5f242",
}

在我的 main.dart 文件中,我有一个名为 initializeThemeData()Future 方法code> 加载文件 settings.json ,然后将 json 字符串放入变量 data 中。该方法返回应用的 ThemeData()ThemeData() 使用 data['view_background_color'] 作为 scaffoldBackgroundColor 的值。

# main.dart 
Future<ThemeData> initializeThemeData() async {
  String jsonData =
      await rootBundle.loadString('assets/config/client_settings.json');
  Map<String, dynamic> data = jsonDecode(jsonData);

  return ThemeData(
    scaffoldBackgroundColor:
        Color(convertor.getColor(data['view_background_color'])),
    
  );
}

ThemeData() 中,我还获取了 Convertor() 类,我用它来将十六进制颜色转换为十六进制。这是转换 data['view_background_color'] 的类:

# convertor.dart
class Convertor {
  int getColor(String hexColor) {
    return int.parse("0xFF" + hexColor.substring(1));
  }

}

我的问题是我是否还可以创建一个单独的方法来加载 json 文件并将其放置在单独的文件中,然后从main.dart

然后我想放置一个像这样的变量: scaffoldBackgroundColor: Color(convertor.getColor(backGroundColor))

而不是: scaffoldBackgroundColor:颜色(convertor.getColor(data['view_background_color']))

I have this json file which i use to change the color of the background:

# settings.json

{
    "view_background_color": "#f5f242",
}

In my main.dart file i have a Future method called initializeThemeData() which loads the file settings.json and then places the json string inside the variable data. The method returns a ThemeData() for the app. ThemeData() uses the data['view_background_color'] as the value for scaffoldBackgroundColor.

# main.dart 
Future<ThemeData> initializeThemeData() async {
  String jsonData =
      await rootBundle.loadString('assets/config/client_settings.json');
  Map<String, dynamic> data = jsonDecode(jsonData);

  return ThemeData(
    scaffoldBackgroundColor:
        Color(convertor.getColor(data['view_background_color'])),
    
  );
}

Inside the ThemeData() i also grab the class Convertor() which i use to convert the hex color to a hexdecimal. This is the class that converts the data['view_background_color']:

# convertor.dart
class Convertor {
  int getColor(String hexColor) {
    return int.parse("0xFF" + hexColor.substring(1));
  }

}

My question is if i can also make a separate method for loading the json file and place it in a separate file and then call this method from the main.dart?

I then want to just place a variable like this: scaffoldBackgroundColor: Color(convertor.getColor(backGroundColor))

instead of :
scaffoldBackgroundColor: Color(convertor.getColor(data['view_background_color']))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌枕肩 2025-01-18 15:41:46

我最初的想法是想知道为什么这些都需要来自 json 文件。

这并不是 Flutter 真正的做事方式。除非我在这里遗漏了一些东西,否则这些只是硬编码的值。因此,您有额外的代码来完成这项工作,没有任何额外的好处,并且实际上使应用程序做更多的工作来完成同样的事情。

您可以只拥有一个常量文件,这样就可以避免任何转换。

const viewBackgroundColor = Color(0xFFf5f242);

然后将其传递到您的 ThemeData 中并完成它。

scaffoldBackgroundColor: viewBackgroundColor,

或者您可以将其放入 Settings 类中。

class Settings {
 static const viewBackgroundColor = Color(0xFFf5f242);
}

然后它看起来像这样

scaffoldBackgroundColor: Settings.viewBackgroundColor,

编辑:

当你的设计师仍在工作时,你可以做这样的事情。

class SettingsConverter {
  static Future<Color> getColorFromJson(String key) async {
    String jsonData = await rootBundle.loadString('assets/config/client_settings.json');
    Map<String, dynamic> data = jsonDecode(jsonData);
    return Color(ColorConvertor.getColor(data[key]));
  }
}

class ColorConvertor {
  static int getColor(String hexColor) {
    return int.parse("0xFF" + hexColor.substring(1));
  }
}

那么你的 initializeThemeData 将如下所示。

Future<ThemeData> initializeThemeData() async {
  final bgColor =
      await SettingsConverter.getColorFromJson('view_background_color');

  return ThemeData(
    scaffoldBackgroundColor: bgColor,
  );
}

My initial thought is wondering why any of of this needs to come from a json file to begin with.

It's not really the Flutter way of doing things. Unless I'm missing something here, these are just hardcoded values. So you have extra code to make that work, with no added benefit, and actually making the app do more work to accomplish the same thing.

You could just have a constants file and that would spare you any conversion.

const viewBackgroundColor = Color(0xFFf5f242);

Then you pass that into your ThemeData and be done with it.

scaffoldBackgroundColor: viewBackgroundColor,

Or you could put it in a Settings class.

class Settings {
 static const viewBackgroundColor = Color(0xFFf5f242);
}

Then it would look like this

scaffoldBackgroundColor: Settings.viewBackgroundColor,

Edit:

While your designers are still working, you could do something like this.

class SettingsConverter {
  static Future<Color> getColorFromJson(String key) async {
    String jsonData = await rootBundle.loadString('assets/config/client_settings.json');
    Map<String, dynamic> data = jsonDecode(jsonData);
    return Color(ColorConvertor.getColor(data[key]));
  }
}

class ColorConvertor {
  static int getColor(String hexColor) {
    return int.parse("0xFF" + hexColor.substring(1));
  }
}

Then your initializeThemeData would look like this.

Future<ThemeData> initializeThemeData() async {
  final bgColor =
      await SettingsConverter.getColorFromJson('view_background_color');

  return ThemeData(
    scaffoldBackgroundColor: bgColor,
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文