[英]Flutter constants File with Provider package
I'm having trouble figuring out the best way to set up some constants (mainly strings).我无法找出设置一些常量(主要是字符串)的最佳方法。 Currently I'm using a constants.dart file that just has the const variables defined in there and import it whenever it's needed.目前我正在使用一个 constants.dart 文件,该文件只在其中定义了 const 变量,并在需要时将其导入。 No class or anything, just a blank dart file.没有 class 或任何东西,只是一个空白的 dart 文件。 This works, however, I recently implemented localization using the Flutter Intl plugin in Android Studio.这可行,但是,我最近在 Android Studio 中使用 Flutter Intl 插件实现了本地化。 I got everything to work and can do something like this S.of(context).settings
and it gets the translation from the correct file.我得到了一切工作,可以做这样的事情S.of(context).settings
它从正确的文件中获取翻译。 My problem comes with some constant list of strings I have in my constants.dart file.我的问题来自我的 constants.dart 文件中的一些常量字符串列表。 I use them in many places for option selects.我在很多地方使用它们来选择选项。 They look like this:它们看起来像这样:
const playType = [
'RP/Story Focused',
'Battle/Combat Focused',
'Puzzles and Challenges',
'Exploration/Travel',
];
const length = [
'One Shot',
'2-5 Sessions',
'5-10 Sessions',
'On-going Campaign',
];
I cant change the the strings to the Intl reference because there is not context to be passed.我无法将字符串更改为 Intl 引用,因为没有要传递的上下文。 Not sure how to set up a class that is loaded but not sure how to set that up and use the Provider package to serve it up.不确定如何设置已加载的 class,但不确定如何设置并使用提供程序 package 来提供服务。
EDIT: heres the Constants file.编辑:继承人常量文件。 Calling this with the provider is fine.用提供者调用这个很好。 The issue comes when I need to use the localization on the strings in the lists当我需要对列表中的字符串使用本地化时,问题就出现了
import 'package:scryer/generated/l10n.dart';
class Constants {
Constants._();
static final instance = Constants._();
static List<String> playType = [
S.of(context).rpstoryFocused,//need a reference to a context
'Battle/Combat Focused',
'Puzzles and Challenges',
'Exploration/Travel',
];
static const length = [
'One Shot',
'2-5 Sessions',
'5-10 Sessions',
'On-going Campaign',
];
}
Heres how I call the constants on the actual page.这是我在实际页面上调用常量的方式。 Its a button that on press goes to a new screen that is either a multiselect checkbox list or single select radiobutton list.它是一个按钮,按下时会转到一个新屏幕,该屏幕是多选复选框列表或单个 select 单选按钮列表。 I pass the constants as an argument for the list我将常量作为列表的参数传递
MaterialPageRoute(builder: (context) {
return MultiSelectScreen(
args: MultiSelectArguments(
label: S.of(context).selectPreferredPlayStyle,
options: playType, //this is the constants list reference
selections:
profile.playType,
),
);
})
The easy solution is to not use a constant and just create the lists in these spots, only used like twice i think, but better practice is to pull it out since its being used multiple times简单的解决方案是不使用常量,只在这些位置创建列表,我认为只使用了两次,但更好的做法是把它拉出来,因为它被多次使用
you can try.你可以试试。
class Constants {
final BuildContext context;
Constants(@required this.context);
// static final instance = Constants._(); // to make class a singleton
static const playType = [
'RP/Story Focused',
'Battle/Combat Focused',
'Puzzles and Challenges',
'Exploration/Travel',
];
static const length = [
'One Shot',
'2-5 Sessions',
'5-10 Sessions',
'On-going Campaign',
];
}
Then, at the root of your project you should provide this class like;然后,在项目的根目录中,您应该提供这个 class 之类的;
void main() {
/** WidgetsFlutterBinding.ensureInitialized() is required in Flutter v1.9.4+ before using any plugins if the code is executed before runApp. */
WidgetsFlutterBinding.ensureInitialized();
runApp( Provider(
create: (context) => Constants(context),
child: MyApp(),
),
);
}
You will be able to access these constants in any BuildContext anywhere down the widget tree as follows;您将能够在小部件树的任何位置访问任何 BuildContext 中的这些常量,如下所示;
final Contants constants = Provider<Constants>.of(context);
you get the length constant like;你得到的长度常数像; constants.length
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.