wt1.1a flutter_bloc
This commit is contained in:
@@ -6,8 +6,9 @@ import 'package:flutter/services.dart';
|
||||
|
||||
class AppLocalizations {
|
||||
Locale locale;
|
||||
bool isTest;
|
||||
|
||||
AppLocalizations(this.locale);
|
||||
AppLocalizations(this.locale, {this.isTest = false});
|
||||
|
||||
// Helper method to keep the code in the widgets concise
|
||||
// Localizations are accessed using an InheritedWidget "of" syntax
|
||||
@@ -17,7 +18,7 @@ class AppLocalizations {
|
||||
|
||||
// Static member to have a simple access to the delegate from the MaterialApp
|
||||
static const LocalizationsDelegate<AppLocalizations> delegate =
|
||||
_AppLocalizationsDelegate();
|
||||
AppLocalizationsDelegate();
|
||||
|
||||
Map<String, String> _localizedStrings;
|
||||
|
||||
@@ -38,18 +39,24 @@ class AppLocalizations {
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<AppLocalizations> loadTest(Locale locale) async {
|
||||
return AppLocalizations(locale);
|
||||
}
|
||||
|
||||
// This method will be called from every widget which needs a localized text
|
||||
String translate(String key) {
|
||||
if (isTest) return key;
|
||||
return _localizedStrings[key];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class _AppLocalizationsDelegate
|
||||
class AppLocalizationsDelegate
|
||||
extends LocalizationsDelegate<AppLocalizations> {
|
||||
final bool isTest;
|
||||
// This delegate instance will never change (it doesn't even have fields!)
|
||||
// It can provide a constant constructor.
|
||||
const _AppLocalizationsDelegate();
|
||||
const AppLocalizationsDelegate({this.isTest = false});
|
||||
|
||||
@override
|
||||
bool isSupported(Locale locale) {
|
||||
@@ -60,11 +67,16 @@ class _AppLocalizationsDelegate
|
||||
@override
|
||||
Future<AppLocalizations> load(Locale locale) async {
|
||||
// AppLocalizations class is where the JSON loading actually runs
|
||||
AppLocalizations localizations = new AppLocalizations(locale);
|
||||
await localizations.load();
|
||||
AppLocalizations localizations = new AppLocalizations(locale, isTest: this.isTest);
|
||||
if (isTest) {
|
||||
await localizations.loadTest(locale);
|
||||
} else {
|
||||
await localizations.load();
|
||||
}
|
||||
return localizations;
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
bool shouldReload(_AppLocalizationsDelegate old) => false;
|
||||
bool shouldReload(AppLocalizationsDelegate old) => false;
|
||||
}
|
||||
Reference in New Issue
Block a user