|
| 1 | +// ignore_for_file: empty_constructor_bodies |
| 2 | + |
| 3 | +import 'package:flutter/services.dart'; |
| 4 | + |
| 5 | +class LoadingFontService { |
| 6 | + LoadingFontService() {} |
| 7 | + |
| 8 | + Future<void> initialize() async { |
| 9 | + // I am not updated on the recent AssetManifest API that recently |
| 10 | + // was added in the Flutter stable channel and so doing it |
| 11 | + // this way instead. |
| 12 | + // |
| 13 | + // Since we are not using GoogleFonts as it is not |
| 14 | + // fully support variable fonts yet we have to manually |
| 15 | + // load fonts into the cache |
| 16 | + final fontDataOne = await rootBundle.load( |
| 17 | + 'assets/google_fonts/RobotoFlex-VariableFont_GRAD,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght.ttf', |
| 18 | + ); |
| 19 | + final fontLoaderOne = FontLoader('RobotoFlex'); |
| 20 | + fontLoaderOne.addFont(fontDataOne as Future<ByteData>); |
| 21 | + await fontLoaderOne.load(); |
| 22 | + |
| 23 | + final fontDataTwo = |
| 24 | + await rootBundle.load('assets/google_fonts/RobotoSerif-Italic-VariableFont_GRAD,opsz,wdth,wght.ttf'); |
| 25 | + final fontLoaderTwo = FontLoader('RobotoSerif-Italian'); |
| 26 | + fontLoaderTwo.addFont(fontDataTwo as Future<ByteData>); |
| 27 | + await fontLoaderTwo.load(); |
| 28 | + |
| 29 | + final fontDataThree = |
| 30 | + await rootBundle.load('assets/google_fonts/RobotoSerif-VariableFont_GRAD,opsz,wdth,wght.ttf'); |
| 31 | + final fontLoaderThree = FontLoader('RobotoSerif'); |
| 32 | + fontLoaderThree.addFont(fontDataThree as Future<ByteData>); |
| 33 | + await fontLoaderThree.load(); |
| 34 | + |
| 35 | + final fontDataFour = await rootBundle.load('assets/google_fonts/RobotoSlab-VariableFont_wght.ttf'); |
| 36 | + final fontLoaderFour = FontLoader('RobotoSlab'); |
| 37 | + fontLoaderFour.addFont(fontDataFour as Future<ByteData>); |
| 38 | + await fontLoaderThree.load(); |
| 39 | + } |
| 40 | +} |
0 commit comments