Add a LocaleQuery widget that can be used to fetch locale-specific data

Users of MaterialApp can provide an onLocaleChanged handler that will be
called to asynchronously fetch locale-specific data.  MaterialApp will
then instantiate a LocaleQuery that supplies the locale data to its
descendants.
This commit is contained in:
Jason Simmons
2015-12-07 11:19:15 -08:00
parent 1e1761d181
commit 9693cd5537
8 changed files with 107 additions and 28 deletions

View File

@@ -94,6 +94,14 @@ class StocksAppState extends State<StocksApp> {
return null;
}
Future<LocaleQueryData> _onLocaleChanged(ui.Locale locale) {
String localeString = locale.toString();
return initializeMessages(localeString).then((_) {
Intl.defaultLocale = localeString;
return StockStrings.instance;
});
}
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Stocks',
@@ -102,13 +110,12 @@ class StocksAppState extends State<StocksApp> {
'/': (RouteArguments args) => new StockHome(_stocks, _symbols, _optimismSetting, modeUpdater),
'/settings': (RouteArguments args) => new StockSettings(_optimismSetting, _backupSetting, settingsUpdater)
},
onGenerateRoute: _getRoute
onGenerateRoute: _getRoute,
onLocaleChanged: _onLocaleChanged
);
}
}
void main() {
initializeMessages(Intl.defaultLocale).then((_) {
runApp(new StocksApp());
});
runApp(new StocksApp());
}

View File

@@ -147,7 +147,7 @@ class StockHomeState extends State<StockHome> {
Widget buildToolBar() {
return new ToolBar(
elevation: 0,
center: new Text(StockStrings.title()),
center: new Text(StockStrings.of(context).title()),
right: <Widget>[
new IconButton(
icon: "action/search",
@@ -161,8 +161,9 @@ class StockHomeState extends State<StockHome> {
tabBar: new TabBar(
selection: _tabBarSelection,
labels: <TabLabel>[
new TabLabel(text: StockStrings.market()),
new TabLabel(text: StockStrings.portfolio())]
new TabLabel(text: StockStrings.of(context).market()),
new TabLabel(text: StockStrings.of(context).portfolio())
]
)
);
}

View File

@@ -8,20 +8,26 @@ part of stocks;
// To generate the stock_messages_*.dart files from the ARB files, run:
// pub run intl:generate_from_arb --output-dir=lib/i18n --generated-file-prefix=stock_ --no-use-deferred-loading lib/stock_strings.dart lib/i18n/stocks_*.arb
class StockStrings {
static String title() => Intl.message(
class StockStrings extends LocaleQueryData {
static StockStrings of(BuildContext context) {
return LocaleQuery.of(context);
}
static final StockStrings instance = new StockStrings();
String title() => Intl.message(
'Stocks',
name: 'title',
desc: 'Title for the Stocks application'
);
static String market() => Intl.message(
String market() => Intl.message(
'MARKET',
name: 'market',
desc: 'Label for the Market tab'
);
static String portfolio() => Intl.message(
String portfolio() => Intl.message(
'PORTFOLIO',
name: 'portfolio',
desc: 'Label for the Portfolio tab'