FontPicker class
Creates a widget that lets the user select a Google font from a provided list.
Inside your build
method, use a button that when pressed, will navigate to the font picker screen:
PickerFont? _selectedFont;
ElevatedButton(
child: Text('Pick a font'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FontPicker(
onFontChanged: (PickerFont font) {
_selectedFont = font;
print("${font.fontFamily} with font weight ${font.fontWeight} and font style ${font.fontStyle}.}");
},
),
),
);
}),
The onFontChanged function retrieves the font that the user selects with an object containing details like the font's name, weight, style, etc.
You can then use its toTextStyle
method to style any text with the selected font:
Text('This will be styled with the font: $_selectedFont.fontFamily',
style: selectedFont.toTextStyle()),
Use the showInDialog property to set if it will be shown in a dialog or in its own separate route.
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- FontPicker
Constructors
-
FontPicker({Key? key, List<
String> googleFonts = googleFontsList, bool showFontInfo = true, bool showFontVariants = true, bool showInDialog = false, int recentsCount = 3, required ValueChanged<PickerFont> onFontChanged, String? initialFontFamily, String lang = "en"}) -
Creates a widget that lets the user select a Google font from a provided list.
const
Properties
-
googleFonts
→ List<
String> -
The Google fonts that may be selected from the font picker. Check the
googleFontsList
constant for a list of the 975 fonts available.final - hashCode → int
-
The hash code for this object.
no setterinherited
- initialFontFamily → String?
-
The font family to use initially in the font picker. Defaults to 'Roboto'.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- lang → String
-
The language in which to show the UI. Defaults to English.
final
-
onFontChanged
→ ValueChanged<
PickerFont> -
The callback that returns a PickerFont object with all the details and methods for the font that the user selected.
final
- recentsCount → int
-
Fonts that the user selected before can be saved in
SharedPreferences
and shown at the start of the list. Sets how many you want saved as recents.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- showFontInfo → bool
-
Set whether to show font details (category, number of variants) next to each font tile in the list.
final
- showFontVariants → bool
-
Set whether to show font variants (weights and styles) in the font picker. If set to false, user will only be able to select the default variant of each font.
final
- showInDialog → bool
-
Set to true if the font picker will be used in an AlertDialog (check examples for usage).
final
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → _FontPickerState -
Creates the mutable state for this widget at a given location in the tree.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited