Unverified Commit 872c9d7b authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Add maybeLocaleOf to Localizations (#68911)

parent 9383ec79
...@@ -5541,7 +5541,7 @@ class RichText extends MultiChildRenderObjectWidget { ...@@ -5541,7 +5541,7 @@ class RichText extends MultiChildRenderObjectWidget {
strutStyle: strutStyle, strutStyle: strutStyle,
textWidthBasis: textWidthBasis, textWidthBasis: textWidthBasis,
textHeightBehavior: textHeightBehavior, textHeightBehavior: textHeightBehavior,
locale: locale ?? Localizations.localeOf(context, nullOk: true), locale: locale ?? Localizations.maybeLocaleOf(context),
); );
} }
...@@ -5559,7 +5559,7 @@ class RichText extends MultiChildRenderObjectWidget { ...@@ -5559,7 +5559,7 @@ class RichText extends MultiChildRenderObjectWidget {
..strutStyle = strutStyle ..strutStyle = strutStyle
..textWidthBasis = textWidthBasis ..textWidthBasis = textWidthBasis
..textHeightBehavior = textHeightBehavior ..textHeightBehavior = textHeightBehavior
..locale = locale ?? Localizations.localeOf(context, nullOk: true); ..locale = locale ?? Localizations.maybeLocaleOf(context);
} }
@override @override
......
...@@ -2779,7 +2779,7 @@ class _Editable extends LeafRenderObjectWidget { ...@@ -2779,7 +2779,7 @@ class _Editable extends LeafRenderObjectWidget {
textScaleFactor: textScaleFactor, textScaleFactor: textScaleFactor,
textAlign: textAlign, textAlign: textAlign,
textDirection: textDirection, textDirection: textDirection,
locale: locale ?? Localizations.localeOf(context, nullOk: true), locale: locale ?? Localizations.maybeLocaleOf(context),
selection: value.selection, selection: value.selection,
offset: offset, offset: offset,
onSelectionChanged: onSelectionChanged, onSelectionChanged: onSelectionChanged,
...@@ -2824,7 +2824,7 @@ class _Editable extends LeafRenderObjectWidget { ...@@ -2824,7 +2824,7 @@ class _Editable extends LeafRenderObjectWidget {
..textScaleFactor = textScaleFactor ..textScaleFactor = textScaleFactor
..textAlign = textAlign ..textAlign = textAlign
..textDirection = textDirection ..textDirection = textDirection
..locale = locale ?? Localizations.localeOf(context, nullOk: true) ..locale = locale ?? Localizations.maybeLocaleOf(context)
..selection = value.selection ..selection = value.selection
..offset = offset ..offset = offset
..onSelectionChanged = onSelectionChanged ..onSelectionChanged = onSelectionChanged
......
...@@ -51,7 +51,7 @@ ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size? s ...@@ -51,7 +51,7 @@ ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size? s
return ImageConfiguration( return ImageConfiguration(
bundle: DefaultAssetBundle.of(context), bundle: DefaultAssetBundle.of(context),
devicePixelRatio: MediaQuery.maybeOf(context)?.devicePixelRatio ?? 1.0, devicePixelRatio: MediaQuery.maybeOf(context)?.devicePixelRatio ?? 1.0,
locale: Localizations.localeOf(context, nullOk: true), locale: Localizations.maybeLocaleOf(context),
textDirection: Directionality.maybeOf(context), textDirection: Directionality.maybeOf(context),
size: size, size: size,
platform: defaultTargetPlatform, platform: defaultTargetPlatform,
......
...@@ -416,14 +416,38 @@ class Localizations extends StatefulWidget { ...@@ -416,14 +416,38 @@ class Localizations extends StatefulWidget {
/// true, in which case it returns null. /// true, in which case it returns null.
static Locale? localeOf(BuildContext context, { bool nullOk = false }) { static Locale? localeOf(BuildContext context, { bool nullOk = false }) {
assert(context != null); assert(context != null);
assert(nullOk != null);
final _LocalizationsScope? scope = context.dependOnInheritedWidgetOfExactType<_LocalizationsScope>(); final _LocalizationsScope? scope = context.dependOnInheritedWidgetOfExactType<_LocalizationsScope>();
if (nullOk && scope == null) if (nullOk && scope == null)
return null; return null;
assert(scope != null, 'a Localizations ancestor was not found'); assert(() {
if (scope == null) {
throw FlutterError(
'Requested the Locale of a context that does not include a Localizations ancestor.\n'
'To request the Locale, the context used to retrieve the Localizations widget must '
'be that of a widget that is a descendant of a Localizations widget.'
);
}
if (!nullOk && scope.localizationsState.locale == null) {
throw FlutterError(
'Localizations.localeOf found a Localizations widget that had a unexpected null locale.\n'
);
}
return true;
}());
return scope!.localizationsState.locale; return scope!.localizationsState.locale;
} }
/// The locale of the Localizations widget for the widget tree that
/// corresponds to [BuildContext] `context`.
///
/// If no [Localizations] widget is in scope then this function will return
/// null. Equivalent to calling [localeOf] with `nullOk` set to true.
static Locale? maybeLocaleOf(BuildContext context) {
assert(context != null);
final _LocalizationsScope? scope = context.dependOnInheritedWidgetOfExactType<_LocalizationsScope>();
return scope?.localizationsState.locale;
}
// There doesn't appear to be a need to make this public. See the // There doesn't appear to be a need to make this public. See the
// Localizations.override factory constructor. // Localizations.override factory constructor.
static List<LocalizationsDelegate<dynamic>> _delegatesOf(BuildContext context) { static List<LocalizationsDelegate<dynamic>> _delegatesOf(BuildContext context) {
......
...@@ -34,6 +34,24 @@ void main() { ...@@ -34,6 +34,24 @@ void main() {
await tester.pump(); await tester.pump();
expect(find.text('loaded'), findsOneWidget); expect(find.text('loaded'), findsOneWidget);
}); });
testWidgets('Localizations.localeOf throws when no localizations exist', (WidgetTester tester) async {
final GlobalKey contextKey = GlobalKey(debugLabel: 'Test Key');
await tester.pumpWidget(Container(key: contextKey));
expect(() => Localizations.localeOf(contextKey.currentContext!), throwsA(isAssertionError.having(
(AssertionError e) => e.message,
'message',
contains('does not include a Localizations ancestor'),
)));
});
testWidgets('Localizations.maybeLocaleOf returns null when no localizations exist', (WidgetTester tester) async {
final GlobalKey contextKey = GlobalKey(debugLabel: 'Test Key');
await tester.pumpWidget(Container(key: contextKey));
expect(Localizations.maybeLocaleOf(contextKey.currentContext!), isNull);
});
} }
class FakeLocalizationsDelegate extends LocalizationsDelegate<String> { class FakeLocalizationsDelegate extends LocalizationsDelegate<String> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment