如何在flutter中的单独文件中以单独的方法加载json数据?
我有一个用于更改背景颜色的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最初的想法是想知道为什么这些都需要来自
json
文件。这并不是 Flutter 真正的做事方式。除非我在这里遗漏了一些东西,否则这些只是硬编码的值。因此,您有额外的代码来完成这项工作,没有任何额外的好处,并且实际上使应用程序做更多的工作来完成同样的事情。
您可以只拥有一个常量文件,这样就可以避免任何转换。
然后将其传递到您的
ThemeData
中并完成它。或者您可以将其放入
Settings
类中。然后它看起来像这样
编辑:
当你的设计师仍在工作时,你可以做这样的事情。
那么你的
initializeThemeData
将如下所示。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.
Then you pass that into your
ThemeData
and be done with it.Or you could put it in a
Settings
class.Then it would look like this
Edit:
While your designers are still working, you could do something like this.
Then your
initializeThemeData
would look like this.