flutter_font_picker 0.3.1 flutter_font_picker: ^0.3.1 copied to clipboard
A Flutter widget that lets the user select a Google Font from a custom dropdown.
import 'package:flutter/material.dart';
import 'package:flutter_font_picker/flutter_font_picker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Font Picker Demo',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: MyHomePage(title: 'Font Picker Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _selectedFont = "Roboto";
TextStyle? _selectedFontTextStyle;
List<String> _myGoogleFonts = [
"Abril Fatface",
"Aclonica",
"Alegreya Sans",
"Architects Daughter",
"Archivo",
"Archivo Narrow",
"Bebas Neue",
"Bitter",
"Bree Serif",
"Bungee",
"Cabin",
"Cairo",
"Coda",
"Comfortaa",
"Comic Neue",
"Cousine",
"Croissant One",
"Faster One",
"Forum",
"Great Vibes",
"Heebo",
"Inconsolata",
"Josefin Slab",
"Lato",
"Libre Baskerville",
"Lobster",
"Lora",
"Merriweather",
"Montserrat",
"Mukta",
"Nunito",
"Offside",
"Open Sans",
"Oswald",
"Overlock",
"Pacifico",
"Playfair Display",
"Poppins",
"Raleway",
"Roboto",
"Roboto Mono",
"Source Sans Pro",
"Space Mono",
"Spicy Rice",
"Squada One",
"Sue Ellen Francisco",
"Trade Winds",
"Ubuntu",
"Varela",
"Vollkorn",
"Work Sans",
"Zilla Slab"
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: Text('Pick a font (with a screen)'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FontPicker(
recentsCount: 10,
onFontChanged: (font) {
setState(() {
_selectedFont = font.fontFamily;
_selectedFontTextStyle = font.toTextStyle();
});
print(
"${font.fontFamily} with font weight ${font.fontWeight} and font style ${font.fontStyle}. FontSpec: ${font.toFontSpec()}");
},
googleFonts: _myGoogleFonts)),
);
}),
ElevatedButton(
child: Text('Pick a font (with a dialog)'),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: SingleChildScrollView(
child: Container(
width: double.maxFinite,
child: FontPicker(
showInDialog: true,
onFontChanged: (font) {
setState(() {
_selectedFont = font.fontFamily;
_selectedFontTextStyle = font.toTextStyle();
});
print(
"${font.fontFamily} with font weight ${font.fontWeight} and font style ${font.fontStyle}. FontSpec: ${font.toFontSpec()}");
},
googleFonts: _myGoogleFonts),
),
));
},
);
}),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Pick a font: ',
textAlign: TextAlign.right,
style: TextStyle(fontWeight: FontWeight.w700)),
)),
Expanded(
child: TextField(
readOnly: true,
textAlign: TextAlign.center,
decoration: InputDecoration(
suffixIcon: const Icon(Icons.arrow_drop_down_sharp),
hintText: _selectedFont,
border: InputBorder.none),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FontPicker(
onFontChanged: (font) {
setState(() {
_selectedFont = font.fontFamily;
_selectedFontTextStyle =
font.toTextStyle();
});
print(
"${font.fontFamily} with font weight ${font.fontWeight} and font style ${font.fontStyle}. FontSpec: ${font.toFontSpec()}");
},
googleFonts: _myGoogleFonts)),
);
},
),
),
],
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.blueGrey, width: 2.0)),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Font: $_selectedFont',
style: _selectedFontTextStyle),
Text('The quick brown fox jumped',
style: _selectedFontTextStyle),
Text('over the lazy dog',
style: _selectedFontTextStyle),
])),
),
),
),
)
],
),
)));
}
}