2023-02-11 19:19:18 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:lightmeter/data/models/supported_locale.dart';
|
|
|
|
import 'package:lightmeter/data/shared_prefs_service.dart';
|
|
|
|
import 'package:lightmeter/generated/l10n.dart';
|
2023-06-04 11:04:04 +00:00
|
|
|
import 'package:lightmeter/utils/inherited_generics.dart';
|
2023-02-11 19:19:18 +00:00
|
|
|
|
|
|
|
class SupportedLocaleProvider extends StatefulWidget {
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
const SupportedLocaleProvider({required this.child, super.key});
|
|
|
|
|
|
|
|
static SupportedLocaleProviderState of(BuildContext context) {
|
|
|
|
return context.findAncestorStateOfType<SupportedLocaleProviderState>()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<SupportedLocaleProvider> createState() => SupportedLocaleProviderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class SupportedLocaleProviderState extends State<SupportedLocaleProvider> {
|
|
|
|
late final ValueNotifier<SupportedLocale> valueListenable;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-06-04 11:04:04 +00:00
|
|
|
valueListenable = ValueNotifier(context.get<UserPreferencesService>().locale);
|
2023-02-11 19:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
valueListenable.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return ValueListenableBuilder(
|
|
|
|
valueListenable: valueListenable,
|
2023-06-04 11:04:04 +00:00
|
|
|
builder: (_, value, child) => InheritedWidgetBase<SupportedLocale>(
|
|
|
|
data: value,
|
|
|
|
child: child!,
|
2023-02-11 19:19:18 +00:00
|
|
|
),
|
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLocale(SupportedLocale locale) {
|
|
|
|
S.load(Locale(locale.intlName)).then((value) {
|
|
|
|
valueListenable.value = locale;
|
2023-06-04 11:04:04 +00:00
|
|
|
context.get<UserPreferencesService>().locale = locale;
|
2023-02-11 19:19:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|