Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/google_fonts/lib/src/google_fonts_all_parts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

import 'google_fonts_base.dart';
import 'google_fonts_parts/part_a.dart';
Expand Down Expand Up @@ -44,6 +45,13 @@ class Config {
/// Whether or not the GoogleFonts library can make requests to
/// [fonts.google.com](https://fonts.google.com/) to retrieve font files.
bool allowRuntimeFetching = true;

/// The HTTP client used to fetch fonts.
///
/// If this is null, a shared default [http.Client] will be used.
///
/// If you supply a client, you are responsible for closing it.
http.Client? httpClient;
}

/// Provides configuration, and static methods to obtain [TextStyle]s and [TextTheme]s.
Expand Down
8 changes: 4 additions & 4 deletions packages/google_fonts/lib/src/google_fonts_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ void clearCache() => _loadedFonts.clear();
/// the [FontLoader], that future is removed from this set.
final Set<Future<void>> pendingFontFutures = <Future<void>>{};

/// The client used to fetch fonts.
@visibleForTesting
http.Client httpClient = http.Client();
/// Default client used to fetch fonts when one is not supplied.
final http.Client _httpClient = http.Client();

/// The asset manifest to use for loading pre-bundled fonts.
@visibleForTesting
Expand Down Expand Up @@ -260,8 +259,9 @@ Future<ByteData> _httpFetchFontAndSaveToDevice(
}

http.Response response;
final http.Client client = GoogleFonts.config.httpClient ?? _httpClient;
try {
response = await httpClient.get(uri);
response = await client.get(uri);
} catch (e) {
throw Exception('Failed to load font with url ${file.url}: $e');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ void main() {
late MockHttpClient mockHttpClient;

setUp(() async {
mockHttpClient = MockHttpClient();
httpClient = mockHttpClient;
assetManifest = MockAssetManifest();
mockHttpClient = MockHttpClient();
GoogleFonts.config.httpClient = mockHttpClient;
GoogleFonts.config.allowRuntimeFetching = true;
when(mockHttpClient.gets(any)).thenAnswer((_) async {
return http.Response(_fakeResponse, 200);
Expand Down
4 changes: 2 additions & 2 deletions packages/google_fonts/test/load_font_if_necessary_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ void main() {
late MockHttpClient mockHttpClient;

setUp(() async {
mockHttpClient = MockHttpClient();
httpClient = mockHttpClient;
assetManifest = MockAssetManifest();
mockHttpClient = MockHttpClient();
GoogleFonts.config.httpClient = mockHttpClient;
GoogleFonts.config.allowRuntimeFetching = true;
when(mockHttpClient.gets(any)).thenAnswer((_) async {
return http.Response(_fakeResponse, 200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ void main() {
late MockHttpClient mockHttpClient;

setUp(() async {
mockHttpClient = MockHttpClient();
httpClient = mockHttpClient;
assetManifest = MockAssetManifest();
mockHttpClient = MockHttpClient();
GoogleFonts.config.httpClient = mockHttpClient;
GoogleFonts.config.allowRuntimeFetching = true;
when(mockHttpClient.gets(any)).thenAnswer((_) async {
return http.Response(_fakeResponse, 200);
Expand Down