-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Allow numeric BorderRadius values * Custom fonts and fontFamily * Remove typing warning
- Loading branch information
1 parent
79ea819
commit 0d2bc15
Showing
15 changed files
with
234 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/services.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
import '../models/control.dart'; | ||
|
||
class UserFonts { | ||
static Map<String, FontLoader> fontLoaders = {}; | ||
|
||
static void loadFont(String fontFamily, Uri fontUri) { | ||
var key = "$fontFamily$fontUri"; | ||
if (fontLoaders.containsKey(key)) { | ||
return; | ||
} | ||
var fontLoader = FontLoader(fontFamily); | ||
fontLoaders[key] = fontLoader; | ||
fontLoader.addFont(fetchFont(fontUri)); | ||
fontLoader.load(); | ||
} | ||
|
||
static Future<ByteData> fetchFont(Uri uri) async { | ||
final response = await http.get(uri); | ||
|
||
if (response.statusCode == 200) { | ||
return ByteData.view(response.bodyBytes.buffer); | ||
} else { | ||
// If that call was not successful, throw an error. | ||
throw Exception('Failed to load font $uri'); | ||
} | ||
} | ||
} | ||
|
||
Map<String, String> parseFonts(Control control, String propName) { | ||
var v = control.attrString(propName, null); | ||
if (v == null) { | ||
return {}; | ||
} | ||
|
||
final j1 = json.decode(v); | ||
return fontsFromJson(j1); | ||
} | ||
|
||
Map<String, String> fontsFromJson(Map<String, dynamic> json) { | ||
return json.map((key, value) => MapEntry(key, value)); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flet_view/utils/user_fonts.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
test("Custom fonts are parsed from JSON", () { | ||
const t1 = '''{ | ||
"font1": "https://fonts.com/font1.ttf", | ||
"font2": "https://fonts.com/font2.ttf" | ||
}'''; | ||
|
||
final j1 = json.decode(t1); | ||
var fonts = fontsFromJson(j1); | ||
|
||
expect(fonts.length, 2); | ||
expect(fonts["font1"], "https://fonts.com/font1.ttf"); | ||
expect(fonts["font2"], "https://fonts.com/font2.ttf"); | ||
}); | ||
} |
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
Oops, something went wrong.