34 lines
925 B
Dart
34 lines
925 B
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
enum AppLanguage { ru, en }
|
|
|
|
class LanguageController extends ValueNotifier<AppLanguage> {
|
|
LanguageController([super.value = AppLanguage.ru]);
|
|
|
|
void setLanguage(AppLanguage language) {
|
|
value = language;
|
|
}
|
|
}
|
|
|
|
class LanguageScope extends InheritedNotifier<LanguageController> {
|
|
const LanguageScope({
|
|
super.key,
|
|
required LanguageController controller,
|
|
required super.child,
|
|
}) : super(notifier: controller);
|
|
|
|
static LanguageController of(BuildContext context) {
|
|
final scope = context.dependOnInheritedWidgetOfExactType<LanguageScope>();
|
|
assert(scope != null, 'LanguageScope is missing in widget tree');
|
|
return scope!.notifier!;
|
|
}
|
|
}
|
|
|
|
extension TrExt on BuildContext {
|
|
String tr(String ru, String en) {
|
|
return LanguageScope.of(this).value == AppLanguage.ru ? ru : en;
|
|
}
|
|
|
|
bool get isRu => LanguageScope.of(this).value == AppLanguage.ru;
|
|
}
|