Add fallback font to IconData class (#127269)

This PR adds an ability to specify `fallbackFont` for `Icon` class via `IconData` props.

Read detailed description here https://github.com/flutter/flutter/issues/126670
This commit is contained in:
Vasiliy Ditsyak 2023-06-01 17:43:18 +02:00 committed by GitHub
parent 764f4cc8c2
commit 114f05cef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -280,6 +280,7 @@ class Icon extends StatelessWidget {
fontSize: iconSize,
fontFamily: icon!.fontFamily,
package: icon!.fontPackage,
fontFamilyFallback: icon!.fontFamilyFallback,
shadows: iconShadows,
),
),

View File

@ -31,6 +31,7 @@ class IconData {
this.fontFamily,
this.fontPackage,
this.matchTextDirection = false,
this.fontFamilyFallback,
});
/// The Unicode code point at which this icon is stored in the icon font.
@ -56,6 +57,11 @@ class IconData {
/// [Directionality] is [TextDirection.rtl].
final bool matchTextDirection;
/// The ordered list of font families to fall back on when a glyph cannot be found in a higher priority font family.
///
/// For more details, refer to the documentation of [TextStyle]
final List<String>? fontFamilyFallback;
@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType) {
@ -65,11 +71,20 @@ class IconData {
&& other.codePoint == codePoint
&& other.fontFamily == fontFamily
&& other.fontPackage == fontPackage
&& other.matchTextDirection == matchTextDirection;
&& other.matchTextDirection == matchTextDirection
&& listEquals(other.fontFamilyFallback, fontFamilyFallback);
}
@override
int get hashCode => Object.hash(codePoint, fontFamily, fontPackage, matchTextDirection);
int get hashCode {
return Object.hash(
codePoint,
fontFamily,
fontPackage,
matchTextDirection,
Object.hashAll(fontFamilyFallback ?? const <String?>[]),
);
}
@override
String toString() => 'IconData(U+${codePoint.toRadixString(16).toUpperCase().padLeft(5, '0')})';

View File

@ -127,6 +127,20 @@ void main() {
expect(richText.text.style!.fontFamily, equals('Roboto'));
});
testWidgets('Icon with custom fontFamilyFallback', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: Icon(IconData(0x41, fontFamilyFallback: <String>['FallbackFont'])),
),
),
);
final RichText richText = tester.firstWidget(find.byType(RichText));
expect(richText.text.style!.fontFamilyFallback, equals(<String>['FallbackFont']));
});
testWidgets('Icon with semantic label', (WidgetTester tester) async {
final SemanticsTester semantics = SemanticsTester(tester);