I'm new to flutter and collecting fragments of code from here and there.
I have a class that holds some data that will be passed later to a widget and generate the UI based on the passed data
class OnBoardingViewModel with ChangeNotifier {
final List<OnBoardingPageContent> onBoardingPages = [
OnBoardingPageContent(
image: ImageManager.onBoardingImage1,
title: StringManager.onBoardingTitle1,// I want the text to be localized
subtitle: StringManager.onBoardingSubTitle1,
),
OnBoardingPageContent(
image: ImageManager.onBoardingImage2,
title: StringManager.onBoardingTitle2,
subtitle: StringManager.onBoardingSubTitle2,
)
];
... some other code
the strings above are hardcoded with specific language I wanted them to be localized
the localization require
S.of(context).yourKey
I thought of passing the key as a string and in UI build (where I will have the context) to do
`S.of(context)[thePassedKeyStringVar]` // but that is wrong in Dart
I tried to pass the context to my Class OnBoardingViewModel
so I could have
class OnBoardingViewModel with ChangeNotifier {
static BuildContext context;
final List<OnBoardingPageContent> onBoardingPages = [
OnBoardingPageContent(
title: S.of(context).onBoardingText1,
but it did not end well with me
here is the entry point of my app
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => OnBoardingViewModel(_),
),
...
],
child: const MyApp(),
),
);
}
any help on how to localize the strings in my static list in the above class?