Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
872c9d7b
Unverified
Commit
872c9d7b
authored
Nov 03, 2020
by
Greg Spencer
Committed by
GitHub
Nov 03, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add maybeLocaleOf to Localizations (#68911)
parent
9383ec79
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
7 deletions
+49
-7
basic.dart
packages/flutter/lib/src/widgets/basic.dart
+2
-2
editable_text.dart
packages/flutter/lib/src/widgets/editable_text.dart
+2
-2
image.dart
packages/flutter/lib/src/widgets/image.dart
+1
-1
localizations.dart
packages/flutter/lib/src/widgets/localizations.dart
+26
-2
localizations_test.dart
packages/flutter/test/widgets/localizations_test.dart
+18
-0
No files found.
packages/flutter/lib/src/widgets/basic.dart
View file @
872c9d7b
...
...
@@ -5541,7 +5541,7 @@ class RichText extends MultiChildRenderObjectWidget {
strutStyle:
strutStyle
,
textWidthBasis:
textWidthBasis
,
textHeightBehavior:
textHeightBehavior
,
locale:
locale
??
Localizations
.
localeOf
(
context
,
nullOk:
true
),
locale:
locale
??
Localizations
.
maybeLocaleOf
(
context
),
);
}
...
...
@@ -5559,7 +5559,7 @@ class RichText extends MultiChildRenderObjectWidget {
..
strutStyle
=
strutStyle
..
textWidthBasis
=
textWidthBasis
..
textHeightBehavior
=
textHeightBehavior
..
locale
=
locale
??
Localizations
.
localeOf
(
context
,
nullOk:
true
);
..
locale
=
locale
??
Localizations
.
maybeLocaleOf
(
context
);
}
@override
...
...
packages/flutter/lib/src/widgets/editable_text.dart
View file @
872c9d7b
...
...
@@ -2779,7 +2779,7 @@ class _Editable extends LeafRenderObjectWidget {
textScaleFactor:
textScaleFactor
,
textAlign:
textAlign
,
textDirection:
textDirection
,
locale:
locale
??
Localizations
.
localeOf
(
context
,
nullOk:
true
),
locale:
locale
??
Localizations
.
maybeLocaleOf
(
context
),
selection:
value
.
selection
,
offset:
offset
,
onSelectionChanged:
onSelectionChanged
,
...
...
@@ -2824,7 +2824,7 @@ class _Editable extends LeafRenderObjectWidget {
..
textScaleFactor
=
textScaleFactor
..
textAlign
=
textAlign
..
textDirection
=
textDirection
..
locale
=
locale
??
Localizations
.
localeOf
(
context
,
nullOk:
true
)
..
locale
=
locale
??
Localizations
.
maybeLocaleOf
(
context
)
..
selection
=
value
.
selection
..
offset
=
offset
..
onSelectionChanged
=
onSelectionChanged
...
...
packages/flutter/lib/src/widgets/image.dart
View file @
872c9d7b
...
...
@@ -51,7 +51,7 @@ ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size? s
return
ImageConfiguration
(
bundle:
DefaultAssetBundle
.
of
(
context
),
devicePixelRatio:
MediaQuery
.
maybeOf
(
context
)?.
devicePixelRatio
??
1.0
,
locale:
Localizations
.
localeOf
(
context
,
nullOk:
true
),
locale:
Localizations
.
maybeLocaleOf
(
context
),
textDirection:
Directionality
.
maybeOf
(
context
),
size:
size
,
platform:
defaultTargetPlatform
,
...
...
packages/flutter/lib/src/widgets/localizations.dart
View file @
872c9d7b
...
...
@@ -416,14 +416,38 @@ class Localizations extends StatefulWidget {
/// true, in which case it returns null.
static
Locale
?
localeOf
(
BuildContext
context
,
{
bool
nullOk
=
false
})
{
assert
(
context
!=
null
);
assert
(
nullOk
!=
null
);
final
_LocalizationsScope
?
scope
=
context
.
dependOnInheritedWidgetOfExactType
<
_LocalizationsScope
>();
if
(
nullOk
&&
scope
==
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
;
}
/// 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
// Localizations.override factory constructor.
static
List
<
LocalizationsDelegate
<
dynamic
>>
_delegatesOf
(
BuildContext
context
)
{
...
...
packages/flutter/test/widgets/localizations_test.dart
View file @
872c9d7b
...
...
@@ -34,6 +34,24 @@ void main() {
await
tester
.
pump
();
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
>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment