-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from bamlab/feat/add-custom-icons-font-support
feat: add custom font icons support
- Loading branch information
Showing
46 changed files
with
91 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+336 Bytes
(100%)
example/multi_packages_app/app/test/src/preview/desktop-app_multi_packages.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+336 Bytes
(100%)
..._packages_app/app/test/src/preview/desktop-app_multi_packages_with_keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+686 Bytes
(100%)
example/multi_packages_app/app/test/src/preview/iPadPro-app_multi_packages.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+690 Bytes
(100%)
..._packages_app/app/test/src/preview/iPadPro-app_multi_packages_with_keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.09 KB
(100%)
example/multi_packages_app/app/test/src/preview/iPhone_13-app_multi_packages.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.08 KB
(100%)
...ackages_app/app/test/src/preview/iPhone_13-app_multi_packages_with_keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+533 Bytes
(100%)
example/multi_packages_app/app/test/src/preview/iPhone_8-app_multi_packages.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+604 Bytes
(100%)
...packages_app/app/test/src/preview/iPhone_8-app_multi_packages_with_keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-197 Bytes
(100%)
example/multi_packages_app/app/test/src/preview/pixel_5-app_multi_packages.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-339 Bytes
(100%)
..._packages_app/app/test/src/preview/pixel_5-app_multi_packages_with_keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export './src/widgets/text.dart'; | ||
export './src/widgets/text_field.dart'; | ||
export './src/widgets/icon.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:material_symbols_icons/symbols.dart'; | ||
|
||
class AppIcon extends Icon { | ||
const AppIcon(super.icon, {super.key}); | ||
|
||
static const AppIcon image = AppIcon(Symbols.image); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+42 Bytes
(100%)
example/simple_app/test/src/preview/custom_path_desktop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+260 Bytes
(100%)
example/simple_app/test/src/preview/custom_path_iPadPro.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.41 KB
(98%)
example/simple_app/test/src/preview/custom_path_iPhone_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+257 Bytes
(100%)
example/simple_app/test/src/preview/custom_path_iPhone_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-680 Bytes
(99%)
example/simple_app/test/src/preview/custom_path_pixel_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+42 Bytes
(100%)
example/simple_app/test/src/preview/desktop-app_fade_in_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+42 Bytes
(100%)
example/simple_app/test/src/preview/desktop-app_simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+42 Bytes
(100%)
example/simple_app/test/src/preview/desktop-app_simple_with_keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+260 Bytes
(100%)
example/simple_app/test/src/preview/iPadPro-app_fade_in_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+260 Bytes
(100%)
example/simple_app/test/src/preview/iPadPro-app_simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+271 Bytes
(100%)
example/simple_app/test/src/preview/iPadPro-app_simple_with_keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.41 KB
(98%)
example/simple_app/test/src/preview/iPhone_13-app_fade_in_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.41 KB
(98%)
example/simple_app/test/src/preview/iPhone_13-app_simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.13 KB
(100%)
example/simple_app/test/src/preview/iPhone_13-app_simple_with_keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+257 Bytes
(100%)
example/simple_app/test/src/preview/iPhone_8-app_fade_in_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+257 Bytes
(100%)
example/simple_app/test/src/preview/iPhone_8-app_simple.png
Oops, something went wrong.
Binary file modified
BIN
+271 Bytes
(100%)
example/simple_app/test/src/preview/iPhone_8-app_simple_with_keyboard.png
Oops, something went wrong.
Binary file modified
BIN
-680 Bytes
(99%)
example/simple_app/test/src/preview/pixel_5-app_fade_in_image.png
Oops, something went wrong.
Binary file modified
BIN
-680 Bytes
(99%)
example/simple_app/test/src/preview/pixel_5-app_simple.png
Oops, something went wrong.
Binary file modified
BIN
-1.49 KB
(99%)
example/simple_app/test/src/preview/pixel_5-app_simple_with_keyboard.png
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,36 @@ | ||
import 'dart:io'; | ||
// ignore_for_file: avoid-dynamic, avoid-accessing-collections-by-constant-index | ||
|
||
import 'dart:convert'; | ||
|
||
import 'package:file/file.dart' as f; | ||
import 'package:file/local.dart' as l; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:path/path.dart' as path; | ||
import 'package:platform/platform.dart' as p; | ||
|
||
/// A class representing a package within a multi-packages app | ||
class Package { | ||
/// Creates a new [Package] instance. | ||
/// Either [name] or [relativePath] must be provided. | ||
Package({this.name, this.relativePath}) | ||
: assert(name != null || relativePath != null); | ||
|
||
/// This is the name of the package as defined in the pubspec.yaml file | ||
final String? name; | ||
|
||
/// This is the path to the package relative to where the test is run from. | ||
final String? relativePath; | ||
} | ||
|
||
/// Load fonts to make sure they show up in golden tests. | ||
/// Load fonts and icons to make sure they show up in golden tests. | ||
/// | ||
/// To use it efficiently: | ||
/// * Create a flutter_test_config.dart file. See: | ||
/// https://api.flutter.dev/flutter/flutter_test/flutter_test-library.html | ||
/// * add `await loadFonts();` in the `testExecutable` function. | ||
/// | ||
/// *Note* for this function to work, your package needs to include all fonts | ||
/// it uses in a font dir at the root of the project. | ||
Future<void> loadFonts([String? package]) { | ||
return package != null | ||
? loadFontsFromPackage( | ||
package: Package(name: package, relativePath: './$package'), | ||
) | ||
: loadFontsFromPackage(); | ||
} | ||
|
||
/// Load fonts from a given package to make sure they show up in golden tests. | ||
/// /// Load fonts to make sure they show up in golden tests. | ||
/// | ||
/// To use it efficiently: | ||
/// * Create a flutter_test_config.dart file. See: | ||
/// https://api.flutter.dev/flutter/flutter_test/flutter_test-library.html | ||
/// * add `await loadFontsFromPackage(Package(name: 'my_theme', relativePath: './theme'));` in the `testExecutable` function. | ||
/// | ||
/// *Note* for this function to work, your given package needs to include all fonts | ||
/// it uses in a font dir at the root of the project referenced by the given [package] argument. | ||
/// If no [package] is provided, it will look for a fonts dir at the root of the project. | ||
/// If a [package] is provided with a [Package.relativePath] it will look for a fonts dir with the package located at that path | ||
/// If a [package] is provided with a [Package.name] it will prefix the fonts dir with `packages/[package.name]` | ||
Future<void> loadFontsFromPackage({Package? package}) async { | ||
/// *Note* for this function to work, your package needs to include all fonts | ||
/// it uses az assets. | ||
Future<void> loadFonts() async { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
await _load(loadFontsFromFontsDir(package)); | ||
await _loadMaterialIconFont(); | ||
} | ||
|
||
/// Assumes a fonts dir in root of project | ||
@visibleForTesting | ||
Map<String, List<Future<ByteData>>> loadFontsFromFontsDir([Package? package]) { | ||
final fontFamilyToData = <String, List<Future<ByteData>>>{}; | ||
final currentDir = path.dirname(Platform.script.path); | ||
final fontsDirectory = path.join( | ||
currentDir, | ||
package == null || package.relativePath == null | ||
? 'fonts' | ||
: '${package.relativePath}/fonts', | ||
final fontManifest = await rootBundle.loadStructuredData<Iterable<dynamic>>( | ||
'FontManifest.json', | ||
(string) async => json.decode(string), | ||
); | ||
final prefix = package == null || package.name == null | ||
? '' | ||
: 'packages/${package.name}/'; | ||
for (final file in Directory(fontsDirectory).listSync()) { | ||
if (file is File) { | ||
final fontFamily = prefix + | ||
(path.basenameWithoutExtension(file.path).split('-').firstOrNull ?? | ||
''); | ||
(fontFamilyToData[fontFamily] ??= []).add(_fileToByteData(file)); | ||
} | ||
} | ||
|
||
return fontFamilyToData; | ||
} | ||
|
||
Future<ByteData> _fileToByteData(File file) async { | ||
final bytes = await file.readAsBytes(); | ||
|
||
return ByteData.view(bytes.buffer); | ||
} | ||
|
||
Future<void> _load(Map<String, List<Future<ByteData>>> fontFamilyToData) async { | ||
final waitList = <Future<void>>[]; | ||
for (final entry in fontFamilyToData.entries) { | ||
final loader = FontLoader(entry.key); | ||
for (final data in entry.value) { | ||
loader.addFont(data); | ||
for (final Map<String, dynamic> font in fontManifest) { | ||
final fontLoader = FontLoader(font['family']); | ||
|
||
for (final Map<String, dynamic> fontType in font['fonts']) { | ||
fontLoader.addFont(rootBundle.load(fontType['asset'])); | ||
} | ||
waitList.add(loader.load()); | ||
waitList.add(fontLoader.load()); | ||
} | ||
await Future.wait(waitList); | ||
} | ||
|
||
// Loads the cached material icon font. | ||
// Only necessary for golden tests. Relies on the tool updating cached assets | ||
// before running tests. | ||
Future<void> _loadMaterialIconFont() async { | ||
const f.FileSystem fs = l.LocalFileSystem(); | ||
const p.Platform platform = p.LocalPlatform(); | ||
final flutterRoot = fs.directory(platform.environment['FLUTTER_ROOT']); | ||
|
||
final iconFont = flutterRoot.childFile( | ||
fs.path.join( | ||
'bin', | ||
'cache', | ||
'artifacts', | ||
'material_fonts', | ||
'MaterialIcons-Regular.otf', | ||
), | ||
); | ||
|
||
final bytes = | ||
Future<ByteData>.value(iconFont.readAsBytesSync().buffer.asByteData()); | ||
|
||
await (FontLoader('MaterialIcons')..addFont(bytes)).load(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters